File tree Expand file tree Collapse file tree 8 files changed +139
-4
lines changed
Integration/Mapper/Strategy Expand file tree Collapse file tree 8 files changed +139
-4
lines changed Original file line number Diff line number Diff line change 1111 - 7.0
1212 - 7.1
1313
14+ env :
15+ matrix :
16+ -
17+ - DEPENDENCIES=--prefer-lowest
18+
19+ matrix :
20+ fast_finish : true
21+
22+ cache :
23+ directories :
24+ - .composer/cache
25+
1426install :
1527 - alias composer=composer\ -n && composer selfupdate
1628 - composer validate
17- - composer --prefer-source install
29+ - composer update $DEPENDENCIES
1830
1931script :
2032 - composer test -- --coverage-clover=build/logs/clover.xml
2133
2234after_success :
23- - composer --prefer-source require satooshi/php-coveralls
35+ - composer require satooshi/php-coveralls
2436 - vendor/bin/coveralls -v
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ Contents
3535 1 . [ Merge] ( #merge )
3636 1 . [ TakeFirst] ( #takefirst )
3737 1 . [ ToList] ( #tolist )
38+ 1 . [ Translate] ( #translate )
3839 1 . [ TryCatch] ( #trycatch )
3940 1 . [ Type] ( #type )
4041 1 . [ Unique] ( #unique )
@@ -296,6 +297,7 @@ The following strategies ship with Mapper and provide a suite of commonly used f
296297 - [ Merge] ( #merge ) &ndash ; Merges two data sets together giving precedence to the latter if keys collide.
297298 - [ TakeFirst] ( #takefirst ) &ndash ; Takes the first value from a collection one or more times.
298299 - [ ToList] ( #tolist ) &ndash ; Converts data to a single-element list unless it is already a list.
300+ - [ Translate] ( #translate ) &ndash ; Translates a value using a mapping.
299301 - [ TryCatch] ( #trycatch ) &ndash ; Tries the primary strategy and falls back to an expression if an exception is thrown.
300302 - [ Type] ( #type ) &ndash ; Casts data to the specified type.
301303 - [ Unique] ( #unique ) &ndash ; Creates a collection of unique values by removing duplicates.
@@ -683,6 +685,33 @@ ToList(Strategy|Mapping|array|mixed $expression)
683685
684686> [ 'bar']
685687
688+ ### Translate
689+
690+ Translates a value using a mapping. The mapping may derived from any valid expression.
691+
692+ #### Signature
693+
694+ ``` php
695+ Translate(Strategy $value, Strategy|Mapping|array|mixed $mapping)
696+ ```
697+
698+ 1 . ` $value ` &ndash ; Value used to match against an entry in the mapping.
699+ 2 . ` $mapping ` &ndash ; Mapping that specifies what the value may be translated to.
700+
701+ #### Example
702+
703+ ``` php
704+ (new Mapper)->map(
705+ ['foo' => 'foo'],
706+ new Translate(
707+ new Copy('foo'),
708+ ['foo' => 'bar']
709+ )
710+ );
711+ ```
712+
713+ > 'bar'
714+
686715### TryCatch
687716
688717Tries the primary strategy and falls back to an expression if an exception is thrown. The thrown exception is passed to the specified exception handler. The handler should throw an exception if it does not expect the exception type it receives.
Original file line number Diff line number Diff line change 1515 },
1616 "require-dev" : {
1717 "scriptfusion/static-class" : " ^1" ,
18- "phpunit/phpunit" : " ^4" ,
19- "mockery/mockery" : " ^0"
18+ "phpunit/phpunit" : " ^4.8 " ,
19+ "mockery/mockery" : " ^0.9.4 "
2020 },
2121 "autoload" : {
2222 "psr-4" : {
Original file line number Diff line number Diff line change 55
66/**
77 * Specifies a PHP data type.
8+ *
9+ * @method static BOOLEAN()
10+ * @method static INTEGER()
11+ * @method static FLOAT()
12+ * @method static STRING()
13+ * @method static MAP()
14+ * @method static OBJECT()
815 */
916final class DataType extends AbstractEnumeration
1017{
Original file line number Diff line number Diff line change 1+ <?php
2+ namespace ScriptFUSION \Mapper \Strategy ;
3+
4+ use ScriptFUSION \Mapper \Mapping ;
5+
6+ /**
7+ * Translates a value using a mapping.
8+ */
9+ class Translate extends Decorator
10+ {
11+ private $ mapping ;
12+
13+ /**
14+ * Initializes this instance with the specified value and mapping.
15+ *
16+ * @param Strategy $value Value used to match against an entry in the mapping.
17+ * @param Strategy|Mapping|array|mixed $mapping Mapping that specifies what the value may be translated to.
18+ */
19+ public function __construct (Strategy $ value , $ mapping )
20+ {
21+ parent ::__construct ($ value );
22+
23+ $ this ->mapping = $ mapping ;
24+ }
25+
26+ public function __invoke ($ data , $ context = null )
27+ {
28+ $ value = parent ::__invoke ($ data , $ context );
29+ $ mapping = $ this ->delegate ($ this ->mapping , $ data , $ context );
30+
31+ if (is_array ($ mapping ) && array_key_exists ($ value , $ mapping )) {
32+ return $ mapping [$ value ];
33+ }
34+ }
35+ }
Original file line number Diff line number Diff line change 1616use ScriptFUSION \Mapper \Strategy \Merge ;
1717use ScriptFUSION \Mapper \Strategy \TakeFirst ;
1818use ScriptFUSION \Mapper \Strategy \ToList ;
19+ use ScriptFUSION \Mapper \Strategy \Translate ;
1920use ScriptFUSION \Mapper \Strategy \TryCatch ;
2021use ScriptFUSION \Mapper \Strategy \Type ;
2122use ScriptFUSION \Mapper \Strategy \Unique ;
@@ -267,6 +268,20 @@ public function testToList()
267268 );
268269 }
269270
271+ public function testTranslate ()
272+ {
273+ self ::assertSame (
274+ 'bar ' ,
275+ (new Mapper )->map (
276+ ['foo ' => 'foo ' ],
277+ new Translate (
278+ new Copy ('foo ' ),
279+ ['foo ' => 'bar ' ]
280+ )
281+ )
282+ );
283+ }
284+
270285 public function testTryCatch ()
271286 {
272287 self ::assertSame (
Original file line number Diff line number Diff line change 1+ <?php
2+ namespace ScriptFUSIONTest \Integration \Mapper \Strategy ;
3+
4+ use Mockery \Adapter \Phpunit \MockeryPHPUnitIntegration ;
5+ use ScriptFUSION \Mapper \Mapper ;
6+ use ScriptFUSION \Mapper \Strategy \Translate ;
7+ use ScriptFUSIONTest \MockFactory ;
8+
9+ final class TranslateTest extends \PHPUnit_Framework_TestCase
10+ {
11+ use MockeryPHPUnitIntegration;
12+
13+ public function testValueFound ()
14+ {
15+ $ translate = (new Translate (MockFactory::mockStrategy ('foo ' ), ['foo ' => 'bar ' ]))->setMapper (new Mapper );
16+
17+ self ::assertSame ('bar ' , $ translate ([]));
18+ }
19+
20+ public function testValueNotFound ()
21+ {
22+ $ translate = (new Translate (MockFactory::mockStrategy ('foo ' ), ['bar ' => 'bar ' ]))->setMapper (new Mapper );
23+
24+ self ::assertNull ($ translate ([]));
25+ }
26+ }
Original file line number Diff line number Diff line change 33
44use Mockery \MockInterface ;
55use ScriptFUSION \Mapper \Mapper ;
6+ use ScriptFUSION \Mapper \Strategy \Strategy ;
67use ScriptFUSION \StaticClass ;
78
89final class MockFactory
910{
1011 use StaticClass;
1112
13+ /**
14+ * @param mixed $data
15+ *
16+ * @return Strategy|MockInterface
17+ */
18+ public static function mockStrategy ($ data )
19+ {
20+ return \Mockery::mock (Strategy::class)->shouldReceive ('__invoke ' )->andReturn ($ data )->getMock ();
21+ }
22+
1223 /**
1324 * @param mixed $data
1425 *
You can’t perform that action at this time.
0 commit comments