Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface hasName
- {
- public function getName(): string;
- }
- class Roof implements hasName
- {
- private $name;
- public function __construct(string $name)
- {
- $this->name = $name;
- }
- public function getName(): string
- {
- return $this->name;
- }
- }
- class Wall implements hasName
- {
- private $name;
- public function __construct(string $name)
- {
- $this->name = $name;
- }
- public function getName(): string
- {
- return $this->name;
- }
- }
- class Window implements hasName
- {
- private $name;
- public function __construct(string $name)
- {
- $this->name = $name;
- }
- public function getName(): string
- {
- return $this->name;
- }
- }
- class Door implements hasName
- {
- private $name;
- public function __construct(string $name)
- {
- $this->name = $name;
- }
- public function getName(): string
- {
- return $this->name;
- }
- }
- class LittleDoor extends Door
- {
- }
- class LittleWindow extends Window
- {
- }
- class House
- {
- private $name;
- private $parts;
- public function __construct(string $name, array $parts)
- {
- if ($this->validateName($name)) {
- $this->name = $name;
- }
- if ($this->validateParts($parts)) {
- $this->parts = $parts;
- }
- }
- public function validateParts(array $parts): bool
- {
- foreach ($parts as $part) {
- if (!$part instanceof hasName) {
- throw new InvalidArgumentException('Part is not an instance of hasName');
- }
- }
- return true;
- }
- public function validateName(string $name): bool
- {
- if (strlen($name) <= 3) {
- throw new InvalidArgumentException("Name of the house is too short! ($name)");
- }
- return true;
- }
- }
- try {
- $house = new House('House', [
- new Roof('Roof'),
- new Wall('Wall'),
- new Window('Window'),
- new Door('Door'),
- new LittleDoor('LittleDoor'),
- new LittleWindow('LittleWindow')
- ]);
- echo 'Success' . PHP_EOL;
- } catch (InvalidArgumentException $exception) {
- echo $exception->getMessage();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement