File tree 8 files changed +139
-4
lines changed
Integration/Mapper/Strategy
8 files changed +139
-4
lines changed Original file line number Diff line number Diff line change 11
11
- 7.0
12
12
- 7.1
13
13
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
+
14
26
install :
15
27
- alias composer=composer\ -n && composer selfupdate
16
28
- composer validate
17
- - composer --prefer-source install
29
+ - composer update $DEPENDENCIES
18
30
19
31
script :
20
32
- composer test -- --coverage-clover=build/logs/clover.xml
21
33
22
34
after_success :
23
- - composer --prefer-source require satooshi/php-coveralls
35
+ - composer require satooshi/php-coveralls
24
36
- vendor/bin/coveralls -v
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ Contents
35
35
1 . [ Merge] ( #merge )
36
36
1 . [ TakeFirst] ( #takefirst )
37
37
1 . [ ToList] ( #tolist )
38
+ 1 . [ Translate] ( #translate )
38
39
1 . [ TryCatch] ( #trycatch )
39
40
1 . [ Type] ( #type )
40
41
1 . [ Unique] ( #unique )
@@ -296,6 +297,7 @@ The following strategies ship with Mapper and provide a suite of commonly used f
296
297
- [ Merge] ( #merge ) &ndash ; Merges two data sets together giving precedence to the latter if keys collide.
297
298
- [ TakeFirst] ( #takefirst ) &ndash ; Takes the first value from a collection one or more times.
298
299
- [ 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.
299
301
- [ TryCatch] ( #trycatch ) &ndash ; Tries the primary strategy and falls back to an expression if an exception is thrown.
300
302
- [ Type] ( #type ) &ndash ; Casts data to the specified type.
301
303
- [ Unique] ( #unique ) &ndash ; Creates a collection of unique values by removing duplicates.
@@ -683,6 +685,33 @@ ToList(Strategy|Mapping|array|mixed $expression)
683
685
684
686
> [ 'bar']
685
687
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
+
686
715
### TryCatch
687
716
688
717
Tries 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 15
15
},
16
16
"require-dev" : {
17
17
"scriptfusion/static-class" : " ^1" ,
18
- "phpunit/phpunit" : " ^4" ,
19
- "mockery/mockery" : " ^0"
18
+ "phpunit/phpunit" : " ^4.8 " ,
19
+ "mockery/mockery" : " ^0.9.4 "
20
20
},
21
21
"autoload" : {
22
22
"psr-4" : {
Original file line number Diff line number Diff line change 5
5
6
6
/**
7
7
* 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()
8
15
*/
9
16
final class DataType extends AbstractEnumeration
10
17
{
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 16
16
use ScriptFUSION \Mapper \Strategy \Merge ;
17
17
use ScriptFUSION \Mapper \Strategy \TakeFirst ;
18
18
use ScriptFUSION \Mapper \Strategy \ToList ;
19
+ use ScriptFUSION \Mapper \Strategy \Translate ;
19
20
use ScriptFUSION \Mapper \Strategy \TryCatch ;
20
21
use ScriptFUSION \Mapper \Strategy \Type ;
21
22
use ScriptFUSION \Mapper \Strategy \Unique ;
@@ -267,6 +268,20 @@ public function testToList()
267
268
);
268
269
}
269
270
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
+
270
285
public function testTryCatch ()
271
286
{
272
287
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 3
3
4
4
use Mockery \MockInterface ;
5
5
use ScriptFUSION \Mapper \Mapper ;
6
+ use ScriptFUSION \Mapper \Strategy \Strategy ;
6
7
use ScriptFUSION \StaticClass ;
7
8
8
9
final class MockFactory
9
10
{
10
11
use StaticClass;
11
12
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
+
12
23
/**
13
24
* @param mixed $data
14
25
*
You can’t perform that action at this time.
0 commit comments