Skip to content

Commit

Permalink
Added Joins 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 97b5aaf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 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
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 97b5aaf

Please sign in to comment.