Advertisement
Guest User

Untitled

a guest
Nov 7th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. <?php
  2.  
  3. namespace classes\tools\html;
  4.  
  5. class element {
  6.  
  7. private $tag;
  8. private $html;
  9. private $attributes;
  10. private $selfClosing;
  11. private $selfClosingTags = array(
  12. 'area',
  13. 'base',
  14. 'br',
  15. 'col',
  16. 'embed',
  17. 'hr',
  18. 'img',
  19. 'input',
  20. 'keygen',
  21. 'link',
  22. 'menuitem',
  23. 'meta',
  24. 'param',
  25. 'source',
  26. 'track',
  27. 'wbr');
  28.  
  29. public function __construct($tag=null, $attributes = array(), $html = '', $selfClosing = null) {
  30.  
  31. if (!isset($tag))
  32. throw new \Exception('Tag is a required argument');
  33.  
  34. $this->tag = trim(strtolower($tag));
  35. $this->html = $html;
  36.  
  37. if (isset($attributes['class']))
  38. $attributes['class'] = preg_replace("~\s{2,}~", ' ', $attributes['class']);
  39.  
  40. $this->attributes = $attributes;
  41. $this->selfClosing = $selfClosing;
  42.  
  43. $this->selfClosing = isset($this->selfClosing) ? $this->selfClosing : in_array($this->tag, $this->selfClosingTags);
  44. }
  45.  
  46. public function id($id = null) {
  47. if (isset($id)) {
  48. $this->attributes['id'] = $id;
  49. return $this;
  50. }
  51.  
  52. return isset($this->attributes['id']) ? $this->attributes['id'] : null;
  53. }
  54.  
  55. public function html($html = null) {
  56.  
  57. if (isset($html)) {
  58. $this->html = $html;
  59. return $this;
  60. }
  61.  
  62. return isset($this->html) ? $this->html : null;
  63. }
  64.  
  65. public function val($val = null) {
  66. if (isset($val)) {
  67. $this->attributes['value'] = $val;
  68. return $this;
  69. }
  70.  
  71. return isset($this->attributes['value']) ? $this->attributes['value'] : null;
  72. }
  73.  
  74. public function name($name = null) {
  75. if (isset($name)) {
  76. $this->attributes['name'] = $name;
  77. return $this;
  78. }
  79.  
  80. return isset($this->attributes['name']) ? $this->attributes['name'] : null;
  81. }
  82.  
  83. public function attr($attr = null, $val = null) {
  84.  
  85. if (!isset($attr))
  86. return $this;
  87.  
  88. if (isset($val)) {
  89. $this->attributes[$attr] = $val;
  90. return $this;
  91. }
  92.  
  93. return isset($this->attributes[$attr]) ? $this->attributes[$attr] : null;
  94. }
  95.  
  96. public function removeAttr($attr = null) {
  97.  
  98. if (!isset($attr))
  99. return $this;
  100.  
  101. unset($this->attributes[$attr]);
  102. return $this;
  103. }
  104.  
  105. public function prop($prop = null, $val = null) {
  106. if (!isset($prop) || (!isset($val) && !is_bool($val)))
  107. return $this;
  108.  
  109. if ($val)
  110. $this->attributes[$prop]=$prop;
  111. else
  112. unset ($this->attributes[$prop]);
  113.  
  114. return $this;
  115. }
  116.  
  117. public function addClass($class = '') {
  118.  
  119. $class = trim($class);
  120.  
  121. if (isset($this->attributes['class'])) {
  122. $classList = explode(' ', $this->attributes['class']);
  123. if (!in_array($class, $classList))
  124. $this->attributes['class'].=" $class";
  125. } else
  126. $this->attributes['class'] = "$class";
  127.  
  128. return $this;
  129. }
  130.  
  131. public function removeClass($class = '') {
  132.  
  133. $pattern = "~(^$class$|^$class\s|\s$class\s|\s$class$)~";
  134. $this->attributes['class'] = trim(preg_replace($pattern, ' ', $this->attributes['class']));
  135.  
  136. if (empty($this->attributes['class']))
  137. $this->removeAttr('class');
  138.  
  139. return $this;
  140. }
  141.  
  142. public function append($html = '') {
  143. $this->html.=$html;
  144. return $this;
  145. }
  146.  
  147. public function prepend($html = '') {
  148. $this->html = $html . $this->html;
  149. return $this;
  150. }
  151.  
  152. public function appendTo($element = null) {
  153.  
  154. if (!isset($element))
  155. return $this;
  156.  
  157. $element->append($this);
  158. return $this;
  159. }
  160.  
  161. public function prependTo($element = null) {
  162.  
  163. if (!isset($element))
  164. return $this;
  165.  
  166. $element->prepend($this);
  167. return $this;
  168. }
  169.  
  170. public function __toString() {
  171.  
  172. $element = "<$this->tag";
  173. foreach ($this->attributes as $attribute => $value) {
  174. $element.= " $attribute=\"$value\"";
  175. }
  176. if ($this->selfClosing) {
  177. $element.="/>";
  178. return $element;
  179. }
  180.  
  181. $element.=">$this->html</$this->tag>";
  182.  
  183. return $element;
  184. }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement