2017 © Pedro Peláez
 

library optional

image

jimmyoak/optional

  • Friday, October 28, 2016
  • by jimmyoak
  • Repository
  • 2 Watchers
  • 8 Stars
  • 1,582 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 31 % Grown

The README.md

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)

The Versions

28/10 2016

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

util utilities optional jimmy jimmyoak optionals

28/10 2016

2.2.0

2.2.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

util utilities optional jimmy jimmyoak optionals

28/10 2016

2.0.0

2.0.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

util utilities optional jimmy jimmyoak optionals

28/10 2016

2.1.0

2.1.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

util utilities optional jimmy jimmyoak optionals

19/08 2016

1.2.1

1.2.1.0

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

util utilities optional jimmy jimmyoak optionals

19/08 2016

1.2.0

1.2.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

util utilities optional jimmy jimmyoak optionals

08/08 2016

1.1.0

1.1.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

util utilities optional jimmy jimmyoak optionals

08/08 2016

1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

util utilities optional jimmy jimmyoak optionals