Advertisement
zukars3

Untitled

Mar 28th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.56 KB | None | 0 0
  1. <?php
  2.  
  3. interface HousePartInterface
  4. {
  5.     public function getName(): string;
  6. }
  7.  
  8. abstract class NameValidator
  9. {
  10.     protected function validateName(string $name): void
  11.     {
  12.         if (strlen($name) <= 3) {
  13.             throw new InvalidArgumentException("Name is too short! ($name)");
  14.         }
  15.     }
  16. }
  17.  
  18. class Roof extends NameValidator implements HousePartInterface
  19. {
  20.     private $name;
  21.  
  22.     public function __construct(string $name)
  23.     {
  24.         $this->validateName($name);
  25.         $this->name = $name;
  26.     }
  27.  
  28.     public function getName(): string
  29.     {
  30.         return $this->name;
  31.     }
  32. }
  33.  
  34. class Wall extends NameValidator implements HousePartInterface
  35. {
  36.     private $name;
  37.  
  38.     public function __construct(string $name)
  39.     {
  40.         $this->validateName($name);
  41.         $this->name = $name;
  42.     }
  43.  
  44.     public function getName(): string
  45.     {
  46.         return $this->name;
  47.     }
  48. }
  49.  
  50. class Window extends NameValidator implements HousePartInterface
  51. {
  52.     private $name;
  53.  
  54.     public function __construct(string $name)
  55.     {
  56.         $this->validateName($name);
  57.         $this->name = $name;
  58.     }
  59.  
  60.     public function getName(): string
  61.     {
  62.         return $this->name;
  63.     }
  64. }
  65.  
  66. class Door extends NameValidator implements HousePartInterface
  67. {
  68.     private $name;
  69.  
  70.     public function __construct(string $name)
  71.     {
  72.         $this->validateName($name);
  73.         $this->name = $name;
  74.     }
  75.  
  76.     public function getName(): string
  77.     {
  78.         return $this->name;
  79.     }
  80. }
  81.  
  82. class LittleDoor extends Door
  83. {
  84.  
  85. }
  86.  
  87. class LittleWindow extends Window
  88. {
  89.  
  90. }
  91.  
  92. class House extends NameValidator
  93. {
  94.     private $name;
  95.     private $parts;
  96.  
  97.     public function __construct(string $name, array $parts)
  98.     {
  99.         $this->validateName($name);
  100.         $this->validateName($name);
  101.         $this->validateParts($parts);
  102.         $this->name = $name;
  103.         $this->parts = $parts;
  104.  
  105.     }
  106.  
  107.     private function validateParts(array $parts): void
  108.     {
  109.         foreach ($parts as $part) {
  110.             if (!$part instanceof HousePartInterface) {
  111.                 throw new InvalidArgumentException('Part is not a part of house');
  112.             }
  113.         }
  114.     }
  115.  
  116. }
  117.  
  118. try {
  119.  
  120.     $house = new House('House', [
  121.         new Roof('Roof'),
  122.         new Wall('Wall'),
  123.         new Window('Window'),
  124.         new Door('Door'),
  125.         new LittleDoor('LittleDoor'),
  126.         new LittleWindow('LittleWindow')
  127.     ]);
  128.  
  129.     echo 'Success' . PHP_EOL;
  130. } catch (InvalidArgumentException $exception) {
  131.  
  132.     echo $exception->getMessage();
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement