Add PhpOption support to Option::of

This commit is contained in:
Daniel J. Summers 2024-07-27 21:20:35 -04:00
parent 7837f4af17
commit 7d25b9ea28
4 changed files with 96 additions and 4 deletions

View File

@ -20,7 +20,8 @@
"php": "^8"
},
"require-dev": {
"phpunit/phpunit": "^11"
"phpunit/phpunit": "^11",
"phpoption/phpoption": "^1"
},
"autoload": {
"psr-4": {

77
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "8b1b205cce046bd609215dd01d4ab9b7",
"content-hash": "9c9d70d95d369f37fa95a10637a56a58",
"packages": [],
"packages-dev": [
{
@ -243,6 +243,81 @@
},
"time": "2022-02-21T01:04:05+00:00"
},
{
"name": "phpoption/phpoption",
"version": "1.9.3",
"source": {
"type": "git",
"url": "https://github.com/schmittjoh/php-option.git",
"reference": "e3fac8b24f56113f7cb96af14958c0dd16330f54"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/schmittjoh/php-option/zipball/e3fac8b24f56113f7cb96af14958c0dd16330f54",
"reference": "e3fac8b24f56113f7cb96af14958c0dd16330f54",
"shasum": ""
},
"require": {
"php": "^7.2.5 || ^8.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.2",
"phpunit/phpunit": "^8.5.39 || ^9.6.20 || ^10.5.28"
},
"type": "library",
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": false
},
"branch-alias": {
"dev-master": "1.9-dev"
}
},
"autoload": {
"psr-4": {
"PhpOption\\": "src/PhpOption/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Johannes M. Schmitt",
"email": "schmittjoh@gmail.com",
"homepage": "https://github.com/schmittjoh"
},
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
}
],
"description": "Option Type for PHP",
"keywords": [
"language",
"option",
"php",
"type"
],
"support": {
"issues": "https://github.com/schmittjoh/php-option/issues",
"source": "https://github.com/schmittjoh/php-option/tree/1.9.3"
},
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/phpoption/phpoption",
"type": "tidelift"
}
],
"time": "2024-07-20T21:41:07+00:00"
},
{
"name": "phpunit/php-code-coverage",
"version": "11.0.5",

View File

@ -82,8 +82,8 @@ readonly class Option
public static function of(mixed $value): self
{
return match (true) {
// TODO: can we do this check without requiring this package?
// $value instanceof PhpOption => $value->isDefined() ? self::Some($value->get()) : self::None(),
is_object($value) && is_a($value, 'PhpOption\Option') =>
$value->isDefined() ? self::Some($value->get()) : self::None(),
default => new self($value),
};
}

View File

@ -10,6 +10,7 @@ namespace Test;
use BitBadger\InspiredByFSharp\Option;
use InvalidArgumentException;
use PhpOption\{None, Some};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@ -64,6 +65,21 @@ class OptionTest extends TestCase
$this->assertEquals('test', $it->get(), 'The value was not assigned correctly');
}
#[TestDox('Of succeeds with PhpOption\Some')]
public function testOfSucceedsWithPhpOptionSome(): void
{
$it = Option::of(Some::create('something'));
$this->assertTrue(Option::isSome($it), 'A "Some" PhpOption should have created a "Some" option');
$this->assertEquals('something', $it->get(), 'The value was not assigned correctly');
}
#[TestDox('Of succeeds with PhpOption\None')]
public function testOfSucceedsWithPhpOptionNone(): void
{
$it = Option::of(None::create());
$this->assertTrue(Option::isNone($it), 'A "None" PhpOption should have created a "None" option');
}
#[TestDox('IsNone succeeds with None')]
public function testIsNoneSucceedsWithNone(): void
{