Skip to content

Commit

Permalink
feature: introduce OrderedListInterface#combine
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Bösing <[email protected]>
  • Loading branch information
boesing committed Aug 22, 2024
1 parent d36e7b6 commit a359f2c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/OrderedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,14 @@ public function shuffle(): OrderedListInterface

return $instance;
}

public function combine(OrderedListInterface $other, OrderedListInterface ...$others): OrderedListInterface
{
$instance = clone $this;
foreach ([$other, ...$others] as $other) {
$instance->data = [...$instance->data, ...$other->toNativeArray()];
}

return $instance;
}
}
10 changes: 10 additions & 0 deletions src/OrderedListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,14 @@ public function removeAt(int $index): self;
* @return OrderedListInterface<TValue>
*/
public function shuffle(): self;

/**
* Combines multiple lists into one.
*
* @template TValueFromOther of TValue
* @param OrderedListInterface<TValueFromOther> $other
* @param OrderedListInterface<TValueFromOther> ...$others
* @return OrderedListInterface<TValue|TValueFromOther>
*/
public function combine(OrderedListInterface $other, OrderedListInterface ...$others): self;
}
45 changes: 45 additions & 0 deletions tests/GenericOrderedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1487,4 +1487,49 @@ public function testWillShuffleValuesInList(): void
self::assertNotSame($list, $list2);
self::assertNotSame([1, 2, 3], $list2->toNativeArray());
}

public function testWillCombineWithAnohterList(): void
{
/**
* @var OrderedListInterface<non-empty-string> $list
*/
$list = new GenericOrderedList([
'foo',
'bar',
'baz',
]);

$list2 = new GenericOrderedList([
'qoo',
'ooq',
]);

$list3 = $list->combine($list2);

self::assertNotSame($list, $list3);
self::assertSame(['foo', 'bar', 'baz', 'qoo', 'ooq'], $list3->toNativeArray());
}

public function testWillCombineWithMultipleLists(): void
{
/**
* @var OrderedListInterface<non-empty-string> $list
*/
$list = new GenericOrderedList(['foo']);

$list2 = new GenericOrderedList(['bar']);

$list3 = new GenericOrderedList([
'baz',
'qoo',
'ooq',
]);

$list4 = $list->combine($list2, $list3);

self::assertNotSame($list, $list2);
self::assertNotSame($list, $list3);
self::assertNotSame($list, $list4);
self::assertSame(['foo', 'bar', 'baz', 'qoo', 'ooq'], $list4->toNativeArray());
}
}

0 comments on commit a359f2c

Please sign in to comment.