2017 © Pedro Peláez
 

library optional

Optional class for various if-null-else purposes

image

noini/optional

Optional class for various if-null-else purposes

  • Monday, May 14, 2018
  • by noini
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Optional

Optional is multipurpose tool to reduce if-null checking in PHP code., (*1)

Installation

Install via composer, (*2)

composer require noini/optional

Usage

Static create

Optional::create($payload)->then(...);

Function

optional($payload)->then(...);

Simple example

Following example will call callable because $content value is not null., (*3)

$payload = "content string";
$optional = new Optional($payload);
$optional->then(function($data) {
    echo "Had content: " . $data;
})->otherwise(function($data) {
    echo "I had null value";
});

has()

Has() method can be used to check if Optional payload meets requirement(s)., (*4)

Null payload, (*5)

If Optional payload is null, then has() will fail and create false result., (*6)

optional(null)->has(1)->getResult(); // false

Null value can be checked with type if needed, (*7)

optional(null)->has(Noini\Optional\Helpers\Types::NULL)->getResult(); // true

Using strict equals comparison, (*8)

Compares payload to has() parameter., (*9)

optional(50)->has(50)->getResult(); // true
optional("50")->has(50)->getResult(); // false

Using type check, (*10)

Types class constants can be used to check if payload data type meets requirement., (*11)

optional("50")->has(Noini\Optional\Helpers\Types::INTEGER)->getResult(); // false
optional("50")->has(Noini\Optional\Helpers\Types::STRING)->getResult(); // true

Class checking, (*12)

Passing class will check if payload is instance of given class., (*13)

optional(new \stdClass())->has(\stdClass::class)->getResult(); // true;

Using callback, (*14)

Use callback for customized payload checking. Callback must return boolean., (*15)

optional(50)->has(function($data) {
    return $data > 10;
})->then(function($data) {
    echo "Value is over 10";
});

then()

Callback function will be called if new instance of Optional has non null value:, (*16)

optional(1)->then(function($data) {
    echo "I had non null value: " . $data;
});

Then() can be invoked in constructor by providing callable., (*17)

optional(50, function($data) {
    echo "Having non null value: " . $data;
});

Chained with has() method then() will call callable only if latest has() comparison was successful., (*18)

optional(50)
    ->has(Types::INTEGER)
    ->then(function($data) {
        echo "Data is a integer";
    });

otherwise()

Otherwise callback will be called if latest has comparison fails., (*19)

optional(50)
    ->has(function($data) {
        return $data > 9000;
    })->otherwise(function($data) {
        echo "Data was not over 9000";
    });

if-else

Has-then method chaining can also be done with if() -method. If() should contain both condition and callback function with one method:, (*20)

optional("data")->if(Types::STRING, function($data) {
    echo "I had a string value: " . $data;
});

Use else() method chained with if(), (*21)

optional(null)
    ->if(Types::STRING, function($data) {
        echo "I should not be called this time";
    })
    ->else(function($data) {
        echo "This means optional payload was not a string";
    });

Notice has-then cannot ne used after if., (*22)

The Versions

14/05 2018

dev-master

9999999-dev

Optional class for various if-null-else purposes

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Jouni Jouppila

14/05 2018

1.1.0

1.1.0.0

Optional class for various if-null-else purposes

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Jouni Jouppila

12/05 2018

1.0.0

1.0.0.0

Optional class for various if-null-else purposes

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Jouni Jouppila