prewk / option
Option object for PHP inspired by Rust
Installs: 495 672
Dependents: 2
Suggesters: 0
Security: 0
Stars: 42
Watchers: 4
Forks: 8
Open Issues: 7
Requires
- php: >=8.1.0
- prewk/result: >= 1.2.0
Requires (Dev)
- ergebnis/composer-normalize: 2.42.0
- infection/infection: 0.27.11
- phpunit/phpunit: 10.5.15
- psalm/plugin-phpunit: 0.19.0
- symplify/easy-coding-standard: 12.1.14
- vimeo/psalm: 5.24.0
- dev-master
- 4.0.0
- 3.3.0
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 2.1.1
- 2.1.0
- 2.0.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.1
- dev-dependabot/composer/symplify/easy-coding-standard-12.3.4
- dev-dependabot/github_actions/shivammathur/setup-php-2.31.1
- dev-dependabot/composer/phpunit/phpunit-10.5.26
- dev-dependabot/composer/vimeo/psalm-5.25.0
- dev-dependabot/composer/ergebnis/composer-normalize-2.43.0
- dev-dependabot/github_actions/actions/checkout-4.1.7
- dev-dependabot/github_actions/coverallsapp/github-action-2.3.0
This package is auto-updated.
Last update: 2024-11-02 08:08:33 UTC
README
A PHP implementation of Rust's Option type with roughly the same API.
Version information
Version 4.x.x requires PHP 8.1+. 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