diff --git a/.travis.yml b/.travis.yml index 31b8b51..c252509 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: php php: + - 7.2 + - 7.3 - 7.4 before_script: diff --git a/composer.json b/composer.json index d9691a3..2199580 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ } ], "require": { - "php": ">=7.4" + "php": ">=7.2" }, "require-dev": { "phpunit/phpunit": "~8.0" diff --git a/readme.md b/readme.md index 2cbddb5..1b5c7e5 100644 --- a/readme.md +++ b/readme.md @@ -21,8 +21,8 @@ It helps you construct conditionals as you speak it object Oriented way. ## Minimum Requirement -- PHP 7.4 -- (Older version compatibility to arrive with upcoming version of this package) +- PHP 7.2 + +- Composer ## Installation @@ -35,7 +35,7 @@ or add to the require object of `composer.json` file with the version number: ```json { "require": { - "omitobisam/conditional": "^1.1" + "omitobisam/conditional": "^1.2" } } ``` @@ -56,6 +56,7 @@ Conditional::if(is_null($data)) ->else(fn() => doThat()); ``` +> PS: You can still use the old `function() { return v; }`, `fn()` is the new short arrow function in PHP 7.4+ See: https://www.php.net/manual/en/functions.arrow.php Conditional also comes with a helper function called `conditional()` and its used like so: @@ -232,3 +233,15 @@ For new feature, checkout with prefix `feature/#issueid` e.g `feature/#100-add-a ## Licence MIT (see LICENCE file) + +## Additional Information + +Be aware that this package is part of a series of "The Proof of Concept". + +See other packages in this series here: + +- https://github.com/transprime-research/piper [A functional PHP pipe in object-oriented way] +- https://github.com/transprime-research/arrayed [A smart PHP array class object-oriented way] +- https://github.com/transprime-research/attempt [A smart PHP try...catch statement] +- https://github.com/omitobi/carbonate [A smart Carbon + Collection package] +- https://github.com/omitobi/laravel-habitue [Jsonable Http Request(er) package with Collections response] diff --git a/src/Conditional.php b/src/Conditional.php index 1b08044..69f8fae 100644 --- a/src/Conditional.php +++ b/src/Conditional.php @@ -12,45 +12,50 @@ */ class Conditional { - private static bool $truthy = false; + private $truthy = false; - private static bool $conditionsExists = false; + private $conditionsExists = false; - private static bool $ifCalled = false; + private $ifCalled = false; - private static bool $thenCalled = false; + private $thenCalled = false; - private static bool $elseCalled = false; + private $elseCalled = false; - private static bool $elseIfCalled = false; + private $elseIfCalled = false; - private static $finalValue; + private $finalValue; - private static $finalValueChanged = null; + private $finalValueChanged = null; public static function if($condition) { - self::setTruthy($condition); + return (new static)->on($condition); + } + + public function on($condition) + { + $this->setTruthy($condition); - self::$conditionsExists = true; + $this->conditionsExists = true; - self::$ifCalled = true; + $this->ifCalled = true; - return new static; + return $this; } - public static function setTruthy($condition) + public function setTruthy($condition) { if (!$condition instanceof Closure) { - self::$truthy = (bool)$condition; + $this->truthy = (bool)$condition; } else { - self::$truthy = (bool)$condition(); + $this->truthy = (bool)$condition(); } } public function else($action) { - if (!self::$thenCalled) { + if (!$this->thenCalled) { throw new InvalidConditionOrderException( 'then() must be called before calling else()' ); @@ -58,9 +63,9 @@ public function else($action) $this->toggleTruthy(); - self::$conditionsExists = true; + $this->conditionsExists = true; - self::$elseCalled = true; + $this->elseCalled = true; return $this->then($action)->value(); } @@ -73,24 +78,26 @@ public function then($action) ); } - if (self::$truthy) { + if ($this->truthy) { if ($this->isExceptionClass($action)) { throw $action; } if (!$this->canBeCalled($action)) { - $action = fn() => $action; + $action = function () use ($action) { + return $action; + }; } - self::$finalValue = $action(); + $this->finalValue = $action(); - self::$finalValueChanged = true; + $this->finalValueChanged = true; } - self::$thenCalled = true; + $this->thenCalled = true; - self::$conditionsExists = false; + $this->conditionsExists = false; return $this; } @@ -104,17 +111,17 @@ public function elseIf($condition) ); } - self::$conditionsExists = true; + $this->conditionsExists = true; - if (self::$truthy) { + if ($this->truthy) { $this->toggleTruthy(); return $this; } - self::setTruthy($condition); + $this->setTruthy($condition); - self::$elseIfCalled = true; + $this->elseIfCalled = true; return $this; } @@ -126,19 +133,19 @@ private function isExceptionClass($action) public function value() { - return self::$finalValue; + return $this->finalValue; } private function allowElseIf() { - return self::$thenCalled && - !self::$conditionsExists && - !self::$elseCalled; + return $this->thenCalled && + !$this->conditionsExists && + !$this->elseCalled; } private function allowThen() { - return self::$conditionsExists && (self::$ifCalled || self::$elseIfCalled); + return $this->conditionsExists && ($this->ifCalled || $this->elseIfCalled); } protected function canBeCalled($value) @@ -151,18 +158,6 @@ protected function canBeCalled($value) private function toggleTruthy() { - self::$truthy = !self::$truthy; - } - - public function __destruct() - { - self::$truthy = false; - self::$conditionsExists = false; - self::$ifCalled = false; - self::$thenCalled = false; - self::$elseCalled = false; - self::$elseIfCalled = false; - self::$finalValue = null; - self::$finalValueChanged = null; + $this->truthy = !$this->truthy; } } diff --git a/tests/ConditionalTest.php b/tests/ConditionalTest.php index 5d81a35..2b71635 100644 --- a/tests/ConditionalTest.php +++ b/tests/ConditionalTest.php @@ -42,8 +42,12 @@ public function testExecutionFollowsConditions() $secondResponse = 2; Conditional::if($firstResponse === $secondResponse) - ->then(fn() => $this->assertEquals($firstResponse, $secondResponse)) - ->else(fn() => $this->assertNotEquals($firstResponse, $secondResponse)); + ->then(function () use ($firstResponse, $secondResponse) { + return $this->assertEquals($firstResponse, $secondResponse); + }) + ->else(function () use ($firstResponse, $secondResponse) { + return $this->assertNotEquals($firstResponse, $secondResponse); + }); } public function testThatValueIsReceived() @@ -62,9 +66,19 @@ public function testThatValueIsReceived() public function testGetFunctionValue() { - $result = Conditional::if(fn() => true) - ->then(fn() => fn() => 'fn 1') - ->else(fn() => fn() => 'fn 2'); + $result = Conditional::if(function () { + return true; + }) + ->then(function () { + return function () { + return 'fn 1'; + }; + }) + ->else(function () { + return function () { + return 'fn 2'; + }; + }); $this->assertInstanceOf(Closure::class, $result); } @@ -181,17 +195,11 @@ public function testElseIfCannotBeChainedWithElseIf() public function testElseIfCanBeCalledMultipleTimesInAnInstance() { $value = Conditional::if(false) - ->then('a') - ->elseIf('b' == 1) - ->then('b') - ->elseIf('b' !== 2) - ->then('2') - ->else(1); $this->assertEquals($value, 2); @@ -212,6 +220,26 @@ public function testConditionalHelperFunctionAcceptsThenAndElseValues() ); } + public function testConditionalTwice() + { + $conditionally = function ($condition) { + $conditional = Conditional::if($condition); + + Conditional::if(func_num_args() === 2) + ->then('1') + ->else('2'); + + return $conditional; + }; + + $this->assertEquals( + 'b', + $conditionally(1 === 2) + ->then('a') + ->else('b') + ); + } + // public function testIfCannotBeCalledAfterElseIf() // { // $this->expectException(InvalidConditionOrderException::class); @@ -236,7 +264,8 @@ private function dd() } } -class Invokable { +class Invokable +{ public function __invoke($value = '') { @@ -244,6 +273,7 @@ public function __invoke($value = '') } } -class TestException extends \Exception { +class TestException extends \Exception +{ } \ No newline at end of file