Skip to content

Commit

Permalink
Merge pull request #19 from cocur/join
Browse files Browse the repository at this point in the history
Add Join link, fixes #18
  • Loading branch information
florianeckerstorfer committed Dec 4, 2017
2 parents 82c4dd6 + c4ffeda commit afc4dc9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -205,6 +205,7 @@ $chain->reduce(function ($current, $value) {
- `->count()`
- `->countValues()`
- `->first()`
- `->join([$glue])`
- `->last()`
- `->reduce()`
- `->sum()`
Expand Down
4 changes: 3 additions & 1 deletion src/Chain.php
Expand Up @@ -14,6 +14,7 @@
use Cocur\Chain\Link\Intersect;
use Cocur\Chain\Link\IntersectAssoc;
use Cocur\Chain\Link\IntersectKey;
use Cocur\Chain\Link\Join;
use Cocur\Chain\Link\Keys;
use Cocur\Chain\Link\Last;
use Cocur\Chain\Link\Map;
Expand Down Expand Up @@ -41,7 +42,7 @@
* Chain.
*
* @author Florian Eckerstorfer
* @copyright 2015 Florian Eckerstorfer
* @copyright 2015-2017 Florian Eckerstorfer
*/
class Chain extends AbstractChain implements Countable
{
Expand All @@ -57,6 +58,7 @@ class Chain extends AbstractChain implements Countable
Intersect,
IntersectAssoc,
IntersectKey,
Join,
Keys,
Last,
Map,
Expand Down
25 changes: 25 additions & 0 deletions src/Link/Join.php
@@ -0,0 +1,25 @@
<?php

namespace Cocur\Chain\Link;

/**
* Implode.
*
* @author Florian Eckerstorfer
* @copyright 2017 Florian Eckerstorfer
*/
trait Join
{
/**
* Join array elements with a string
*
* @param string $glue
*
* @return string Returns a string containing a string representation of all the array elements in the same order,
* with the glue string between each element.
*/
public function join($glue = '')
{
return implode($glue, $this->array);
}
}
1 change: 1 addition & 0 deletions tests/ChainTest.php
Expand Up @@ -68,6 +68,7 @@ public function chainHasTraits()
$this->assertTrue(method_exists($c, 'intersect'));
$this->assertTrue(method_exists($c, 'intersectAssoc'));
$this->assertTrue(method_exists($c, 'intersectKey'));
$this->assertTrue(method_exists($c, 'join'));
$this->assertTrue(method_exists($c, 'keys'));
$this->assertTrue(method_exists($c, 'last'));
$this->assertTrue(method_exists($c, 'map'));
Expand Down
28 changes: 28 additions & 0 deletions tests/Link/JoinTest.php
@@ -0,0 +1,28 @@
<?php

namespace Cocur\Chain\Link;

use PHPUnit_Framework_TestCase;

/**
* CountTest.
*
* @author Florian Eckerstorfer
* @copyright 2015 Florian Eckerstorfer
* @group unit
*/
class JoinTest extends PHPUnit_Framework_TestCase
{
/**
* @test
* @covers Cocur\Chain\Link\Join::join()
*/
public function joinReturnsStringWithoutGlue()
{
/** @var \Cocur\Chain\Link\Join $mock */
$mock = $this->getMockForTrait('Cocur\Chain\Link\Join');
$mock->array = ['a', 'b', 'c'];

$this->assertEquals('abc', $mock->join());
}
}

0 comments on commit afc4dc9

Please sign in to comment.