oliverdavies.uk/modules/opd_presentations/src/Events.php

44 lines
836 B
PHP
Raw Normal View History

2025-06-10 20:47:23 +01:00
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations;
2025-06-10 20:47:23 +01:00
use Webmozart\Assert\Assert;
2025-06-10 20:47:23 +01:00
/**
2025-06-12 02:10:08 +01:00
* @implements \IteratorAggregate<Event>
2025-06-10 20:47:23 +01:00
*/
readonly final class Events implements \IteratorAggregate {
2025-06-10 20:47:23 +01:00
2025-06-12 02:10:08 +01:00
public function filter(\Closure $callback): self {
return new self(array_filter(
array: $this->events,
callback: $callback,
));
}
2025-06-12 02:10:08 +01:00
public function first(): Event {
2025-06-11 09:54:18 +01:00
return array_values($this->events)[0];
}
2025-06-10 20:47:23 +01:00
public function getIterator(): \Traversable {
return new \ArrayIterator($this->events);
}
2025-06-12 02:10:09 +01:00
/**
* @param Event[] $events
*/
2025-06-12 02:10:09 +01:00
public static function fromEvents(array $events): self {
return new self($events);
}
2025-06-12 02:10:09 +01:00
/**
* @param Event[] $events
*/
private function __construct(private array $events) {
Assert::allIsInstanceOf($events, Event::class);
}
2025-06-10 20:47:23 +01:00
}