git clone https://gist.github.com/26257062f345f7c66e699e7cc9f8eede.git lannister
cd lannister
php index.php
| <?php | |
| namespace Cinema\Cast; | |
| /** | |
| * Family interface | |
| * | |
| * @author Raphael Landas | |
| * @package MyApp | |
| * @copyright 2018 EthicalJobs | |
| * @version 1.0.0 | |
| * @since 1.0.0 | |
| */ | |
| interface CastInterface | |
| { | |
| /** | |
| * Get the first name | |
| * | |
| * @return string | |
| */ | |
| public function getFirstName(); | |
| /** | |
| * Get the last name | |
| * | |
| * @return string | |
| */ | |
| public function getLastName(); | |
| /** | |
| * Get the full name | |
| * | |
| * @return string | |
| */ | |
| public function getName(); | |
| } |
| <?php | |
| namespace Cinema\Cast; | |
| /** | |
| * Cersei Lannister entity model | |
| * | |
| * @author Raphael Landas | |
| * @package Cinema\Cast | |
| * @copyright 2018 EthicalJobs | |
| * @version 1.0.0 | |
| * @since 1.0.0 | |
| */ | |
| class CerseiLannister implements CastInterface, ChildInterface | |
| { | |
| /** | |
| * First name | |
| * | |
| * @var string | |
| */ | |
| private $firstName = 'Cersei'; | |
| /** | |
| * Last name | |
| * | |
| * @var string | |
| */ | |
| private $lastName = 'Lannister'; | |
| /** | |
| * Child | |
| * | |
| * @var | |
| */ | |
| private $child; | |
| /** | |
| * CerseiLannister constructor. | |
| * | |
| * @param CastInterface $child | |
| */ | |
| public function __construct(CastInterface $child) | |
| { | |
| $this->child = $child; | |
| } | |
| /** | |
| * Get the sfirst name | |
| * | |
| * @return string | |
| */ | |
| public function getFirstName() | |
| { | |
| return $this->firstName; | |
| } | |
| /** | |
| * Get the last name | |
| * | |
| * @return string | |
| */ | |
| public function getLastName() | |
| { | |
| return $this->lastName; | |
| } | |
| /** | |
| * Get the full name | |
| * | |
| * @return string | |
| */ | |
| public function getName() | |
| { | |
| return $this->firstName . ' ' . $this->lastName; | |
| } | |
| /** | |
| * Get the child entity | |
| * | |
| * @return CastInterface | |
| */ | |
| public function getChild() | |
| { | |
| return $this->child; | |
| } | |
| /** | |
| * Set the child entity | |
| * | |
| * @param CastInterface $child | |
| * @return $this | |
| */ | |
| public function setChild(CastInterface $child) | |
| { | |
| $this->child = $child; | |
| return $this; | |
| } | |
| } |
| <?php | |
| namespace Cinema\Cast; | |
| /** | |
| * Child interface | |
| * | |
| * @author Raphael Landas | |
| * @package MyApp | |
| * @copyright 2018 EthicalJob | |
| * @version 1.0.0 | |
| * @since 1.0.0 | |
| */ | |
| interface ChildInterface | |
| { | |
| /** | |
| * Get the child entity | |
| * | |
| * @return CastInterface | |
| */ | |
| public function getChild(); | |
| } |
| <?php | |
| namespace Cinema\Cast\Families; | |
| use Cinema\Cast\CastInterface; | |
| /** | |
| * Family interface | |
| * | |
| * @author Raphael Landas | |
| * @package Cinema\Cast\Families | |
| * @copyright 2018 EthicalJobs | |
| * @version 1.0.0 | |
| * @since 1.0.0 | |
| */ | |
| interface FamilyInterface | |
| { | |
| /** | |
| * Add a family member | |
| * | |
| * @param string $name | |
| * @param CastInterface $member | |
| * @return $this | |
| */ | |
| public function addMember($name, CastInterface $member); | |
| } |
| <?php | |
| namespace MyApp; | |
| use Cinema\Cast\Families; | |
| use Cinema\Cast; | |
| $theLannisters = new Families\TheLannisters(); | |
| $theLannisters->addMember('jaime', new Cast\Jaime()) | |
| ->addMember('joanna', new Cast\JoannaLannister()) | |
| ->addMember('cersei', new Cast\CerseiLannister((new Cast\JoannaLannister()))); | |
| $familyNames = $theLannisters->getFamilyNames(); | |
| echo "The Lannisters:" . PHP_EOL; | |
| foreach ($familyNames as $familyName) { | |
| echo $familyName . PHP_EOL; | |
| } |
| <?php | |
| namespace Cinema\Cast; | |
| /** | |
| * Jaime Lannister entity model | |
| * | |
| * @author Raphael Landas | |
| * @package Cinema\Cast | |
| * @copyright 2018 EthicalJobs | |
| * @version 1.0.0 | |
| * @since 1.0.0 | |
| */ | |
| class Jaime implements CastInterface | |
| { | |
| /** | |
| * First name | |
| * | |
| * @var string | |
| */ | |
| private $firstName = 'Jaime'; | |
| /** | |
| * Last name | |
| * | |
| * @var string | |
| */ | |
| private $lastName = 'Lannister'; | |
| /** | |
| * Get the sfirst name | |
| * | |
| * @return string | |
| */ | |
| public function getFirstName() | |
| { | |
| return $this->firstName; | |
| } | |
| /** | |
| * Get the last name | |
| * | |
| * @return string | |
| */ | |
| public function getLastName() | |
| { | |
| return $this->lastName; | |
| } | |
| /** | |
| * Get the full name | |
| * | |
| * @return string | |
| */ | |
| public function getName() | |
| { | |
| return $this->firstName . ' ' . $this->lastName; | |
| } | |
| } |
| <?php | |
| namespace Cinema\Cast; | |
| /** | |
| * Joann Lannister entity model | |
| * | |
| * @author Raphael Landas | |
| * @package Cinema\Cast | |
| * @copyright 2018 EthicalJobs | |
| * @version 1.0.0 | |
| * @since 1.0.0 | |
| */ | |
| class JoannaLannister implements CastInterface | |
| { | |
| /** | |
| * First name | |
| * | |
| * @var string | |
| */ | |
| private $firstName = 'Joanna'; | |
| /** | |
| * Last name | |
| * | |
| * @var string | |
| */ | |
| private $lastName = 'Lannister'; | |
| /** | |
| * Get the sfirst name | |
| * | |
| * @return string | |
| */ | |
| public function getFirstName() | |
| { | |
| return $this->firstName; | |
| } | |
| /** | |
| * Get the last name | |
| * | |
| * @return string | |
| */ | |
| public function getLastName() | |
| { | |
| return $this->lastName; | |
| } | |
| /** | |
| * Get the full name | |
| * | |
| * @return string | |
| */ | |
| public function getName() | |
| { | |
| return $this->firstName . ' ' . $this->lastName; | |
| } | |
| } |
| <?php | |
| namespace Cinema\Cast\Families; | |
| use Cinema\Cast\CastInterface; | |
| use Cinema\Cast\ChildInterface; | |
| /** | |
| * The Lannister family entity model | |
| * | |
| * @author Raphael Landas | |
| * @package Cinema\Case\Families | |
| * @copyright 2018 EthicalJobs | |
| * @version 1.0.0 | |
| * @since 1.0.0 | |
| */ | |
| class TheLannisters implements FamilyInterface | |
| { | |
| /** | |
| * Members collection | |
| * | |
| * @var array | |
| */ | |
| private $members = []; | |
| /** | |
| * Add member to the family | |
| * | |
| * @param string $name | |
| * @param CastInterface $member | |
| * @return self | |
| */ | |
| public function addMember($name, CastInterface $member) | |
| { | |
| $this->members[$name] = v; | |
| return $this; | |
| } | |
| /** | |
| * Get all family names | |
| * | |
| * @return array | |
| */ | |
| public function getFamilyNames() | |
| { | |
| $output = []; | |
| foreach ($this->members as $member) { | |
| $output[] = $member->getName(); | |
| if ($member instanceof ChildInterface) { | |
| $output[] = $member->getChild() | |
| ->getName(); | |
| } | |
| } | |
| return $output; | |
| } | |
| } |