Skip to content
Permalink
Browse files

Added support for single expressions returning an array in Join strag…

…egy.
  • Loading branch information
Bilge committed Mar 2, 2019
1 parent ddc6a99 commit 1b8326e238876db9e5f86533ef924f003316a433
Showing with 53 additions and 9 deletions.
  1. +5 −4 .travis.yml
  2. +11 −2 README.md
  3. +10 −3 src/Strategy/Join.php
  4. +8 −0 test/Functional/DocumentationTest.php
  5. +19 −0 test/Integration/Mapper/Strategy/JoinTest.php
@@ -10,6 +10,7 @@ php:
- 7.0
- 7.1
- 7.2
- 7.3

env:
matrix:
@@ -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
@@ -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

@@ -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

@@ -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).
@@ -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)
{
@@ -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);
}
}
@@ -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()
@@ -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(
@@ -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.
You can’t perform that action at this time.