diff --git a/composer.json b/composer.json index f882dcc..c45c119 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "php": "^8" }, "require-dev": { - "phpunit/phpunit": "^11" + "phpunit/phpunit": "^11", + "phpoption/phpoption": "^1" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index cc4e0a7..5ebc5d5 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/src/Option.php b/src/Option.php index 74960f2..8923d5a 100644 --- a/src/Option.php +++ b/src/Option.php @@ -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), }; } diff --git a/tests/OptionTest.php b/tests/OptionTest.php index d183932..19c4b7f 100644 --- a/tests/OptionTest.php +++ b/tests/OptionTest.php @@ -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 {