Skip to content

Commit

Permalink
Merge pull request #30 from Bl00D4NGEL/feat/add-array-functions-to-co…
Browse files Browse the repository at this point in the history
…llection

feat: Add toArray and toList functions to Collection
  • Loading branch information
Bl00D4NGEL authored Apr 24, 2024
2 parents 2bb6795 + 7afc215 commit 9b67d79
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/Domain/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use InvalidArgumentException;
use IteratorAggregate;
use Traversable;

use function array_filter;
use function array_map;
use function array_reduce;
Expand All @@ -28,7 +29,7 @@
class Collection implements ArrayAccess, Countable, IteratorAggregate
{
/**
* @param T[] $items
* @param array<array-key, T> $items
* @param class-string<T>|null $itemType
* @throws Assert\AssertionFailedException
*/
Expand Down Expand Up @@ -63,6 +64,27 @@ public static function fromIterable(iterable $items, ?string $itemType = null):
return new static(iterator_to_array($items), $itemType);
}

/**
* Returns the collection as an array.
* The returned array is either a key-value array with values of type T or a list of T
*
* @return array<array-key, T>|list<T>
*/
public function toArray(): array
{
return $this->items;
}

/**
* Returns the collection as a list of T
*
* @return list<T>
*/
public function toList(): array
{
return array_values($this->items);
}

/**
* Returns true if every value in the collection passes the callback truthy test. Opposite of self::none().
* Callback arguments will be element, index, collection.
Expand Down
30 changes: 29 additions & 1 deletion tests/Domain/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function testAddMultiple(): void
public function testFilter(): void
{
// Given
$items = [1, 2, 3,4, 5, 6, 7, 8, 9, 10];
$items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$collection = new Collection($items);

// When
Expand Down Expand Up @@ -584,4 +584,32 @@ public function testHasItems(): void
$this->assertFalse((new Collection([]))->hasItems());
$this->assertTrue((new Collection([1]))->hasItems());
}

public function testToArray(): void
{
$this->assertSame([], (new Collection([]))->toArray());

$list = [1, 2, 3];
$this->assertSame($list, (new Collection($list))->toArray());

$keyValue = ['foo' => 1, 'bar' => 2, 'baz' => 3];
$this->assertSame($keyValue, (new Collection($keyValue))->toArray());

$numberIndexed = [1 => 1, 2 => 2, 3 => 3, 0 => 0];
$this->assertSame($numberIndexed, (new Collection($numberIndexed))->toArray());
}

public function testToList(): void
{
$this->assertSame([], (new Collection([]))->toList());

$list = [1, 2, 3];
$this->assertSame($list, (new Collection($list))->toList());

$keyValue = ['foo' => 1, 'bar' => 2, 'baz' => 3];
$this->assertSame([1, 2, 3], (new Collection($keyValue))->toList());

$numberIndexed = [1 => 1, 2 => 2, 3 => 3, 0 => 0];
$this->assertSame([1, 2, 3, 0], (new Collection($numberIndexed))->toList());
}
}

0 comments on commit 9b67d79

Please sign in to comment.