Skip to content

Commit

Permalink
Added Join strategy and accompanying tests and documentation.
Browse files Browse the repository at this point in the history
Removed support for PHP 5.6.
  • Loading branch information
Paul committed Jan 24, 2017
1 parent b9c39c9 commit fa9d018
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ sudo: false
language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1
Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"license": "LGPL-3.0",
"require": {
"php": ">=5.5",
"php": "^5.6|^7",
"scriptfusion/array-walker": "^1",
"eloquent/enumeration": "^5"
},
Expand Down
28 changes: 28 additions & 0 deletions src/Strategy/Join.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace ScriptFUSION\Mapper\Strategy;

/**
* Joins sub-string expressions together with a glue string.
*/
class Join extends Delegate
{
private $glue;

/**
* Initializes this instance with the specified glue to join the specified sub-strings together.
*
* @param string $glue Glue.
* @param array ...$expressions Sub-string expressions.
*/
public function __construct($glue, ...$expressions)
{
parent::__construct($expressions);

$this->glue = "$glue";
}

public function __invoke($data, $context = null)
{
return implode($this->glue, parent::__invoke($data, $context));
}
}
12 changes: 12 additions & 0 deletions test/Functional/DocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
20 changes: 20 additions & 0 deletions test/Integration/Mapper/Strategy/JoinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace ScriptFUSIONTest\Integration\Mapper\Strategy;

use ScriptFUSION\Mapper\Mapper;
use ScriptFUSION\Mapper\Strategy\Join;
use ScriptFUSION\Mapper\Strategy\Strategy;

final class JoinTest extends \PHPUnit_Framework_TestCase
{
public function testJoin()
{
$join = (new Join(
'-',
'foo',
\Mockery::mock(Strategy::class)->shouldReceive('__invoke')->andReturn('bar')->once()->getMock()
))->setMapper(new Mapper);

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

0 comments on commit fa9d018

Please sign in to comment.