Skip to content

Commit b16d28c

Browse files
committed
new constructor: optional()
1 parent 9bba42d commit b16d28c

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
v2.1.0 (2019-11)
22

3+
* New `optional()` constructor for nullable values
34
* Better `wrap...` function names
45

56
v2.0.0 (2019-10-23)

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ $odd1 = new OddNumber(61); // works as expected, $odd1->value() will retur
4141
$odd2 = new OddNumber(40); // throws an InvalidArgumentException
4242
$odd3 = new OddNumber("string"); // throws an InvalidArgumentException
4343
$odd4 = new OddNumber(null); // throws an InvalidArgumentException
44+
45+
$odd5 = OddNumber::optional(33); // works as expected, $odd5->value() will return 33
46+
$nonodd = OddNumber::optional(null); // $nonodd is now null
47+
$odd6 = OddNumber::optional(40); // throws an InvalidArgumentException
4448
```
4549

4650

@@ -95,6 +99,11 @@ of external values without wrapping them in an instance.
9599
Valid values are stored in the new instance, invalid values cause an `InvalidArgumentException` to be thrown.
96100
Other instances of the same class are always considered valid (*re-wrapping*).
97101

102+
* <code>public static function <b>optional</b>($raw\_value): ?static</code>
103+
104+
Same as the default constructor,
105+
but also accepts `null` values (which will be returned unchanged).
106+
98107
* <code>abstract public static function <b>isValid</b>($test\_value): bool</code>
99108

100109
Checks the validity of a raw value.

src/AbstractValue.php

+16
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ public function __construct($rawValue)
113113
}
114114
}
115115

116+
/**
117+
* Same as the default constructor,
118+
* but also accepts `null` values (which will be returned unchanged).
119+
*
120+
* @param $rawValue
121+
* @return static|null
122+
*/
123+
public static function optional($rawValue)
124+
{
125+
if ($rawValue === null) {
126+
return null;
127+
}
128+
129+
return new static($rawValue);
130+
}
131+
116132

117133
/**
118134
* Equality test.

test/10-ValueTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,26 @@ public function testExtendedBuiltinEqualsZero($additional_property, ExtTestWrapp
477477
"Extended wrapper object is considered equal to zero by builtin== !");
478478
}
479479

480+
/**
481+
* @depends testConstructor
482+
* @depends testConstructorWithInstance
483+
* @depends testInvalidInitializer
484+
* @depends testValue
485+
* @depends testRewrap
486+
*/
487+
public function testOptionalConstructor()
488+
{
489+
$tw1 = TestWrapper4::optional('43001');
490+
$this->assertSame('43001', $tw1->value());
491+
492+
$tw2 = TestWrapper4::optional($tw1);
493+
$this->assertSame($tw1->value(), $tw2->value());
494+
495+
$tw3 = TestWrapper4::optional(null);
496+
$this->assertNull($tw3);
497+
498+
$this->expectException(\InvalidArgumentException::class);
499+
TestWrapper4::optional('x1x1x1x1x');
500+
}
501+
480502
}

0 commit comments

Comments
 (0)