diff --git a/.travis.yml b/.travis.yml index 2582b2c..2a3fb2b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,6 @@ sudo: false language: php php: - - 5.5 - 5.6 - 7.0 - 7.1 diff --git a/README.md b/README.md index cb1e875..0b54817 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Contents 1. [Filter](#filter) 1. [Flatten](#flatten) 1. [IfExists](#ifexists) + 1. [Join](#join) 1. [Merge](#merge) 1. [TakeFirst](#takefirst) 1. [ToList](#tolist) @@ -294,6 +295,7 @@ The following strategies ship with Mapper and provide a suite of commonly used f - [Filter](#filter) – Filters null values or values rejected by the specified callback. - [Flatten](#flatten) – Moves all nested values to the top level. - [IfExists](#ifexists) – Delegates to one expression or another depending on whether the specified condition maps to null. + - [Join](#join) – Joins sub-string expressions together with a glue string. - [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. @@ -605,6 +607,30 @@ $data = ['foo' => 'foo']; > false +### Join + +Joins sub-string expressions together with a glue string. + +#### Signature + +```php +Join(string $glue, array ...$expressions) +``` + + 1. `$glue` – Glue. + 2. `$expressions` – Sub-string expressions. + +#### Example + +```php +(new Mapper)->map( + ['foo' => 'foo'], + new Join('-', new Copy('foo'), 'bar') +); +``` + +> 'foo-bar' + ### Merge Merges two data sets together giving precedence to the latter if string keys collide; integer keys never collide. For more information see [array_merge](http://php.net/manual/en/function.array-merge.php). @@ -828,7 +854,7 @@ Walk(Strategy|Mapping|array|mixed $expression, array|string $path) Requirements ------------ - - [PHP 5.5](http://php.net/) + - [PHP 5.6](http://php.net/) - [Composer](https://getcomposer.org/) Limitations diff --git a/composer.json b/composer.json index 16e2a7e..30e6f6e 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ ], "license": "LGPL-3.0", "require": { - "php": ">=5.5", + "php": "^5.6|^7", "scriptfusion/array-walker": "^1", "eloquent/enumeration": "^5" }, diff --git a/src/Strategy/Join.php b/src/Strategy/Join.php new file mode 100644 index 0000000..228ad9e --- /dev/null +++ b/src/Strategy/Join.php @@ -0,0 +1,28 @@ +glue = "$glue"; + } + + public function __invoke($data, $context = null) + { + return implode($this->glue, parent::__invoke($data, $context)); + } +} diff --git a/test/Functional/DocumentationTest.php b/test/Functional/DocumentationTest.php index ec64f34..8d456c6 100644 --- a/test/Functional/DocumentationTest.php +++ b/test/Functional/DocumentationTest.php @@ -13,6 +13,7 @@ use ScriptFUSION\Mapper\Strategy\Filter; use ScriptFUSION\Mapper\Strategy\Flatten; use ScriptFUSION\Mapper\Strategy\IfExists; +use ScriptFUSION\Mapper\Strategy\Join; use ScriptFUSION\Mapper\Strategy\Merge; use ScriptFUSION\Mapper\Strategy\TakeFirst; use ScriptFUSION\Mapper\Strategy\ToList; @@ -228,6 +229,17 @@ public function testIfExists() self::assertFalse((new Mapper)->map($data, new IfExists(new Copy('bar'), true, false))); } + public function testJoin() + { + self::assertSame( + 'foo-bar', + (new Mapper)->map( + ['foo' => 'foo'], + new Join('-', new Copy('foo'), 'bar') + ) + ); + } + public function testMerge() { self::assertSame( diff --git a/test/Integration/Mapper/Strategy/JoinTest.php b/test/Integration/Mapper/Strategy/JoinTest.php new file mode 100644 index 0000000..d5e32e7 --- /dev/null +++ b/test/Integration/Mapper/Strategy/JoinTest.php @@ -0,0 +1,20 @@ +shouldReceive('__invoke')->andReturn('bar')->once()->getMock() + ))->setMapper(new Mapper); + + self::assertSame('foo-bar', $join([])); + } +}