From 9308ce76a5a7f7d4793df794bb48c6ade8b220aa Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 12 Jun 2025 02:10:08 +0100 Subject: [PATCH] Refactor --- modules/opd_presentations/src/Events.php | 7 +++++++ modules/opd_presentations/src/Presentation.php | 7 ++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/opd_presentations/src/Events.php b/modules/opd_presentations/src/Events.php index 4deb9127d..ef1029ab2 100644 --- a/modules/opd_presentations/src/Events.php +++ b/modules/opd_presentations/src/Events.php @@ -15,6 +15,13 @@ readonly final class Events implements \IteratorAggregate { private function __construct(private array $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]; } diff --git a/modules/opd_presentations/src/Presentation.php b/modules/opd_presentations/src/Presentation.php index 6116728a2..330e960fe 100644 --- a/modules/opd_presentations/src/Presentation.php +++ b/modules/opd_presentations/src/Presentation.php @@ -12,14 +12,11 @@ final class Presentation extends Node implements NodeInterface { public const NODE_TYPE = 'presentation'; public function getPastEvents(): Events { - $events = $this->get('field_events')->referencedEntities(); + $events = Events::new($this->get('field_events')->referencedEntities()); $today = strtotime('today'); - return Events::new(array_filter( - array: $events, - callback: fn (Event $event): bool => $event->getEventDate() < $today, - )); + return $events->filter(fn (Event $event) => $event->getEventDate() < $today); } }