Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface HousePartInterface
- {
- public function getName(): string;
- }
- abstract class NameValidator
- {
- protected function validateName(string $name): void
- {
- if (strlen($name) <= 3) {
- throw new InvalidArgumentException("Name is too short! ($name)");
- }
- }
- }
- class Roof extends NameValidator implements HousePartInterface
- {
- private $name;
- public function __construct(string $name)
- {
- $this->validateName($name);
- $this->name = $name;
- }
- public function getName(): string
- {
- return $this->name;
- }
- }
- class Wall extends NameValidator implements HousePartInterface
- {
- private $name;
- public function __construct(string $name)
- {
- $this->validateName($name);
- $this->name = $name;
- }
- public function getName(): string
- {
- return $this->name;
- }
- }
- class Window extends NameValidator implements HousePartInterface
- {
- private $name;
- public function __construct(string $name)
- {
- $this->validateName($name);
- $this->name = $name;
- }
- public function getName(): string
- {
- return $this->name;
- }
- }
- class Door extends NameValidator implements HousePartInterface
- {
- private $name;
- public function __construct(string $name)
- {
- $this->validateName($name);
- $this->name = $name;
- }
- public function getName(): string
- {
- return $this->name;
- }
- }
- class LittleDoor extends Door
- {
- }
- class LittleWindow extends Window
- {
- }
- class House extends NameValidator
- {
- private $name;
- private $parts;
- public function __construct(string $name, array $parts)
- {
- $this->validateName($name);
- $this->validateName($name);
- $this->validateParts($parts);
- $this->name = $name;
- $this->parts = $parts;
- }
- private function validateParts(array $parts): void
- {
- foreach ($parts as $part) {
- if (!$part instanceof HousePartInterface) {
- throw new InvalidArgumentException('Part is not a part of house');
- }
- }
- }
- }
- 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