Skip to content

Commit 9b67d79

Browse files
authored
Merge pull request #30 from Bl00D4NGEL/feat/add-array-functions-to-collection
feat: Add toArray and toList functions to Collection
2 parents 2bb6795 + 7afc215 commit 9b67d79

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/Domain/Collection.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use InvalidArgumentException;
1212
use IteratorAggregate;
1313
use Traversable;
14+
1415
use function array_filter;
1516
use function array_map;
1617
use function array_reduce;
@@ -28,7 +29,7 @@
2829
class Collection implements ArrayAccess, Countable, IteratorAggregate
2930
{
3031
/**
31-
* @param T[] $items
32+
* @param array<array-key, T> $items
3233
* @param class-string<T>|null $itemType
3334
* @throws Assert\AssertionFailedException
3435
*/
@@ -63,6 +64,27 @@ public static function fromIterable(iterable $items, ?string $itemType = null):
6364
return new static(iterator_to_array($items), $itemType);
6465
}
6566

67+
/**
68+
* Returns the collection as an array.
69+
* The returned array is either a key-value array with values of type T or a list of T
70+
*
71+
* @return array<array-key, T>|list<T>
72+
*/
73+
public function toArray(): array
74+
{
75+
return $this->items;
76+
}
77+
78+
/**
79+
* Returns the collection as a list of T
80+
*
81+
* @return list<T>
82+
*/
83+
public function toList(): array
84+
{
85+
return array_values($this->items);
86+
}
87+
6688
/**
6789
* Returns true if every value in the collection passes the callback truthy test. Opposite of self::none().
6890
* Callback arguments will be element, index, collection.

tests/Domain/CollectionTest.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testAddMultiple(): void
156156
public function testFilter(): void
157157
{
158158
// Given
159-
$items = [1, 2, 3,4, 5, 6, 7, 8, 9, 10];
159+
$items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
160160
$collection = new Collection($items);
161161

162162
// When
@@ -584,4 +584,32 @@ public function testHasItems(): void
584584
$this->assertFalse((new Collection([]))->hasItems());
585585
$this->assertTrue((new Collection([1]))->hasItems());
586586
}
587+
588+
public function testToArray(): void
589+
{
590+
$this->assertSame([], (new Collection([]))->toArray());
591+
592+
$list = [1, 2, 3];
593+
$this->assertSame($list, (new Collection($list))->toArray());
594+
595+
$keyValue = ['foo' => 1, 'bar' => 2, 'baz' => 3];
596+
$this->assertSame($keyValue, (new Collection($keyValue))->toArray());
597+
598+
$numberIndexed = [1 => 1, 2 => 2, 3 => 3, 0 => 0];
599+
$this->assertSame($numberIndexed, (new Collection($numberIndexed))->toArray());
600+
}
601+
602+
public function testToList(): void
603+
{
604+
$this->assertSame([], (new Collection([]))->toList());
605+
606+
$list = [1, 2, 3];
607+
$this->assertSame($list, (new Collection($list))->toList());
608+
609+
$keyValue = ['foo' => 1, 'bar' => 2, 'baz' => 3];
610+
$this->assertSame([1, 2, 3], (new Collection($keyValue))->toList());
611+
612+
$numberIndexed = [1 => 1, 2 => 2, 3 => 3, 0 => 0];
613+
$this->assertSame([1, 2, 3, 0], (new Collection($numberIndexed))->toList());
614+
}
587615
}

0 commit comments

Comments
 (0)