-
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 Translate strategy and accompanying tests and documentation.
Updated Travis config to test lowest set of dependencies.
- Loading branch information
Paul
committed
Jan 20, 2017
1 parent
da266a0
commit b9c39c9
Showing
8 changed files
with
139 additions
and
4 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
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,35 @@ | ||
<?php | ||
namespace ScriptFUSION\Mapper\Strategy; | ||
|
||
use ScriptFUSION\Mapper\Mapping; | ||
|
||
/** | ||
* Translates a value using a mapping. | ||
*/ | ||
class Translate extends Decorator | ||
{ | ||
private $mapping; | ||
|
||
/** | ||
* Initializes this instance with the specified value and mapping. | ||
* | ||
* @param Strategy $value Value used to match against an entry in the mapping. | ||
* @param Strategy|Mapping|array|mixed $mapping Mapping that specifies what the value may be translated to. | ||
*/ | ||
public function __construct(Strategy $value, $mapping) | ||
{ | ||
parent::__construct($value); | ||
|
||
$this->mapping = $mapping; | ||
} | ||
|
||
public function __invoke($data, $context = null) | ||
{ | ||
$value = parent::__invoke($data, $context); | ||
$mapping = $this->delegate($this->mapping, $data, $context); | ||
|
||
if (is_array($mapping) && array_key_exists($value, $mapping)) { | ||
return $mapping[$value]; | ||
} | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
namespace ScriptFUSIONTest\Integration\Mapper\Strategy; | ||
|
||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use ScriptFUSION\Mapper\Mapper; | ||
use ScriptFUSION\Mapper\Strategy\Translate; | ||
use ScriptFUSIONTest\MockFactory; | ||
|
||
final class TranslateTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
use MockeryPHPUnitIntegration; | ||
|
||
public function testValueFound() | ||
{ | ||
$translate = (new Translate(MockFactory::mockStrategy('foo'), ['foo' => 'bar']))->setMapper(new Mapper); | ||
|
||
self::assertSame('bar', $translate([])); | ||
} | ||
|
||
public function testValueNotFound() | ||
{ | ||
$translate = (new Translate(MockFactory::mockStrategy('foo'), ['bar' => 'bar']))->setMapper(new Mapper); | ||
|
||
self::assertNull($translate([])); | ||
} | ||
} |
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