Add remove method
This commit is contained in:
parent
a9665c3611
commit
7cc6e66313
3 changed files with 54 additions and 3 deletions
|
@ -14,9 +14,24 @@ class AbstractCollection implements CollectionInterface
|
||||||
|
|
||||||
public function add(mixed $item): void
|
public function add(mixed $item): void
|
||||||
{
|
{
|
||||||
|
if (null === $item) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$this->items[] = $item;
|
$this->items[] = $item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function remove(mixed $item): void
|
||||||
|
{
|
||||||
|
$key = array_search($item, $this->items, true);
|
||||||
|
|
||||||
|
if ($key) {
|
||||||
|
unset($this->items[$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->items = array_values($this->items);
|
||||||
|
}
|
||||||
|
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
return $this->items;
|
return $this->items;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Loom\Utility\Collection\Interface;
|
namespace Loom\Utility\Collection\Interface;
|
||||||
|
@ -6,5 +7,6 @@ namespace Loom\Utility\Collection\Interface;
|
||||||
interface CollectionInterface
|
interface CollectionInterface
|
||||||
{
|
{
|
||||||
public function add(mixed $item): void;
|
public function add(mixed $item): void;
|
||||||
|
public function remove(mixed $item): void;
|
||||||
public function toArray(): array;
|
public function toArray(): array;
|
||||||
}
|
}
|
|
@ -19,20 +19,54 @@ class CollectionTest extends TestCase
|
||||||
$this->assertEquals($expectedResult, $collection->toArray());
|
$this->assertEquals($expectedResult, $collection->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[DataProvider('removeDataProvider')]
|
||||||
|
public function testRemove(array $startingArray, mixed $itemToRemove, array $expectedResult): void
|
||||||
|
{
|
||||||
|
$collection = new Collection($startingArray);
|
||||||
|
$collection->remove($itemToRemove);
|
||||||
|
|
||||||
|
$this->assertEquals($expectedResult, $collection->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
public static function addDataProvider(): array
|
public static function addDataProvider(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
[
|
[
|
||||||
'startingItems' => ['apple', 'banana', 'cherry'],
|
'startingItems' => ['A', 'B', 'C'],
|
||||||
'itemToAdd' => 'grape',
|
'itemToAdd' => 'D',
|
||||||
'expectedResult' => ['apple', 'banana', 'cherry', 'grape'],
|
'expectedResult' => ['A', 'B', 'C', 'D'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'startingItems' => [],
|
'startingItems' => [],
|
||||||
'itemToAdd' => 1,
|
'itemToAdd' => 1,
|
||||||
'expectedResult' => [1],
|
'expectedResult' => [1],
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
'startingItems' => [],
|
||||||
|
'itemToAdd' => null,
|
||||||
|
'expectedResult' => [],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function removeDataProvider(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'startingArray' => ['A', 'B', 'C'],
|
||||||
|
'itemToRemove' => 'B',
|
||||||
|
'expectedResult' => ['A', 'C'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'startingArray' => ['A', 'B', 'C'],
|
||||||
|
'itemToRemove' => 'orange',
|
||||||
|
'expectedResult' => ['A', 'B', 'C'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'startingArray' => [],
|
||||||
|
'itemToRemove' => 'A',
|
||||||
|
'expectedResult' => [],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue