diff --git a/modules/opd_daily_emails/opd_daily_emails.module b/modules/opd_daily_emails/opd_daily_emails.module index a9dfc9d4c..68b433bf2 100644 --- a/modules/opd_daily_emails/opd_daily_emails.module +++ b/modules/opd_daily_emails/opd_daily_emails.module @@ -4,7 +4,7 @@ declare(strict_types=1); use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\StringTranslation\TranslatableMarkup; -use Drupal\opd_daily_emails\Repository\DailyEmailNodeRepository; +use Drupal\opd_daily_emails\DailyEmailNodeRepository; /** * Implements hook_token_info(). diff --git a/modules/opd_daily_emails/opd_daily_emails.services.yml b/modules/opd_daily_emails/opd_daily_emails.services.yml index 9345f1e2e..43fbb8e41 100644 --- a/modules/opd_daily_emails/opd_daily_emails.services.yml +++ b/modules/opd_daily_emails/opd_daily_emails.services.yml @@ -1,3 +1,3 @@ services: - Drupal\opd_daily_emails\Repository\DailyEmailNodeRepository: + Drupal\opd_daily_emails\DailyEmailNodeRepository: autowire: true diff --git a/modules/opd_daily_emails/src/Repository/DailyEmailNodeRepository.php b/modules/opd_daily_emails/src/DailyEmailNodeRepository.php similarity index 77% rename from modules/opd_daily_emails/src/Repository/DailyEmailNodeRepository.php rename to modules/opd_daily_emails/src/DailyEmailNodeRepository.php index 02374708e..46380e8b7 100644 --- a/modules/opd_daily_emails/src/Repository/DailyEmailNodeRepository.php +++ b/modules/opd_daily_emails/src/DailyEmailNodeRepository.php @@ -2,11 +2,10 @@ declare(strict_types=1); -namespace Drupal\opd_daily_emails\Repository; +namespace Drupal\opd_daily_emails; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\node\NodeInterface; -use Drupal\opd_daily_emails\Collection\DailyEmailCollection; final class DailyEmailNodeRepository implements DailyEmailRepositoryInterface { @@ -15,7 +14,7 @@ final class DailyEmailNodeRepository implements DailyEmailRepositoryInterface { ) { } - public function getAll(): DailyEmailCollection { + public function getAll(): DailyEmails { $nodeStorage = $this->entityTypeManager ->getStorage('node'); @@ -31,7 +30,7 @@ final class DailyEmailNodeRepository implements DailyEmailRepositoryInterface { /** @var NodeInterface[] */ $nodes = $nodeStorage->loadMultiple($nodeIds); - return new DailyEmailCollection($nodes); + return DailyEmails::new($nodes); } } diff --git a/modules/opd_daily_emails/src/DailyEmailRepositoryInterface.php b/modules/opd_daily_emails/src/DailyEmailRepositoryInterface.php new file mode 100644 index 000000000..8cce569f7 --- /dev/null +++ b/modules/opd_daily_emails/src/DailyEmailRepositoryInterface.php @@ -0,0 +1,11 @@ + $emails */ - public function __construct( + private function __construct( private array $emails, ) { } @@ -24,4 +24,8 @@ final class DailyEmailCollection implements \Countable { return array_values($this->emails)[0]; } + public static function new(array $emails): self { + return new self($emails); + } + } diff --git a/modules/opd_daily_emails/src/Repository/DailyEmailRepositoryInterface.php b/modules/opd_daily_emails/src/Repository/DailyEmailRepositoryInterface.php deleted file mode 100644 index 5445c0105..000000000 --- a/modules/opd_daily_emails/src/Repository/DailyEmailRepositoryInterface.php +++ /dev/null @@ -1,13 +0,0 @@ -getGuests(); - assert(is_array($guests)); - assert(!is_null($guests[0])); - assert($guests[0] instanceof TermInterface); - - // TODO: allow for more than two guests. - if (count($guests) === 2) { - assert($guests[1] instanceof TermInterface); - - $replacements[$original] = sprintf('%s %s %s', $guests[0]->label(), t('and'), $guests[1]->label()); - break; - } - - $replacements[$original] = $guests[0]->label(); + $replacements[$original] = strval($node->getGuests()); break; } } diff --git a/modules/opd_podcast/src/Entity/PodcastEpisode.php b/modules/opd_podcast/src/PodcastEpisode.php similarity index 56% rename from modules/opd_podcast/src/Entity/PodcastEpisode.php rename to modules/opd_podcast/src/PodcastEpisode.php index 70026de7f..4ca0e0061 100644 --- a/modules/opd_podcast/src/Entity/PodcastEpisode.php +++ b/modules/opd_podcast/src/PodcastEpisode.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Drupal\opd_podcast\Entity; +namespace Drupal\opd_podcast; use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; @@ -11,8 +11,8 @@ final class PodcastEpisode extends Node implements NodeInterface { public const NODE_TYPE = 'podcast_episode'; - public function getGuests(): array { - return $this->get('field_podcast_guests')->referencedEntities(); + public function getGuests(): PodcastGuests { + return PodcastGuests::new($this->get('field_podcast_guests')->referencedEntities()); } } diff --git a/modules/opd_podcast/src/PodcastGuests.php b/modules/opd_podcast/src/PodcastGuests.php new file mode 100644 index 000000000..557d1dff5 --- /dev/null +++ b/modules/opd_podcast/src/PodcastGuests.php @@ -0,0 +1,62 @@ + + */ +final class PodcastGuests implements \ArrayAccess, \Countable, \IteratorAggregate, \Stringable { + + /** + * @param TermInterface[] $guests + */ + private function __construct(private array $guests) { + } + + public function count(): int { + return count($this->guests); + } + + public function getIterator(): \Traversable { + return new \ArrayIterator($this->guests); + } + + public function offsetExists(mixed $offset): bool { + return isset($this->guests[$offset]); + } + + public function offsetGet(mixed $offset): mixed { + return $this->guests[$offset]; + } + + public function offsetSet(mixed $offset, mixed $value): void { + $this->guests[$offset] = $value; + } + + public function offsetUnset(mixed $offset): void { + unset($this->guests[$offset]); + } + + public function __toString(): string { + // TODO: allow for more than two guests. + if (count($this->guests) === 2) { + assert($this->guests[1] instanceof TermInterface); + + return sprintf('%s %s %s', $this->guests[0]->label(), t('and'), $this->guests[1]->label()); + } + + return strval($this->guests[0]->label()); + } + + /** + * @param TermInterface[] $guests + */ + public static function new(array $guests): self { + return new self($guests); + } + +} diff --git a/modules/opd_presentations/opd_presentations.module b/modules/opd_presentations/opd_presentations.module index e2382f26a..04bd30c72 100644 --- a/modules/opd_presentations/opd_presentations.module +++ b/modules/opd_presentations/opd_presentations.module @@ -2,8 +2,8 @@ declare(strict_types=1); -use Drupal\opd_presentations\Entity\Event; -use Drupal\opd_presentations\Entity\Presentation; +use Drupal\opd_presentations\Event; +use Drupal\opd_presentations\Presentation; /** * Implements hook_entity_bundle_info_alter(). diff --git a/modules/opd_presentations/src/Entity/Event.php b/modules/opd_presentations/src/Event.php similarity index 91% rename from modules/opd_presentations/src/Entity/Event.php rename to modules/opd_presentations/src/Event.php index cedd43c86..b6c7919b5 100644 --- a/modules/opd_presentations/src/Entity/Event.php +++ b/modules/opd_presentations/src/Event.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Drupal\opd_presentations\Entity; +namespace Drupal\opd_presentations; use Drupal\paragraphs\Entity\Paragraph; use Drupal\paragraphs\ParagraphInterface; diff --git a/modules/opd_presentations/src/Collection/EventCollection.php b/modules/opd_presentations/src/Events.php similarity index 53% rename from modules/opd_presentations/src/Collection/EventCollection.php rename to modules/opd_presentations/src/Events.php index f1f271489..4deb9127d 100644 --- a/modules/opd_presentations/src/Collection/EventCollection.php +++ b/modules/opd_presentations/src/Events.php @@ -2,19 +2,17 @@ declare(strict_types=1); -namespace Drupal\opd_presentations\Collection; - -use Drupal\opd_presentations\Entity\Event; +namespace Drupal\opd_presentations; /** * @implements \IteratorAggregate */ -readonly final class EventCollection implements \IteratorAggregate { +readonly final class Events implements \IteratorAggregate { /** * @param Event[] $events */ - public function __construct(private array $events) { + private function __construct(private array $events) { } public function first(): Event { @@ -25,4 +23,11 @@ readonly final class EventCollection implements \IteratorAggregate { return new \ArrayIterator($this->events); } + /** + * @param Event[] $events + */ + public static function new(array $events): self { + return new self($events); + } + } diff --git a/modules/opd_presentations/src/Entity/Presentation.php b/modules/opd_presentations/src/Presentation.php similarity index 66% rename from modules/opd_presentations/src/Entity/Presentation.php rename to modules/opd_presentations/src/Presentation.php index f541626c9..44bbef704 100644 --- a/modules/opd_presentations/src/Entity/Presentation.php +++ b/modules/opd_presentations/src/Presentation.php @@ -2,24 +2,22 @@ declare(strict_types=1); -namespace Drupal\opd_presentations\Entity; +namespace Drupal\opd_presentations; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; -use Drupal\opd_presentations\Collection\EventCollection; -use Drupal\opd_presentations\Entity\Event; final class Presentation extends Node implements NodeInterface { public const NODE_TYPE = 'presentation'; - public function getPastEvents(): EventCollection { + public function getPastEvents(): Events { $events = $this->get('field_events')->referencedEntities(); $today = (new DrupalDateTime('today'))->format('U'); - return new EventCollection(array_filter( + return Events::new(array_filter( array: $events, callback: fn (Event $event): bool => $event->getEventDate() < $today, )); diff --git a/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php b/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php index d1a9316ee..67f554f79 100644 --- a/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php +++ b/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php @@ -7,8 +7,8 @@ namespace Drupal\Tests\opd_presentations\Traits; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Tests\node\Traits\NodeCreationTrait; use Drupal\ctools\Testing\EntityCreationTrait; -use Drupal\opd_presentations\Entity\Event; -use Drupal\opd_presentations\Entity\Presentation; +use Drupal\opd_presentations\Event; +use Drupal\opd_presentations\Presentation; trait PresentationCreationTrait {