70 lines
1.4 KiB
PHP
70 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Drupal\opd_presentations;
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \IteratorAggregate<Event>
|
|
*/
|
|
readonly final class Events implements \Countable, \IteratorAggregate {
|
|
|
|
public function count(): int {
|
|
return count($this->events);
|
|
}
|
|
|
|
public function filter(\Closure $callback): self {
|
|
return new self(array_filter(
|
|
array: $this->events,
|
|
callback: $callback,
|
|
));
|
|
}
|
|
|
|
public function first(): Event {
|
|
return array_values($this->events)[0];
|
|
}
|
|
|
|
public function getIterator(): \Traversable {
|
|
return new \ArrayIterator($this->events);
|
|
}
|
|
|
|
public function getPast(): self {
|
|
return (new self($this->events))
|
|
->filter(fn (Event $event): bool => $event->isPast());
|
|
}
|
|
|
|
public static function fromDateStrings(string ...$dates): self {
|
|
$events = array_map(
|
|
array: $dates,
|
|
callback: fn (string $date): Event => Event::create([
|
|
'field_date' => strtotime($date),
|
|
]),
|
|
);
|
|
|
|
return new self($events);
|
|
}
|
|
|
|
/**
|
|
* @return Event[]
|
|
*/
|
|
public function toEvents(): array {
|
|
return $this->events;
|
|
}
|
|
|
|
/**
|
|
* @param Event[] $events
|
|
*/
|
|
public static function fromEvents(array $events): self {
|
|
return new self($events);
|
|
}
|
|
|
|
/**
|
|
* @param Event[] $events
|
|
*/
|
|
private function __construct(private array $events) {
|
|
Assert::allIsInstanceOf($events, Event::class);
|
|
}
|
|
|
|
}
|