https://github.com/ghostwriter/option
Provides an Option type implementation for PHP
https://github.com/ghostwriter/option
ghostwriter maybe option option-type php
Last synced: 28 days ago
JSON representation
Provides an Option type implementation for PHP
- Host: GitHub
- URL: https://github.com/ghostwriter/option
- Owner: ghostwriter
- License: other
- Created: 2021-11-23T01:30:14.000Z (over 3 years ago)
- Default Branch: 2.0.x
- Last Pushed: 2025-04-22T00:50:45.000Z (30 days ago)
- Last Synced: 2025-04-22T01:30:08.002Z (30 days ago)
- Topics: ghostwriter, maybe, option, option-type, php
- Language: PHP
- Homepage:
- Size: 1000 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# Option
[](https://github.com/sponsors/ghostwriter)
[](https://github.com/ghostwriter/option/actions/workflows/automation.yml)
[](https://www.php.net/supported-versions)
[](https://packagist.org/packages/ghostwriter/option)Provides an **`Option`** type implementation for PHP.
## Installation
You can install the package via composer:
``` bash
composer require ghostwriter/option
```### Star ⭐️ this repo if you find it useful
You can also star (🌟) this repo to find it easier later.
## Usage
```php
use Ghostwriter\Option\Exception\NullPointerException;
use Ghostwriter\Option\None;
use Ghostwriter\Option\Some;
use Ghostwriter\Option\Option;$message = '#BlackLivesMatter';
Option::new($message); // return `Some`$some = Some::new($message); // return `Some`
echo $some->get(); // prints #BlackLivesMatter$none = None::new(); // return `None`
// calling the `get` method on `None` will throw `NullPointerException`
echo $none->get(); // throw `NullPointerException`echo $none->getOr('#BLM'); // prints #BLM
// calling the `new` static method with `null` will return `None`
Option::new(null); // return `None`// calling the `new` static method with `null will throw `NullPointerException`
Some::new(null); // throws `NullPointerException`// calling the `new` static method with `None will throw `NullPointerException`
Some::new(None::new()); // throws `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
``` bash
composer test
```### Credits
- [Nathanael Esayeas](https://github.com/ghostwriter)
- [All Contributors](https://github.com/ghostwriter/option/contributors)### Changelog
Please see [CHANGELOG.md](./CHANGELOG.md) for more information on what has changed recently.
### License
Please see [LICENSE](./LICENSE) for more information on the license that applies to this project.
### Security
Please see [SECURITY.md](./SECURITY.md) for more information on security disclosure process.