Skip to content

Commit

Permalink
Merge pull request #79 from boesing/feature/typed-merge-stack
Browse files Browse the repository at this point in the history
feature: introduce typed merge stack
  • Loading branch information
boesing committed Jun 7, 2021
2 parents d2ed907 + 2d9967c commit 3ba0be6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
9 changes: 6 additions & 3 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ public function __construct(array $data = [])
parent::__construct($data);
}

public function merge(...$stack): MapInterface
public function merge(MapInterface ...$stack): MapInterface
{
$instance = clone $this;
foreach ($stack as $list) {
$instance->data = array_merge($instance->data, $list->toNativeArray());
$merges = [];
foreach ($stack as $map) {
$merges[] = $map->toNativeArray();
}

$instance->data = array_merge($instance->data, ...$merges);

return $instance;
}

Expand Down
2 changes: 1 addition & 1 deletion src/MapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function sort(?callable $callback = null): MapInterface;
* @psalm-param list<MapInterface<TKey,TValue>> $stack
* @psalm-return MapInterface<TKey,TValue>
*/
public function merge(...$stack): MapInterface;
public function merge(MapInterface ...$stack): MapInterface;

/**
* @psalm-param MapInterface<TKey,TValue> $other
Expand Down
13 changes: 6 additions & 7 deletions src/OrderedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use function array_key_exists;
use function array_keys;
use function array_map;
use function array_merge;
use function array_replace;
use function array_reverse;
Expand Down Expand Up @@ -46,14 +45,14 @@ final public function __construct(array $data = [])
parent::__construct($data);
}

public function merge(...$stack): OrderedListInterface
public function merge(OrderedListInterface ...$stack): OrderedListInterface
{
$instance = clone $this;
$values = array_map(static function (OrderedListInterface $list): array {
return $list->toNativeArray();
}, $stack);

$instance->data = array_values(array_merge($instance->data, ...$values));
foreach ($stack as $list) {
foreach ($list->toNativeArray() as $value) {
$instance->data[] = $value;
}
}

return $instance;
}
Expand Down
2 changes: 1 addition & 1 deletion src/OrderedListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function sort(?callable $callback = null): OrderedListInterface;
* @psalm-param list<OrderedListInterface<TValue>> $stack
* @psalm-return OrderedListInterface<TValue>
*/
public function merge(...$stack): OrderedListInterface;
public function merge(OrderedListInterface ...$stack): OrderedListInterface;

/**
* @template TNewValue
Expand Down
8 changes: 8 additions & 0 deletions tests/GenericMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ public function mergeStacks(): Generator
['baz' => 'bar', 'qoo' => 'ooq'],
],
];

yield 'none' => [
['foo' => 'bar'],
['foo' => 'bar'],
[
[],
],
];
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/GenericOrderedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ public function mergeStacks(): Generator
['baz', 'foo'],
],
];

yield 'none' => [
[
'foo',
'bar',
],
[
'foo',
'bar',
],
[
[],
],
];
}

public function testWillMapByUsingCallable(): void
Expand Down

0 comments on commit 3ba0be6

Please sign in to comment.