https://github.com/dgame/php-optional
Rust-like Optional-type for PHP
https://github.com/dgame/php-optional
argument-unpacking optional php
Last synced: 10 months ago
JSON representation
Rust-like Optional-type for PHP
- Host: GitHub
- URL: https://github.com/dgame/php-optional
- Owner: Dgame
- Created: 2016-05-27T10:24:38.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-11-26T11:13:30.000Z (about 6 years ago)
- Last Synced: 2025-02-17T06:35:51.280Z (12 months ago)
- Topics: argument-unpacking, optional, php
- Language: PHP
- Size: 53.7 KB
- Stars: 8
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# php-optional
[](https://scrutinizer-ci.com/g/Dgame/php-optional/?branch=master)
[](https://travis-ci.org/Dgame/php-optional)
Rust-like Optional-type for PHP 7
### Some - a valid Value
```php
$some = some(42);
$this->assertTrue($some->isSome());
$this->assertEquals(42, $some->unwrap());
```
### Some with argument unpacking
```php
$some = some(42);
$this->assertTrue($some->isSome($value));
$this->assertFalse($some->isNone());
$this->assertEquals(42, $value);
```
### None - an invalid value
```php
$none = none();
$this->assertTrue($none->isNone());
$this->assertFalse($none->isSome());
```
### None with argument unpacking
```php
$none = none();
$this->assertTrue($none->isNone());
$this->assertFalse($none->isSome($value));
$this->assertNull($value);
```
### Maybe - decides for you if your value is a `Some` or a `None`
```php
$maybe = maybe(null);
$this->assertTrue($maybe->isNone());
$maybe = maybe(42);
$this->assertTrue($maybe->isSome());
$this->assertEquals(42, $maybe->unwrap());
```
### Ensure that a condition is fulfilled
```php
$result = some(0)->ensure(function($value) {
return $value > 0;
});
$this->assertTrue($result->isNone());
```