Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- abstract class Tag
- {
- protected $tag;
- protected $children;
- protected $attributes;
- public function printTag()
- {
- echo "<$this->tag ";
- if ($this->attributes != null) {
- foreach ($this->attributes as $prop => $value) {
- echo $prop . '="' . $value . '" ';
- }
- }
- echo '>';
- if (is_array($this->children)) {
- foreach ($this->children as $child) {
- if ($child instanceof Tag) {
- $child->printTag();
- } else if (is_string($child)) {
- echo $child;
- }
- }
- } else if (is_string($this->children)) {
- echo $this->children;
- } else if ($this->children instanceof Tag) {
- $this->children->printTag();
- }
- echo "</$this->tag>";
- }
- }
- class Tag_Builder extends Tag
- {
- public function __construct(string $tag, $children = null, array $attributes = [])
- {
- $this->tag = $tag;
- $this->children = [];
- if (!is_array($children)) {
- $this->addChild($children);
- } else {
- $this->children = $children;
- }
- $this->attributes = $attributes;
- }
- public function getTag(): string
- {
- return $this->tag;
- }
- public function isThisTag(string $tag): bool
- {
- if ($this->tag == $tag) {
- return true;
- }
- return false;
- }
- public function addChild($child, $key = null)
- {
- if (is_null($key)) {
- $this->children[] = $child;
- } else {
- $this->children[$key] = $child;
- }
- }
- public function getChild($key)
- {
- return $this->children[$key];
- }
- public function removeChild($key)
- {
- unset($this->children[$key]);
- }
- public function getChildren()
- {
- $this->children;
- }
- public function addChildren(array $children)
- {
- foreach ($children as $key => $value) {
- $this->children[$key] = $value;
- }
- }
- public function setChildren($children)
- {
- $this->children = $children;
- }
- public function removeChildren(array $keys)
- {
- foreach ($keys as $key) {
- unset($this->children[$key]);
- }
- }
- public function getAttrib(string $key)
- {
- return $this->attributes[$key];
- }
- public function setAttrib(string $key, string $value)
- {
- $this->attributes[$key] = $value;
- }
- public function removeAttrib(string $key)
- {
- unset($this->attribute[$key]);
- }
- public function getAttributes()
- {
- return $this->attributes;
- }
- public function addAttributes(array $attributes)
- {
- foreach ($attributes as $key => $value) {
- $this->attributes[$key] = $value;
- }
- }
- public function setAttributes(array $attributes)
- {
- $this->attributes = $attributes;
- }
- public function removeAttributes(array $keys)
- {
- foreach ($keys as $key) {
- unset($this->attributes[$key]);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement