Refactor
This commit is contained in:
parent
9308ce76a5
commit
51299096c5
5 changed files with 48 additions and 29 deletions
18
modules/opd_podcast/src/Episode.php
Normal file
18
modules/opd_podcast/src/Episode.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_podcast;
|
||||
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
final class Episode extends Node implements NodeInterface {
|
||||
|
||||
public const NODE_TYPE = 'podcast_episode';
|
||||
|
||||
public function getGuests(): Guests {
|
||||
return Guests::new($this->get('field_podcast_guests')->referencedEntities());
|
||||
}
|
||||
|
||||
}
|
14
modules/opd_podcast/src/Guest.php
Normal file
14
modules/opd_podcast/src/Guest.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_podcast;
|
||||
|
||||
use Drupal\taxonomy\Entity\Term;
|
||||
use Drupal\taxonomy\TermInterface;
|
||||
|
||||
final class Guest extends Term implements TermInterface {
|
||||
|
||||
public const TERM_TYPE = 'podcast_guest';
|
||||
|
||||
}
|
|
@ -7,12 +7,12 @@ namespace Drupal\opd_podcast;
|
|||
use Drupal\taxonomy\TermInterface;
|
||||
|
||||
/**
|
||||
* @implements \IteratorAggregate<TermInterface>
|
||||
* @implements \IteratorAggregate<Guest>
|
||||
*/
|
||||
final class PodcastGuests implements \ArrayAccess, \Countable, \IteratorAggregate, \Stringable {
|
||||
final class Guests implements \ArrayAccess, \Countable, \IteratorAggregate, \Stringable {
|
||||
|
||||
/**
|
||||
* @param TermInterface[] $guests
|
||||
* @param Guest[] $guests
|
||||
*/
|
||||
private function __construct(private array $guests) {
|
||||
}
|
||||
|
@ -44,16 +44,16 @@ final class PodcastGuests implements \ArrayAccess, \Countable, \IteratorAggregat
|
|||
public function __toString(): string {
|
||||
// TODO: allow for more than two guests.
|
||||
if (count($this->guests) === 2) {
|
||||
assert($this->guests[1] instanceof TermInterface);
|
||||
assert($this->guests[1] instanceof Guest);
|
||||
|
||||
return sprintf('%s %s %s', $this->guests[0]->label(), t('and'), $this->guests[1]->label());
|
||||
return sprintf('%s %s %s', $this->guests[0]->getName(), t('and'), $this->guests[1]->getName());
|
||||
}
|
||||
|
||||
return strval($this->guests[0]->label());
|
||||
return strval($this->guests[0]->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TermInterface[] $guests
|
||||
* @param Guest[] $guests
|
||||
*/
|
||||
public static function new(array $guests): self {
|
||||
return new self($guests);
|
|
@ -1,18 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_podcast;
|
||||
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
final class PodcastEpisode extends Node implements NodeInterface {
|
||||
|
||||
public const NODE_TYPE = 'podcast_episode';
|
||||
|
||||
public function getGuests(): PodcastGuests {
|
||||
return PodcastGuests::new($this->get('field_podcast_guests')->referencedEntities());
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue