63 lines
1.4 KiB
PHP
63 lines
1.4 KiB
PHP
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace Drupal\opd_podcast;
|
||
|
|
||
|
use Drupal\taxonomy\TermInterface;
|
||
|
|
||
|
/**
|
||
|
* @implements \IteratorAggregate<TermInterface>
|
||
|
*/
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
}
|