Skip to content

Commit

Permalink
Add filter and toArray methods
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 17, 2024
1 parent e96dc4f commit d399959
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/DataTransferObject/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class Collection implements \IteratorAggregate, \Countable
{
/** @var list<T> */
public array $items;
private array $items;

/**
* @param list<T> $items
Expand All @@ -39,4 +39,22 @@ public function count(): int
{
return count($this->items);
}

/**
* @param callable(T):bool $callback
*
* @return self<T>
*/
public function filter(callable $callback): self
{
return new self(array_values(array_filter($this->items, $callback)));
}

/**
* @return list<T>
*/
public function toArray(): array
{
return $this->items;
}
}

0 comments on commit d399959

Please sign in to comment.