Skip to content

Commit b9c39c9

Browse files
author
Paul
committed
Added Translate strategy and accompanying tests and documentation.
Updated Travis config to test lowest set of dependencies.
1 parent da266a0 commit b9c39c9

File tree

8 files changed

+139
-4
lines changed

8 files changed

+139
-4
lines changed

.travis.yml

+14-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,26 @@ php:
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+
1426
install:
1527
- alias composer=composer\ -n && composer selfupdate
1628
- composer validate
17-
- composer --prefer-source install
29+
- composer update $DEPENDENCIES
1830

1931
script:
2032
- composer test -- --coverage-clover=build/logs/clover.xml
2133

2234
after_success:
23-
- composer --prefer-source require satooshi/php-coveralls
35+
- composer require satooshi/php-coveralls
2436
- vendor/bin/coveralls -v

README.md

+29
Original file line numberDiff line numberDiff 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) – Merges two data sets together giving precedence to the latter if keys collide.
297298
- [TakeFirst](#takefirst) – Takes the first value from a collection one or more times.
298299
- [ToList](#tolist) – Converts data to a single-element list unless it is already a list.
300+
- [Translate](#translate) – Translates a value using a mapping.
299301
- [TryCatch](#trycatch) – Tries the primary strategy and falls back to an expression if an exception is thrown.
300302
- [Type](#type) – Casts data to the specified type.
301303
- [Unique](#unique) – 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` – Value used to match against an entry in the mapping.
699+
2. `$mapping` – 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

688717
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.

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
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": {

src/DataType.php

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
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
*/
916
final class DataType extends AbstractEnumeration
1017
{

src/Strategy/Translate.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

test/Functional/DocumentationTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ScriptFUSION\Mapper\Strategy\Merge;
1717
use ScriptFUSION\Mapper\Strategy\TakeFirst;
1818
use ScriptFUSION\Mapper\Strategy\ToList;
19+
use ScriptFUSION\Mapper\Strategy\Translate;
1920
use ScriptFUSION\Mapper\Strategy\TryCatch;
2021
use ScriptFUSION\Mapper\Strategy\Type;
2122
use 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 numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

test/MockFactory.php

+11
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@
33

44
use Mockery\MockInterface;
55
use ScriptFUSION\Mapper\Mapper;
6+
use ScriptFUSION\Mapper\Strategy\Strategy;
67
use ScriptFUSION\StaticClass;
78

89
final 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
*

0 commit comments

Comments
 (0)