Skip to content

Commit

Permalink
Added support for single expressions returning an array in Join strag…
Browse files Browse the repository at this point in the history
…egy.
  • Loading branch information
Bilge committed Mar 2, 2019
1 parent ddc6a99 commit 1b8326e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ php:
- 7.0
- 7.1
- 7.2
- 7.3

env:
matrix:
Expand All @@ -21,16 +22,16 @@ matrix:

cache:
directories:
- .composer/cache
- vendor

install:
- alias composer=composer\ -n && composer selfupdate
- composer validate
- composer update $DEPENDENCIES
- composer update --no-progress --no-suggest $DEPENDENCIES

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

after_success:
- composer require satooshi/php-coveralls
- vendor/bin/coveralls -v
- composer require php-coveralls/php-coveralls:^2
- vendor/bin/php-coveralls -v
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ $data = ['foo' => 'foo'];
### Join

Joins sub-string expressions together with a glue string.
Joins expressions together with a glue string.

#### Signature

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

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

#### Example

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

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

> 'bar-baz'
### 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).
Expand Down
13 changes: 10 additions & 3 deletions src/Strategy/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class Join extends Delegate
private $glue;

/**
* Initializes this instance with the specified glue to join the specified sub-strings together.
* Initializes this instance with the specified glue to join the specified expressions together.
*
* @param string $glue Glue.
* @param array ...$expressions Sub-string expressions.
* @param array ...$expressions Expressions to join or a single expression that resolves to an array to join.
*/
public function __construct($glue, ...$expressions)
{
Expand All @@ -23,6 +23,13 @@ public function __construct($glue, ...$expressions)

public function __invoke($data, $context = null)
{
return implode($this->glue, parent::__invoke($data, $context));
$pieces = parent::__invoke($data, $context);

if (count($pieces) === 1 && is_array($pieces[0])) {
// Unwrap.
$pieces = $pieces[0];
}

return implode($this->glue, $pieces);
}
}
8 changes: 8 additions & 0 deletions test/Functional/DocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ public function testJoin()
new Join('-', new Copy('foo'), 'bar')
)
);

self::assertSame(
'bar-baz',
(new Mapper)->map(
['foo' => ['bar', 'baz']],
new Join('-', new Copy('foo'))
)
);
}

public function testMerge()
Expand Down
19 changes: 19 additions & 0 deletions test/Integration/Mapper/Strategy/JoinTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?php
namespace ScriptFUSIONTest\Integration\Mapper\Strategy;

use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use ScriptFUSION\Mapper\Mapper;
use ScriptFUSION\Mapper\Strategy\Join;
use ScriptFUSION\Mapper\Strategy\Strategy;

final class JoinTest extends \PHPUnit_Framework_TestCase
{
use MockeryPHPUnitIntegration;

/**
* Tests that multiple expression are joined.
*/
public function testJoin()
{
$join = (new Join(
Expand All @@ -17,4 +23,17 @@ public function testJoin()

self::assertSame('foo-bar', $join([]));
}

/**
* Tests that a single expression evaluating to an array is joined.
*/
public function testJoinArray()
{
$join = (new Join(
'-',
['foo', 'bar']
))->setMapper(new Mapper);

self::assertSame('foo-bar', $join([]));
}
}

0 comments on commit 1b8326e

Please sign in to comment.