serhatozdal/php-java-optional

Full implementation of Java8 Optional for PHP

1.0 2018-04-14 19:31 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:09:19 UTC


README

Full implementation of JAVA8 Optional for PHP

Build Status Latest Stable Version Latest Unstable Version GitHub license

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