ghostwriter/option

Provides an Option type implementation for PHP

Fund package maintenance!
ghostwriter

Installs: 2 351 408

Dependents: 11

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 1

Open Issues: 0

1.5.1 2023-07-05 19:51 UTC

README

Automation Supported PHP Version Mutation Coverage Code Coverage Type Coverage Latest Version on Packagist Downloads

Provides an Option type implementation for PHP.

Installation

You can install the package via composer:

composer require ghostwriter/option

Star ⭐️ this repo if you find it useful

You can also star (🌟) this repo to find it easier later.

Usage

use Ghostwriter\Option\Exception\NullPointerException;
use Ghostwriter\Option\None;
use Ghostwriter\Option\Some;

$greeting = Some::new('Hello World!');
echo $greeting->get();        // 'Hello World!'


$name = None::new();
echo $name->get();                  // throw `NullPointerException`
echo $name->getOr('Default Value'); // 'Default Value'

None::new();            // return `None`
Some::nullable(null);   // return `None`
Some::new(null);        // throw `NullPointerException`

--- Example

function divide(int $x, int $y): OptionInterface
{
    if ($y === 0) {
        return None::new();
    }

    return Some::new($x / $y);
}

divide(1, 0); // None
divide(1, 1); // Some(1)

Testing

composer test

Credits

Changelog

Please see CHANGELOG.md for more information on what has changed recently.

License

Please see LICENSE for more information on the license that applies to this project.

Security

Please see SECURITY.md for more information on security disclosure process.