Skip to content

Commit d399959

Browse files
committed
Add filter and toArray methods
1 parent e96dc4f commit d399959

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/DataTransferObject/Collection.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
final class Collection implements \IteratorAggregate, \Countable
1313
{
1414
/** @var list<T> */
15-
public array $items;
15+
private array $items;
1616

1717
/**
1818
* @param list<T> $items
@@ -39,4 +39,22 @@ public function count(): int
3939
{
4040
return count($this->items);
4141
}
42+
43+
/**
44+
* @param callable(T):bool $callback
45+
*
46+
* @return self<T>
47+
*/
48+
public function filter(callable $callback): self
49+
{
50+
return new self(array_values(array_filter($this->items, $callback)));
51+
}
52+
53+
/**
54+
* @return list<T>
55+
*/
56+
public function toArray(): array
57+
{
58+
return $this->items;
59+
}
4260
}

0 commit comments

Comments
 (0)