A library to reduce the code required for null-checking.
PHP
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
source ... Nov 2, 2014
tests ... Nov 2, 2014
.editorconfig
.gitignore Minor changes. Nov 2, 2014
.scrutinizer.yml Minor changes. Nov 2, 2014
.travis.yml ... Nov 2, 2014
code-of-conduct.md Added contribution, code of conduct and updated license files Aug 10, 2015
composer.json ... Nov 2, 2014
composer.lock
contributing.md Added contribution, code of conduct and updated license files Aug 10, 2015
license.md
phpunit.xml First. Aug 28, 2014
readme.md Updated readme. Nov 2, 2014

readme.md

Optional

Build Status Code Quality Code Coverage Version License

This library is inspired by the code of Johannes Schmitt and the writing of Igor Wiedler. It is intended to reduce the code required for null-checking. Part of TypedPHP.

Examples

<?php

require("vendor/autoload.php");

use TypedPHP\Optional\Optional;

class Foo
{
    public function hello()
    {
        return new Bar();
    }
}

class Bar
{
    public function world()
    {
        return "hello world";
    }
}

$optional = new Optional(new Foo());
$optional->hello()->world()->value(); // "hello world"
<?php

require("vendor/autoload.php");

use TypedPHP\Optional\None;

$none = new None();
$none->hello()->world()->value(); // null
<?php

require("vendor/autoload.php");

use TypedPHP\Optional\None;

$none = new None();
$none
    ->none(function() { print "none"; }); // "none" printed
    ->value(function($value) { print $value; }); // $value not printed

use TypedPHP\Optional\Optional;

$optional = new Optional("hello world");
$optional
    ->none(function() { print "none"; }); // "none" not printed
    ->value(function($value) { print $value; }); // "hello world" printed

Once an Optional method call returns an empty value, it is transformed into a None.

Installation

❯ composer require "typedphp/optional:*"

Testing

❯ composer create-project "typedphp/optional:*" .
❯ vendor/bin/phpunit