Skip to content

Commit 1b8326e

Browse files
committed
Added support for single expressions returning an array in Join stragegy.
1 parent ddc6a99 commit 1b8326e

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

.travis.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ php:
1010
- 7.0
1111
- 7.1
1212
- 7.2
13+
- 7.3
1314

1415
env:
1516
matrix:
@@ -21,16 +22,16 @@ matrix:
2122

2223
cache:
2324
directories:
24-
- .composer/cache
25+
- vendor
2526

2627
install:
2728
- alias composer=composer\ -n && composer selfupdate
2829
- composer validate
29-
- composer update $DEPENDENCIES
30+
- composer update --no-progress --no-suggest $DEPENDENCIES
3031

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

3435
after_success:
35-
- composer require satooshi/php-coveralls
36-
- vendor/bin/coveralls -v
36+
- composer require php-coveralls/php-coveralls:^2
37+
- vendor/bin/php-coveralls -v

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ $data = ['foo' => 'foo'];
682682
683683
### Join
684684

685-
Joins sub-string expressions together with a glue string.
685+
Joins expressions together with a glue string.
686686

687687
#### Signature
688688

@@ -691,7 +691,7 @@ Join(string $glue, array ...$expressions)
691691
```
692692

693693
1. `$glue` – Glue.
694-
2. `$expressions` – Sub-string expressions.
694+
2. `$expressions` – Expressions to join or a single expression that resolves to an array to join.
695695

696696
#### Example
697697

@@ -704,6 +704,15 @@ Join(string $glue, array ...$expressions)
704704

705705
> 'foo-bar'
706706
707+
```php
708+
(new Mapper)->map(
709+
['foo' => ['bar', 'baz']],
710+
new Join('-', new Copy('foo'))
711+
);
712+
```
713+
714+
> 'bar-baz'
715+
707716
### Merge
708717

709718
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).

src/Strategy/Join.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class Join extends Delegate
99
private $glue;
1010

1111
/**
12-
* Initializes this instance with the specified glue to join the specified sub-strings together.
12+
* Initializes this instance with the specified glue to join the specified expressions together.
1313
*
1414
* @param string $glue Glue.
15-
* @param array ...$expressions Sub-string expressions.
15+
* @param array ...$expressions Expressions to join or a single expression that resolves to an array to join.
1616
*/
1717
public function __construct($glue, ...$expressions)
1818
{
@@ -23,6 +23,13 @@ public function __construct($glue, ...$expressions)
2323

2424
public function __invoke($data, $context = null)
2525
{
26-
return implode($this->glue, parent::__invoke($data, $context));
26+
$pieces = parent::__invoke($data, $context);
27+
28+
if (count($pieces) === 1 && is_array($pieces[0])) {
29+
// Unwrap.
30+
$pieces = $pieces[0];
31+
}
32+
33+
return implode($this->glue, $pieces);
2734
}
2835
}

test/Functional/DocumentationTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,14 @@ public function testJoin()
281281
new Join('-', new Copy('foo'), 'bar')
282282
)
283283
);
284+
285+
self::assertSame(
286+
'bar-baz',
287+
(new Mapper)->map(
288+
['foo' => ['bar', 'baz']],
289+
new Join('-', new Copy('foo'))
290+
)
291+
);
284292
}
285293

286294
public function testMerge()

test/Integration/Mapper/Strategy/JoinTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
<?php
22
namespace ScriptFUSIONTest\Integration\Mapper\Strategy;
33

4+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
45
use ScriptFUSION\Mapper\Mapper;
56
use ScriptFUSION\Mapper\Strategy\Join;
67
use ScriptFUSION\Mapper\Strategy\Strategy;
78

89
final class JoinTest extends \PHPUnit_Framework_TestCase
910
{
11+
use MockeryPHPUnitIntegration;
12+
13+
/**
14+
* Tests that multiple expression are joined.
15+
*/
1016
public function testJoin()
1117
{
1218
$join = (new Join(
@@ -17,4 +23,17 @@ public function testJoin()
1723

1824
self::assertSame('foo-bar', $join([]));
1925
}
26+
27+
/**
28+
* Tests that a single expression evaluating to an array is joined.
29+
*/
30+
public function testJoinArray()
31+
{
32+
$join = (new Join(
33+
'-',
34+
['foo', 'bar']
35+
))->setMapper(new Mapper);
36+
37+
self::assertSame('foo-bar', $join([]));
38+
}
2039
}

0 commit comments

Comments
 (0)