oliverdavies.uk/src/Presentation/Collection/AbstractCollection.php

43 lines
780 B
PHP
Raw Normal View History

2025-05-04 21:32:34 +01:00
<?php
declare(strict_types=1);
namespace App\Presentation\Collection;
use ArrayIterator;
use Countable;
use Iterator;
use IteratorAggregate;
abstract class AbstractCollection implements Countable, IteratorAggregate
{
public function __construct(protected array $items = [])
{
}
public function count(): int
{
return count($this->items);
}
public function filter(callable $callback): self
{
return new static(
array_filter(
array: $this->items,
callback: $callback,
)
);
}
public function getIterator(): Iterator
{
return new ArrayIterator($this->items);
}
public function toArray(): array
{
return $this->items;
}
}