Skip to content

Commit afc4dc9

Browse files
Merge pull request #19 from cocur/join
Add Join link, fixes #18
2 parents 82c4dd6 + c4ffeda commit afc4dc9

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ $chain->reduce(function ($current, $value) {
205205
- `->count()`
206206
- `->countValues()`
207207
- `->first()`
208+
- `->join([$glue])`
208209
- `->last()`
209210
- `->reduce()`
210211
- `->sum()`

src/Chain.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Cocur\Chain\Link\Intersect;
1515
use Cocur\Chain\Link\IntersectAssoc;
1616
use Cocur\Chain\Link\IntersectKey;
17+
use Cocur\Chain\Link\Join;
1718
use Cocur\Chain\Link\Keys;
1819
use Cocur\Chain\Link\Last;
1920
use Cocur\Chain\Link\Map;
@@ -41,7 +42,7 @@
4142
* Chain.
4243
*
4344
* @author Florian Eckerstorfer
44-
* @copyright 2015 Florian Eckerstorfer
45+
* @copyright 2015-2017 Florian Eckerstorfer
4546
*/
4647
class Chain extends AbstractChain implements Countable
4748
{
@@ -57,6 +58,7 @@ class Chain extends AbstractChain implements Countable
5758
Intersect,
5859
IntersectAssoc,
5960
IntersectKey,
61+
Join,
6062
Keys,
6163
Last,
6264
Map,

src/Link/Join.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Cocur\Chain\Link;
4+
5+
/**
6+
* Implode.
7+
*
8+
* @author Florian Eckerstorfer
9+
* @copyright 2017 Florian Eckerstorfer
10+
*/
11+
trait Join
12+
{
13+
/**
14+
* Join array elements with a string
15+
*
16+
* @param string $glue
17+
*
18+
* @return string Returns a string containing a string representation of all the array elements in the same order,
19+
* with the glue string between each element.
20+
*/
21+
public function join($glue = '')
22+
{
23+
return implode($glue, $this->array);
24+
}
25+
}

tests/ChainTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function chainHasTraits()
6868
$this->assertTrue(method_exists($c, 'intersect'));
6969
$this->assertTrue(method_exists($c, 'intersectAssoc'));
7070
$this->assertTrue(method_exists($c, 'intersectKey'));
71+
$this->assertTrue(method_exists($c, 'join'));
7172
$this->assertTrue(method_exists($c, 'keys'));
7273
$this->assertTrue(method_exists($c, 'last'));
7374
$this->assertTrue(method_exists($c, 'map'));

tests/Link/JoinTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Cocur\Chain\Link;
4+
5+
use PHPUnit_Framework_TestCase;
6+
7+
/**
8+
* CountTest.
9+
*
10+
* @author Florian Eckerstorfer
11+
* @copyright 2015 Florian Eckerstorfer
12+
* @group unit
13+
*/
14+
class JoinTest extends PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @test
18+
* @covers Cocur\Chain\Link\Join::join()
19+
*/
20+
public function joinReturnsStringWithoutGlue()
21+
{
22+
/** @var \Cocur\Chain\Link\Join $mock */
23+
$mock = $this->getMockForTrait('Cocur\Chain\Link\Join');
24+
$mock->array = ['a', 'b', 'c'];
25+
26+
$this->assertEquals('abc', $mock->join());
27+
}
28+
}

0 commit comments

Comments
 (0)