Skip to content

Commit 7aa8739

Browse files
committed
add unionnull
1 parent 9b0a4e5 commit 7aa8739

3 files changed

Lines changed: 101 additions & 17 deletions

File tree

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,19 @@ $param->schema();
120120

121121
### Core types
122122

123-
| Type | Helper | Attribute | Description |
124-
| --------------------- | ---------- | ----------- | ----------------------------------------- |
125-
| [String](#string) | `string` | `_string` | String, optionally matching a regex |
126-
| [Int](#int) | `int` | `_int` | Integer with optional range/accept/reject |
127-
| [Float](#float) | `float` | `_float` | Float with optional range/accept/reject |
128-
| [Bool](#bool) | `bool` | `_bool` | Boolean |
129-
| [Null](#null) | `null` | `_null` | Null |
130-
| [Object](#object) | `object` || Object of a given class |
131-
| [Mixed](#mixed) | `mixed` | `_mixed` | Any type |
132-
| [Array](#array) | `arrayp` | `_arrayp` | Array with named parameters |
133-
| [Iterable](#iterable) | `iterable` | `_iterable` | Iterable with generic key/value |
134-
| [Union](#union) | `union` | `_union` | Value matching at least one parameter |
123+
| Type | Helper | Attribute | Description |
124+
| ------------------------ | ----------- | ------------ | --------------------------------------------- |
125+
| [String](#string) | `string` | `_string` | String, optionally matching a regex |
126+
| [Int](#int) | `int` | `_int` | Integer with optional range/accept/reject |
127+
| [Float](#float) | `float` | `_float` | Float with optional range/accept/reject |
128+
| [Bool](#bool) | `bool` | `_bool` | Boolean |
129+
| [Null](#null) | `null` | `_null` | Null |
130+
| [Object](#object) | `object` || Object of a given class |
131+
| [Mixed](#mixed) | `mixed` | `_mixed` | Any type |
132+
| [Array](#array) | `arrayp` | `_arrayp` | Array with named parameters |
133+
| [Iterable](#iterable) | `iterable` | `_iterable` | Iterable with generic key/value |
134+
| [Union](#union) | `union` | `_union` | Value matching at least one parameter |
135+
| [UnionNull](#union-null) | `unionNull` | `_unionNull` | Value matching at least one parameter or null |
135136

136137
### Derived types
137138

@@ -522,6 +523,8 @@ $union('100'); // '100'
522523
$union(100); // 100
523524
```
524525

526+
## UnionNull
527+
525528
You can also use `unionNull()` as a shorthand for nullable unions:
526529

527530
```php
@@ -536,13 +539,11 @@ $maybeInt(null); // null
536539
Attribute notation:
537540

538541
```php
539-
use Chevere\Parameter\Attributes\_union;
540-
use Chevere\Parameter\Attributes\_float;
542+
use Chevere\Parameter\Attributes\_null;
541543
use Chevere\Parameter\Attributes\_int;
542544

543-
#[_union(
544-
new _int(),
545-
new _float()
545+
#[_unionNull(
546+
new _int()
546547
)]
547548
```
548549

src/Attributes/_unionNull.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Chevere.
5+
*
6+
* (c) Rodolfo Berrios <rodolfo@chevere.org>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Chevere\Parameter\Attributes;
15+
16+
use Attribute;
17+
use Chevere\Parameter\Interfaces\ParameterAttributeInterface;
18+
use Chevere\Parameter\Interfaces\UnionParameterInterface;
19+
use Chevere\Parameter\Parameters;
20+
use Chevere\Parameter\Traits\AttrTrait;
21+
use Chevere\Parameter\UnionParameter;
22+
23+
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::TARGET_CLASS_CONSTANT)]
24+
class _unionNull implements ParameterAttributeInterface
25+
{
26+
use AttrTrait;
27+
28+
private UnionParameterInterface $parameter;
29+
30+
public function __construct(
31+
ParameterAttributeInterface ...$parameterAttribute,
32+
) {
33+
$parameters = new Parameters();
34+
array_unshift($parameterAttribute, new _null());
35+
foreach ($parameterAttribute as $name => $attribute) {
36+
$name = (string) $name;
37+
$parameters = $parameters
38+
->withRequired($name, $attribute->parameter());
39+
}
40+
$this->parameter = new UnionParameter($parameters);
41+
}
42+
43+
public function __invoke(mixed $mixed): mixed
44+
{
45+
return $this->parameter->__invoke($mixed);
46+
}
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Chevere.
5+
*
6+
* (c) Rodolfo Berrios <rodolfo@chevere.org>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Chevere\Tests\Attributes;
15+
16+
use Chevere\Parameter\Attributes\_int;
17+
use Chevere\Parameter\Attributes\_unionNull;
18+
use PHPUnit\Framework\TestCase;
19+
use function Chevere\Parameter\int;
20+
use function Chevere\Parameter\unionNull;
21+
22+
final class _unionNullTest extends TestCase
23+
{
24+
public function testConstruct(): void
25+
{
26+
$parameter = unionNull(
27+
int(),
28+
);
29+
$attr = new _unionNull(
30+
new _int(),
31+
);
32+
$this->assertEquals($parameter, $attr->parameter());
33+
$attr->__invoke(null);
34+
$attr->__invoke(1);
35+
}
36+
}

0 commit comments

Comments
 (0)