Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.23 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4. abstract class Tag
  5. {
  6.     protected $tag;
  7.     protected $children;
  8.     protected $attributes;
  9.  
  10.     public function printTag()
  11.     {
  12.         echo "<$this->tag ";
  13.  
  14.         if ($this->attributes != null) {
  15.             foreach ($this->attributes as $prop => $value) {
  16.                 echo $prop . '="' . $value . '" ';
  17.             }
  18.         }
  19.         echo '>';
  20.  
  21.         if (is_array($this->children)) {
  22.             foreach ($this->children as $child) {
  23.  
  24.                 if ($child instanceof Tag) {
  25.                     $child->printTag();
  26.                 } else if (is_string($child)) {
  27.                     echo $child;
  28.                 }
  29.             }
  30.         } else if (is_string($this->children)) {
  31.             echo $this->children;
  32.         } else if ($this->children instanceof Tag) {
  33.             $this->children->printTag();
  34.         }
  35.  
  36.         echo "</$this->tag>";
  37.     }
  38. }
  39.  
  40. class Tag_Builder extends Tag
  41. {
  42.     public function __construct(string $tag, $children = null, array $attributes = [])
  43.     {
  44.         $this->tag = $tag;
  45.  
  46.         $this->children = [];
  47.         if (!is_array($children)) {
  48.             $this->addChild($children);
  49.         } else {
  50.             $this->children = $children;
  51.         }
  52.  
  53.         $this->attributes = $attributes;
  54.     }
  55.     public function getTag(): string
  56.     {
  57.         return $this->tag;
  58.     }
  59.     public function isThisTag(string $tag): bool
  60.     {
  61.         if ($this->tag == $tag) {
  62.             return true;
  63.         }
  64.         return false;
  65.     }
  66.  
  67.     public function addChild($child, $key = null)
  68.     {
  69.         if (is_null($key)) {
  70.             $this->children[] = $child;
  71.         } else {
  72.             $this->children[$key] = $child;
  73.         }
  74.     }
  75.  
  76.     public function getChild($key)
  77.     {
  78.         return $this->children[$key];
  79.     }
  80.     public function removeChild($key)
  81.     {
  82.         unset($this->children[$key]);
  83.     }
  84.  
  85.     public function getChildren()
  86.     {
  87.         $this->children;
  88.     }
  89.     public function addChildren(array $children)
  90.     {
  91.         foreach ($children as $key => $value) {
  92.             $this->children[$key] = $value;
  93.         }
  94.     }
  95.     public function setChildren($children)
  96.     {
  97.         $this->children = $children;
  98.     }
  99.     public function removeChildren(array $keys)
  100.     {
  101.         foreach ($keys as $key) {
  102.             unset($this->children[$key]);
  103.         }
  104.     }
  105.  
  106.  
  107.     public function getAttrib(string $key)
  108.     {
  109.         return $this->attributes[$key];
  110.     }
  111.     public function setAttrib(string $key, string $value)
  112.     {
  113.         $this->attributes[$key] = $value;
  114.     }
  115.     public function removeAttrib(string $key)
  116.     {
  117.         unset($this->attribute[$key]);
  118.     }
  119.  
  120.     public function getAttributes()
  121.     {
  122.         return $this->attributes;
  123.     }
  124.     public function addAttributes(array $attributes)
  125.     {
  126.         foreach ($attributes as $key => $value) {
  127.             $this->attributes[$key] = $value;
  128.         }
  129.     }
  130.     public function setAttributes(array $attributes)
  131.     {
  132.         $this->attributes = $attributes;
  133.     }
  134.     public function removeAttributes(array $keys)
  135.     {
  136.         foreach ($keys as $key) {
  137.             unset($this->attributes[$key]);
  138.         }
  139.     }
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement