55 lines
1.1 KiB
PHP
55 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Drupal\opd_podcast;
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \IteratorAggregate<Guest>
|
|
*/
|
|
final class Guests implements \Countable, \IteratorAggregate, \Stringable {
|
|
|
|
public function __toString(): string {
|
|
// TODO: allow for more than two guests.
|
|
if ($this->count() === 2) {
|
|
assert($this->first() instanceof Guest);
|
|
|
|
return sprintf('%s %s %s', $this->first()->getName(), t('and'), $this->get(1)->getName());
|
|
}
|
|
|
|
return strval($this->first()->getName());
|
|
}
|
|
|
|
public function count(): int {
|
|
return count($this->guests);
|
|
}
|
|
|
|
public function first(): ?Guest {
|
|
return array_values($this->guests)[0];
|
|
}
|
|
|
|
public function getIterator(): \Traversable {
|
|
return new \ArrayIterator($this->guests);
|
|
}
|
|
|
|
/**
|
|
* @param Guest[] $guests
|
|
*/
|
|
public static function fromGuests(array $guests): self {
|
|
return new self($guests);
|
|
}
|
|
|
|
/**
|
|
* @param Guest[] $guests
|
|
*/
|
|
private function __construct(private array $guests) {
|
|
Assert::allIsInstanceOf($guests, Guest::class);
|
|
}
|
|
|
|
private function get(int $offset): ?Guest {
|
|
return $this->guests[$offset];
|
|
}
|
|
|
|
}
|