oliverdavies.uk/modules/opd_podcast/src/Guests.php

49 lines
991 B
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Drupal\opd_podcast;
/**
2025-06-12 02:10:08 +01:00
* @implements \IteratorAggregate<Guest>
*/
2025-06-12 02:10:08 +01:00
final class Guests implements \Countable, \IteratorAggregate, \Stringable {
/**
2025-06-12 02:10:08 +01:00
* @param Guest[] $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 __toString(): string {
// TODO: allow for more than two guests.
2025-06-12 02:10:08 +01:00
if ($this->count() === 2) {
assert($this->get(0) instanceof Guest);
2025-06-12 02:10:08 +01:00
return sprintf('%s %s %s', $this->get(0)->getName(), t('and'), $this->get(1)->getName());
}
2025-06-12 02:10:08 +01:00
return strval($this->get(0)->getName());
}
/**
2025-06-12 02:10:08 +01:00
* @param Guest[] $guests
*/
public static function new(array $guests): self {
return new self($guests);
}
2025-06-12 02:10:08 +01:00
private function get(int $offset): ?Guest {
return $this->guests[$offset];
}
}