Advertisement
zukars3

Untitled

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