Skip to content

Commit c883b50

Browse files
committed
feat: add toList and toArray functions to Collection
1 parent 337df9b commit c883b50

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/Domain/Collection.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class Collection implements ArrayAccess, Countable, IteratorAggregate
2929
{
3030
/**
31-
* @param T[] $items
31+
* @param array<array-key, T> $items
3232
* @param class-string<T>|null $itemType
3333
* @throws Assert\AssertionFailedException
3434
*/
@@ -63,6 +63,27 @@ public static function fromIterable(iterable $items, ?string $itemType = null):
6363
return new static(iterator_to_array($items), $itemType);
6464
}
6565

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

0 commit comments

Comments
 (0)