Skip to content

Commit fa9d018

Browse files
author
Paul
committed
Added Join strategy and accompanying tests and documentation.
Removed support for PHP 5.6.
1 parent b9c39c9 commit fa9d018

File tree

6 files changed

+88
-3
lines changed

6 files changed

+88
-3
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ sudo: false
66
language: php
77

88
php:
9-
- 5.5
109
- 5.6
1110
- 7.0
1211
- 7.1

README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Contents
3232
1. [Filter](#filter)
3333
1. [Flatten](#flatten)
3434
1. [IfExists](#ifexists)
35+
1. [Join](#join)
3536
1. [Merge](#merge)
3637
1. [TakeFirst](#takefirst)
3738
1. [ToList](#tolist)
@@ -294,6 +295,7 @@ The following strategies ship with Mapper and provide a suite of commonly used f
294295
- [Filter](#filter) – Filters null values or values rejected by the specified callback.
295296
- [Flatten](#flatten) – Moves all nested values to the top level.
296297
- [IfExists](#ifexists) – Delegates to one expression or another depending on whether the specified condition maps to null.
298+
- [Join](#join) – Joins sub-string expressions together with a glue string.
297299
- [Merge](#merge) – Merges two data sets together giving precedence to the latter if keys collide.
298300
- [TakeFirst](#takefirst) – Takes the first value from a collection one or more times.
299301
- [ToList](#tolist) – Converts data to a single-element list unless it is already a list.
@@ -605,6 +607,30 @@ $data = ['foo' => 'foo'];
605607

606608
> false
607609
610+
### Join
611+
612+
Joins sub-string expressions together with a glue string.
613+
614+
#### Signature
615+
616+
```php
617+
Join(string $glue, array ...$expressions)
618+
```
619+
620+
1. `$glue` – Glue.
621+
2. `$expressions` – Sub-string expressions.
622+
623+
#### Example
624+
625+
```php
626+
(new Mapper)->map(
627+
['foo' => 'foo'],
628+
new Join('-', new Copy('foo'), 'bar')
629+
);
630+
```
631+
632+
> 'foo-bar'
633+
608634
### Merge
609635

610636
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)
828854
Requirements
829855
------------
830856

831-
- [PHP 5.5](http://php.net/)
857+
- [PHP 5.6](http://php.net/)
832858
- [Composer](https://getcomposer.org/)
833859

834860
Limitations

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
],
1010
"license": "LGPL-3.0",
1111
"require": {
12-
"php": ">=5.5",
12+
"php": "^5.6|^7",
1313
"scriptfusion/array-walker": "^1",
1414
"eloquent/enumeration": "^5"
1515
},

src/Strategy/Join.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
namespace ScriptFUSION\Mapper\Strategy;
3+
4+
/**
5+
* Joins sub-string expressions together with a glue string.
6+
*/
7+
class Join extends Delegate
8+
{
9+
private $glue;
10+
11+
/**
12+
* Initializes this instance with the specified glue to join the specified sub-strings together.
13+
*
14+
* @param string $glue Glue.
15+
* @param array ...$expressions Sub-string expressions.
16+
*/
17+
public function __construct($glue, ...$expressions)
18+
{
19+
parent::__construct($expressions);
20+
21+
$this->glue = "$glue";
22+
}
23+
24+
public function __invoke($data, $context = null)
25+
{
26+
return implode($this->glue, parent::__invoke($data, $context));
27+
}
28+
}

test/Functional/DocumentationTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ScriptFUSION\Mapper\Strategy\Filter;
1414
use ScriptFUSION\Mapper\Strategy\Flatten;
1515
use ScriptFUSION\Mapper\Strategy\IfExists;
16+
use ScriptFUSION\Mapper\Strategy\Join;
1617
use ScriptFUSION\Mapper\Strategy\Merge;
1718
use ScriptFUSION\Mapper\Strategy\TakeFirst;
1819
use ScriptFUSION\Mapper\Strategy\ToList;
@@ -228,6 +229,17 @@ public function testIfExists()
228229
self::assertFalse((new Mapper)->map($data, new IfExists(new Copy('bar'), true, false)));
229230
}
230231

232+
public function testJoin()
233+
{
234+
self::assertSame(
235+
'foo-bar',
236+
(new Mapper)->map(
237+
['foo' => 'foo'],
238+
new Join('-', new Copy('foo'), 'bar')
239+
)
240+
);
241+
}
242+
231243
public function testMerge()
232244
{
233245
self::assertSame(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace ScriptFUSIONTest\Integration\Mapper\Strategy;
3+
4+
use ScriptFUSION\Mapper\Mapper;
5+
use ScriptFUSION\Mapper\Strategy\Join;
6+
use ScriptFUSION\Mapper\Strategy\Strategy;
7+
8+
final class JoinTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function testJoin()
11+
{
12+
$join = (new Join(
13+
'-',
14+
'foo',
15+
\Mockery::mock(Strategy::class)->shouldReceive('__invoke')->andReturn('bar')->once()->getMock()
16+
))->setMapper(new Mapper);
17+
18+
self::assertSame('foo-bar', $join([]));
19+
}
20+
}

0 commit comments

Comments
 (0)