Optional
A port of Java 9's java.util.Optional improved for PHP., (*1)
Some examples
Basic usage
Example request class:, (*2)
class Request
{
private $date;
// ... constructor and getter ...
}
Example code of optional:, (*3)
$date = Optional::ofNullable($request->getDate())
->map(function ($dateString) {
return DateTime::createFromFormat('Y-m-d', $dateString);
})
->orElseGet(function () {
return new \DateTime();
});
echo $date->format('Y-m-d');
Given this request:, (*4)
$request = new Request('1992-10-07');
Program will output 1992-10-07., (*5)
If a null is specified in request:, (*6)
$request = new Request(null);
Then program will output the default value, which in this case would be something like 2016-08-19, (*7)
Instead of returning a default value we could just throw an exception in case no value:, (*8)
$date = Optional::ofNullable($request->getDate())
->map(function ($dateString) {
return DateTime::createFromFormat('Y-m-d', $dateString);
})
->orElseThrow(new \Exception());
In case of:, (*9)
$request = new Request(null);
Program will throw the specified exception, otherwise, will return specified parsed date., (*10)
or
We can chain Optionality:, (*11)
$findInMemory = function () {
...
return Optional::ofNullable(...);
};
$findInDisc = function () {
...
return Optional::ofNullable(...);
};
$findRemotely = function () {
...
return Optional::ofNullable(...);
};
$optional = $findInMemory()
->or($findInDisc)
->or($findRemotely);
Callbacks will only execute if the last optional is empty. Example:, (*12)
$findInMemory = function () {
echo "Searching in memory...\n";
return Optional::empty();
};
$findInDisc = function () {
echo "Searching in disc...\n";
return Optional::of("Awesome");
};
$findRemotely = function () {
echo "Searching remotely...";
return Optional::of("Not so awesome");
};
$findInMemory()
->or($findInDisc)
->or($findRemotely)
->ifPresent(function ($value) {
echo $value;
});
Will output:, (*13)
Searching in memory...
Searching in disc...
Awesome
filter
$request = new Request('1992-10-07');
$makeDateTime = function ($value) {
return \DateTime::createFromFormat('Y-m-d', $value);
};
$beforeNow = function (\DateTime $date) {
return $date->getTimestamp() > (new \DateTime())->getTimestamp();
};
$date = Optional::ofNullable($request->getDate())
->map($makeDateTime)
->filter($beforeNow)
->orElse(new \DateTime());
echo $date->format('Y-m-d');
Outputs (similar): 2016-08-19, (*14)
With: $request = new Request('2030-01-01'); would output: 2030-01-01, (*15)
With: $request = new Request(null); would output: 2016-08-19, (*16)
ifPresent
Optional::ofNullable($request->getDate())
->ifPresent(function ($value) {
echo "It's present: " . $value;
});
If value, it would output: It's present: 1992-10-07
Otherwise would do nothing., (*17)
ifPresentOrElse
Similar to ifPresent but with an else callback if optional is empty, (*18)
Optional::ofNullable($request->getDate())
->ifPresentOrElse(function ($value) {
echo "It's present: " . $value;
}, function() {
echo 'No value is present';
});
If value, it would output: It's present: 1992-10-07
Otherwise, it would output: No value is present, (*19)
Comparable
Returns true:, (*20)
Optional::of('something')->equals(Optional::of('something'));
Returns false:, (*21)
Optional::of(5)->equals(Optional::of('something'));
Returns true:, (*22)
Optional::empty()->equals(Optional::empty());
Returns false:, (*23)
Optional::empty()->equals(Optional::of(5));
__toString
echo (string) Optional::of('something');
Outputs: Optional[something], (*24)
echo (string) Optional::empty();
Outputs: Optional.empty, (*25)