library php-java-optional
Full implementation of Java8 Optional for PHP
serhatozdal/php-java-optional
Full implementation of Java8 Optional for PHP
- Saturday, April 14, 2018
- by serhatozdal
- Repository
- 1 Watchers
- 0 Stars
- 3 Installations
- PHP
- 0 Dependents
- 0 Suggesters
- 0 Forks
- 0 Open issues
- 2 Versions
- 0 % Grown
serhatozdal/php-java-optional
Full implementation of JAVA8 Optional for PHP, (*1)
, (*2)
Usage
// ofEmpty refers Optional#empty() in java
// It is renamed as ofEmpty() because of empty() is reserved by PHP
Optional::ofEmpty()->isPresent(); // false
Optional::of("value")->orElse("elseValue"); // value
Optional::ofEmpty()->orElseThrow(function () { throw .... }); // throws exception
Optional::ofEmpty()->filter(function ($a) { return (int) $a; }); // function is not executed
Optional::of(5)->map(function ($a) { return $a * 2; })->get(); // returns 10
Optional::ofEmpty()->orElseGet(function () { return 10; }); // returns 10
Installation
composer require serhatozdal/php-java-optional:1.0
Resources