Refactor
Try a different way of naming and grouping classes.
This commit is contained in:
parent
52c1b33711
commit
6b6b362a49
15 changed files with 111 additions and 59 deletions
|
@ -5,8 +5,7 @@ declare(strict_types=1);
|
|||
use Drupal\Core\Render\BubbleableMetadata;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\opd_podcast\Entity\PodcastEpisode;
|
||||
use Drupal\taxonomy\TermInterface;
|
||||
use Drupal\opd_podcast\PodcastEpisode;
|
||||
|
||||
/**
|
||||
* Implements hook_entity_bundle_info_alter().
|
||||
|
@ -86,20 +85,7 @@ function opd_podcast_tokens(string $type, array $tokens, array $data, array $opt
|
|||
$node = $data['node'] ?? NULL;
|
||||
assert($node instanceof PodcastEpisode);
|
||||
|
||||
$guests = $node->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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
62
modules/opd_podcast/src/PodcastGuests.php
Normal file
62
modules/opd_podcast/src/PodcastGuests.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue