-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IfElse strategy. Changed IfExists strategy to extend IfElse. Changed Either strategy to extend IfExists.
- Loading branch information
Showing
7 changed files
with
183 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace ScriptFUSION\Mapper; | ||
|
||
/** | ||
* The exception that is thrown when an invalid condition is specified. | ||
*/ | ||
class InvalidConditionException extends \RuntimeException | ||
{ | ||
// Intentionally empty. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
namespace ScriptFUSION\Mapper\Strategy; | ||
|
||
use ScriptFUSION\Mapper\InvalidConditionException; | ||
use ScriptFUSION\Mapper\Mapping; | ||
|
||
/** | ||
* Delegates to one expression or another depending on whether the specified condition strictly evaluates to true. | ||
*/ | ||
class IfElse extends Delegate | ||
{ | ||
/** @var callable */ | ||
private $condition; | ||
|
||
/** @var Strategy|Mapping|array|mixed */ | ||
private $else; | ||
|
||
/** | ||
* Initializes this instance with the specified condition, the specified | ||
* expression to be resolved when condition is true and, optionally, the | ||
* specified expression to be resolved when condition is false. | ||
* | ||
* @param callable $condition Condition. | ||
* @param Strategy|Mapping|array|mixed $if Primary expression. | ||
* @param Strategy|Mapping|array|mixed|null $else Optional. Fallback expression. | ||
*/ | ||
public function __construct(callable $condition, $if, $else = null) | ||
{ | ||
parent::__construct($if); | ||
|
||
$this->condition = $condition; | ||
$this->else = $else; | ||
} | ||
|
||
/** | ||
* Resolves the stored expression when the stored condition strictly | ||
* evaluates to true, otherwise resolve the stored fallback expression. | ||
* | ||
* @param mixed $data | ||
* @param mixed $context | ||
* | ||
* @throws InvalidConditionException | ||
* | ||
* @return mixed | ||
*/ | ||
public function __invoke($data, $context = null) | ||
{ | ||
$result = call_user_func($this->condition, $data, $context); | ||
|
||
if (!is_bool($result)) { | ||
throw new InvalidConditionException('Invalid return from condition: must be of type boolean.'); | ||
} | ||
|
||
if ($result === true) { | ||
return parent::__invoke($data, $context); | ||
} | ||
|
||
return $this->delegate($this->else, $data, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace ScriptFUSIONTest\Integration\Mapper\Strategy; | ||
|
||
use ScriptFUSION\Mapper\InvalidConditionException; | ||
use ScriptFUSION\Mapper\Mapper; | ||
use ScriptFUSION\Mapper\Strategy\IfElse; | ||
|
||
final class IfElseTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $condition; | ||
|
||
public function setUp() | ||
{ | ||
$this->condition = function ($data) { | ||
return array_key_exists('baz', $data) && $data['baz'] === 'qux'; | ||
}; | ||
} | ||
|
||
public function testIfElse() | ||
{ | ||
$ifElse = (new IfElse($this->condition, 'foo', 'bar'))->setMapper(new Mapper); | ||
|
||
self::assertSame('foo', $ifElse(['baz' => 'qux'])); | ||
self::assertSame('bar', $ifElse(['baz' => 'quux'])); | ||
self::assertSame('bar', $ifElse([])); | ||
} | ||
|
||
public function testOnlyIf() | ||
{ | ||
$ifElse = (new IfElse($this->condition, 'foo'))->setMapper(new Mapper); | ||
|
||
self::assertSame('foo', $ifElse(['baz' => 'qux'])); | ||
self::assertNull($ifElse(['baz' => 'quux'])); | ||
self::assertNull($ifElse([])); | ||
} | ||
|
||
public function testStrictness() | ||
{ | ||
$this->setExpectedException(InvalidConditionException::class); | ||
|
||
$ifElse = (new IfElse( | ||
function () { | ||
return 1; | ||
}, | ||
'foo', | ||
'bar' | ||
))->setMapper(new Mapper); | ||
|
||
$ifElse([]); | ||
} | ||
} |