From 02338fb1fa74965fc7f62ba0e700d5e61eb9b9c0 Mon Sep 17 00:00:00 2001 From: Dgame Date: Wed, 4 Apr 2018 21:46:07 +0200 Subject: [PATCH] Added either ... or --- src/Either.php | 41 +++++++++++++++++++++++++++++++++++++++++ src/EnsuranceTrait.php | 10 ++++++++++ tests/EnsuranceTest.php | 16 ++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/Either.php diff --git a/src/Either.php b/src/Either.php new file mode 100644 index 0000000..e3e7085 --- /dev/null +++ b/src/Either.php @@ -0,0 +1,41 @@ +value = $value; + $this->ensured = $ensured; + } + + /** + * @param $value + * + * @return mixed + */ + public function or($value) + { + return $this->ensured ? $this->value : $value; + } +} \ No newline at end of file diff --git a/src/EnsuranceTrait.php b/src/EnsuranceTrait.php index c3cea96..20d5fc9 100644 --- a/src/EnsuranceTrait.php +++ b/src/EnsuranceTrait.php @@ -54,6 +54,16 @@ final public function else($value) return $this->disregardThrowable()->isEnsured() ? $this->value : $value; } + /** + * @param $value + * + * @return Either + */ + final public function either($value): Either + { + return new Either($value, $this->disregardThrowable()->isEnsured()); + } + /** * @param null $default * diff --git a/tests/EnsuranceTest.php b/tests/EnsuranceTest.php index 476d1cc..3ee5429 100644 --- a/tests/EnsuranceTest.php +++ b/tests/EnsuranceTest.php @@ -73,4 +73,20 @@ public function testEnsurance(): void $this->assertFalse(ensure('foo')->isIn(['bar'])->disregardThrowable()->isEnsured()); $this->assertFalse(ensure('foo')->isKeyOf(['foo', 'bar'])->disregardThrowable()->isEnsured()); } + + public function testIsEven(): void +{ + $this->assertEquals('foo', ensure(42)->isEven()->then('foo')); +} + + public function testIsOdd(): void + { + $this->assertEquals(23, ensure(42)->isOdd()->else(23)); + } + + public function tessEither(): void + { + $this->assertTrue(ensure(42)->isOdd()->either(false)->or(true)); + $this->assertFalse(ensure(23)->isOdd()->either(false)->or(true)); + } } \ No newline at end of file