prewk / option
Option object for PHP inspired by Rust
Installs: 389 455
Dependents: 2
Suggesters: 0
Security: 0
Stars: 39
Watchers: 5
Forks: 8
Open Issues: 0
Requires
- php: >=7.3.0
- prewk/result: >= 1.2.0
Requires (Dev)
- friends-of-phpspec/phpspec-code-coverage: ^6.0.0
- guzzlehttp/guzzle: ^7.3.0
- php-coveralls/php-coveralls: ^2.4.3
- phpspec/phpspec: ^7.0.1
- phpunit/php-code-coverage: ^9.2.5
- phpunit/php-file-iterator: ^3.0.5
- vimeo/psalm: ^4.9.3
README
A PHP implementation of Rust's Option type with roughly the same API.
Version information
Version 3.x.x requires PHP 7.3+. Make sure you match the versions for this and the Result library if you use both.
Installation
composer require prewk/option
Usage
use Prewk\Option;
use Prewk\Option\{Some, None};
function findSomething(): Option {
// ...
if ($foundSomething) {
return new Some($thing);
} else {
return new None;
}
}
function findSomethingElse(): Result {
// ...
if ($foundSomething) {
return new Some($thing);
} else {
return new None;
}
}
// Fallback to value
$value = findSomething()->unwrapOr(null);
// Fallback to option and throw an exception if both fail
$value = findSomething()->or(findSomethingElse())->unwrap();
// Throw custom exception on missing thing (None)
$value = findSomething()->expect(new Exception("Oh noes!"));
License
MIT & Apache 2.0