Skip to content

Commit

Permalink
Added Translate strategy and accompanying tests and documentation.
Browse files Browse the repository at this point in the history
Updated Travis config to test lowest set of dependencies.
  • Loading branch information
Paul committed Jan 20, 2017
1 parent da266a0 commit b9c39c9
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 4 deletions.
16 changes: 14 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,26 @@ php:
- 7.0
- 7.1

env:
matrix:
-
- DEPENDENCIES=--prefer-lowest

matrix:
fast_finish: true

cache:
directories:
- .composer/cache

install:
- alias composer=composer\ -n && composer selfupdate
- composer validate
- composer --prefer-source install
- composer update $DEPENDENCIES

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

after_success:
- composer --prefer-source require satooshi/php-coveralls
- composer require satooshi/php-coveralls
- vendor/bin/coveralls -v
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Contents
1. [Merge](#merge)
1. [TakeFirst](#takefirst)
1. [ToList](#tolist)
1. [Translate](#translate)
1. [TryCatch](#trycatch)
1. [Type](#type)
1. [Unique](#unique)
Expand Down Expand Up @@ -296,6 +297,7 @@ The following strategies ship with Mapper and provide a suite of commonly used f
- [Merge](#merge) – Merges two data sets together giving precedence to the latter if keys collide.
- [TakeFirst](#takefirst) – Takes the first value from a collection one or more times.
- [ToList](#tolist) – Converts data to a single-element list unless it is already a list.
- [Translate](#translate) – Translates a value using a mapping.
- [TryCatch](#trycatch) – Tries the primary strategy and falls back to an expression if an exception is thrown.
- [Type](#type) – Casts data to the specified type.
- [Unique](#unique) – Creates a collection of unique values by removing duplicates.
Expand Down Expand Up @@ -683,6 +685,33 @@ ToList(Strategy|Mapping|array|mixed $expression)

> ['bar']
### Translate

Translates a value using a mapping. The mapping may derived from any valid expression.

#### Signature

```php
Translate(Strategy $value, Strategy|Mapping|array|mixed $mapping)
```

1. `$value` – Value used to match against an entry in the mapping.
2. `$mapping` – Mapping that specifies what the value may be translated to.

#### Example

```php
(new Mapper)->map(
['foo' => 'foo'],
new Translate(
new Copy('foo'),
['foo' => 'bar']
)
);
```

> 'bar'
### TryCatch

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.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
},
"require-dev": {
"scriptfusion/static-class": "^1",
"phpunit/phpunit": "^4",
"mockery/mockery": "^0"
"phpunit/phpunit": "^4.8",
"mockery/mockery": "^0.9.4"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions src/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

/**
* Specifies a PHP data type.
*
* @method static BOOLEAN()
* @method static INTEGER()
* @method static FLOAT()
* @method static STRING()
* @method static MAP()
* @method static OBJECT()
*/
final class DataType extends AbstractEnumeration
{
Expand Down
35 changes: 35 additions & 0 deletions src/Strategy/Translate.php
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];
}
}
}
15 changes: 15 additions & 0 deletions test/Functional/DocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ScriptFUSION\Mapper\Strategy\Merge;
use ScriptFUSION\Mapper\Strategy\TakeFirst;
use ScriptFUSION\Mapper\Strategy\ToList;
use ScriptFUSION\Mapper\Strategy\Translate;
use ScriptFUSION\Mapper\Strategy\TryCatch;
use ScriptFUSION\Mapper\Strategy\Type;
use ScriptFUSION\Mapper\Strategy\Unique;
Expand Down Expand Up @@ -267,6 +268,20 @@ public function testToList()
);
}

public function testTranslate()
{
self::assertSame(
'bar',
(new Mapper)->map(
['foo' => 'foo'],
new Translate(
new Copy('foo'),
['foo' => 'bar']
)
)
);
}

public function testTryCatch()
{
self::assertSame(
Expand Down
26 changes: 26 additions & 0 deletions test/Integration/Mapper/Strategy/TranslateTest.php
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([]));
}
}
11 changes: 11 additions & 0 deletions test/MockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@

use Mockery\MockInterface;
use ScriptFUSION\Mapper\Mapper;
use ScriptFUSION\Mapper\Strategy\Strategy;
use ScriptFUSION\StaticClass;

final class MockFactory
{
use StaticClass;

/**
* @param mixed $data
*
* @return Strategy|MockInterface
*/
public static function mockStrategy($data)
{
return \Mockery::mock(Strategy::class)->shouldReceive('__invoke')->andReturn($data)->getMock();
}

/**
* @param mixed $data
*
Expand Down

0 comments on commit b9c39c9

Please sign in to comment.