Skip to content

Commit

Permalink
fix: Move SimpleMerge into Trait (Issue cocur#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
colorninja committed Nov 6, 2020
1 parent 418890e commit 94935c5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 36 deletions.
22 changes: 2 additions & 20 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Cocur\Chain\Link\Search;
use Cocur\Chain\Link\Shift;
use Cocur\Chain\Link\Shuffle;
use Cocur\Chain\Link\SimpleMerge;
use Cocur\Chain\Link\Slice;
use Cocur\Chain\Link\Some;
use Cocur\Chain\Link\Sort;
Expand Down Expand Up @@ -74,6 +75,7 @@ class Chain extends AbstractChain implements Countable
use Last;
use Map;
use Merge;
use SimpleMerge;
use Pad;
use Pop;
use Product;
Expand Down Expand Up @@ -158,24 +160,4 @@ public static function fill(int $startIndex, int $num, $value = null): self
{
return new self(array_fill($startIndex, $num, $value));
}

/**
* Create a new Chain from multiple arrays.
* Creates a new Chain and merges the given arrays
* @param array ...$arrays The arrays to merge
*
* @return self
*/
public static function merge(array &...$arrays): self
{
$result = [];

foreach ($arrays as $array) {
foreach($array as $item) {
$result[] = $item;
}
}

return new self($result);
}
}
29 changes: 29 additions & 0 deletions src/Link/SimpleMerge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Cocur\Chain\Link;

use Cocur\Chain\Chain;

/**
* Simple, therefore quicker merge.
* @author Andrey Tsarev
* @copyright 2015-2018 Florian Eckerstorfer
*/
trait SimpleMerge
{
/**
* Merge arrays.
* Merge the elements of one array with the elements of the array in the Chain.
* @param Chain|array $array Array to merge with
* @return self
*/
public function simpleMerge( $array ) : self
{
$array = $array instanceof Chain ? $array->array : $array;
foreach ($array as $item) {
$this->array[] = $item;
}

return $this;
}
}
16 changes: 0 additions & 16 deletions tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,6 @@ public function fillCreatesAFilledChain(): void
$this->assertCount(10, $chain->array);
}

/**
* @test
* @covers \Cocur\Chain\Chain::merge()
*/
public function fillCreatesAMergedChain(): void
{
$array1 = [1,2,3];
$array2 = [4,5];
$array3 = [6];

$chain = Chain::merge($array1, $array2, $array3);

$this->assertIsArray($chain->array);
$this->assertCount(6, $chain->array);
}

/**
* @test
*/
Expand Down
43 changes: 43 additions & 0 deletions tests/Link/SimpleMergeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Cocur\Chain\Link;

use Cocur\Chain\Chain;

/**
* SimpleMergeTest.
*
* @author Andrey Tsarev
* @copyright 2015-2018 Florian Eckerstorfer
*/
class SimpleMergeTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
* @covers \Cocur\Chain\Link\SimpleMerge::simpleMerge()
*/
public function mergeMergesArray(): void
{
/** @var SimpleMerge $mock */
$mock = $this->getMockForTrait(SimpleMerge::class);
$mock->array = [0, 1, 2];
$mock->simpleMerge([3, 4]);

$this->assertEquals([0, 1, 2, 3, 4], $mock->array);
}

/**
* @test
* @covers \Cocur\Chain\Link\SimpleMerge::simpleMerge()
*/
public function mergeMergesChain(): void
{
/** @var SimpleMerge $mock */
$mock = $this->getMockForTrait(SimpleMerge::class);
$mock->array = [0, 1, 2];
$mock->simpleMerge(Chain::create([3, 4]));

$this->assertEquals([0, 1, 2, 3, 4], $mock->array);
}

}

0 comments on commit 94935c5

Please sign in to comment.