Page MenuHomeDevCentral

D1656.id4230.diff
No OneTemporary

D1656.id4230.diff

diff --git a/src/DataTypes/Result/Err.php b/src/DataTypes/Result/Err.php
new file mode 100644
--- /dev/null
+++ b/src/DataTypes/Result/Err.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Keruald\OmniTools\DataTypes\Result;
+
+use Exception;
+use InvalidArgumentException;
+
+class Err extends Result {
+ /**
+ * @var Exception
+ */
+ private $error;
+
+ public function isOK () {
+ return false;
+ }
+
+ public function isError () {
+ return true;
+ }
+
+ public function getValue () {
+ throw new InvalidArgumentException(<<<'EOD'
+This error construct doesn't have a value. You probably want to check with isOK the value.
+Or if you want an error, use getError.
+EOD
+);
+ }
+
+ public function getError () : Exception {
+ return $this->error;
+ }
+
+ public function setError (Exception $error) : void {
+ $this->error = $error;
+ }
+}
diff --git a/src/DataTypes/Result/Ok.php b/src/DataTypes/Result/Ok.php
new file mode 100644
--- /dev/null
+++ b/src/DataTypes/Result/Ok.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace Keruald\OmniTools\DataTypes\Result;
+
+use InvalidArgumentException;
+
+class Ok extends Result {
+ /**
+ * @var mixed
+ */
+ private $value;
+
+ /**
+ * @var string
+ */
+ private $type;
+
+ public function isOK () {
+ return true;
+ }
+
+ public function isError () {
+ return false;
+ }
+
+ public function getValue () {
+ return $this->value;
+ }
+
+ public function setValue ($value) {
+ $type = self::getTypeOf($value);
+ if (!$this->isAcceptableValueType($type)) {
+ throw new InvalidArgumentException(<<<'EOD'
+When you mutate the value of an Ok object, you can't mutate the object type.
+Please consider return a new Ok instead.
+EOD
+);
+ }
+
+ $this->value = $value;
+ $this->type = $type;
+ }
+
+ private function isAcceptableValueType (string $type) : bool {
+ return $this->value === null || $type === $this->type;
+ }
+
+
+ private static function getTypeOf ($v) {
+ $type = gettype($v);
+
+ if ($type === "object") {
+ return get_class($v);
+ }
+
+ return $type;
+ }
+}
diff --git a/src/DataTypes/Result/Result.php b/src/DataTypes/Result/Result.php
new file mode 100644
--- /dev/null
+++ b/src/DataTypes/Result/Result.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Keruald\OmniTools\DataTypes\Result;
+
+abstract class Result {
+ public abstract function isOK ();
+ public abstract function isError ();
+
+ public abstract function getValue ();
+}
diff --git a/tests/DataTypes/Result/ErrTest.php b/tests/DataTypes/Result/ErrTest.php
new file mode 100644
--- /dev/null
+++ b/tests/DataTypes/Result/ErrTest.php
@@ -0,0 +1,26 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\DataTypes\Result;
+
+use Keruald\OmniTools\DataTypes\Result\Err;
+use PHPUnit\Framework\TestCase;
+
+class ErrTest extends TestCase {
+ public function setUp () {
+ $this->v = new Err;
+ }
+
+ public function testIsOk () : void {
+ $this->AssertFalse($this->v->isOk());
+ }
+
+ public function testIsError () : void {
+ $this->assertTrue($this->v->isError());
+ }
+
+ public function testGetValue () : void {
+ $this->expectException("InvalidArgumentException");
+ $this->v->getValue();
+ }
+}
diff --git a/tests/DataTypes/Result/OkTest.php b/tests/DataTypes/Result/OkTest.php
new file mode 100644
--- /dev/null
+++ b/tests/DataTypes/Result/OkTest.php
@@ -0,0 +1,37 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\DataTypes\Result;
+
+use Keruald\OmniTools\DataTypes\Result\Ok;
+use PHPUnit\Framework\TestCase;
+
+class OkTest extends TestCase {
+
+ public function setUp () {
+ $this->v = new Ok;
+ $this->v->setValue(42);
+ }
+
+ public function testIsOk () : void {
+ $this->AssertTrue($this->v->isOk());
+ }
+
+ public function testIsError () : void {
+ $this->assertFalse($this->v->isError());
+ }
+
+ public function testGetValue () : void {
+ $this->assertEquals(42, $this->v->getValue());
+ }
+
+ public function testSetValue () : void {
+ $this->v->setValue(666);
+ $this->assertEquals(666, $this->v->getValue());
+ }
+
+ public function testSetValueWhenTypeIsMutated () : void {
+ $this->expectException("InvalidArgumentException");
+ $this->v->setValue("Another type");
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 25, 04:50 (17 h, 32 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2261865
Default Alt Text
D1656.id4230.diff (4 KB)

Event Timeline