Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace classes\tools\html;
- class element {
- private $tag;
- private $html;
- private $attributes;
- private $selfClosing;
- private $selfClosingTags = array(
- 'area',
- 'base',
- 'br',
- 'col',
- 'embed',
- 'hr',
- 'img',
- 'input',
- 'keygen',
- 'link',
- 'menuitem',
- 'meta',
- 'param',
- 'source',
- 'track',
- 'wbr');
- public function __construct($tag=null, $attributes = array(), $html = '', $selfClosing = null) {
- if (!isset($tag))
- throw new \Exception('Tag is a required argument');
- $this->tag = trim(strtolower($tag));
- $this->html = $html;
- if (isset($attributes['class']))
- $attributes['class'] = preg_replace("~\s{2,}~", ' ', $attributes['class']);
- $this->attributes = $attributes;
- $this->selfClosing = $selfClosing;
- $this->selfClosing = isset($this->selfClosing) ? $this->selfClosing : in_array($this->tag, $this->selfClosingTags);
- }
- public function id($id = null) {
- if (isset($id)) {
- $this->attributes['id'] = $id;
- return $this;
- }
- return isset($this->attributes['id']) ? $this->attributes['id'] : null;
- }
- public function html($html = null) {
- if (isset($html)) {
- $this->html = $html;
- return $this;
- }
- return isset($this->html) ? $this->html : null;
- }
- public function val($val = null) {
- if (isset($val)) {
- $this->attributes['value'] = $val;
- return $this;
- }
- return isset($this->attributes['value']) ? $this->attributes['value'] : null;
- }
- public function name($name = null) {
- if (isset($name)) {
- $this->attributes['name'] = $name;
- return $this;
- }
- return isset($this->attributes['name']) ? $this->attributes['name'] : null;
- }
- public function attr($attr = null, $val = null) {
- if (!isset($attr))
- return $this;
- if (isset($val)) {
- $this->attributes[$attr] = $val;
- return $this;
- }
- return isset($this->attributes[$attr]) ? $this->attributes[$attr] : null;
- }
- public function removeAttr($attr = null) {
- if (!isset($attr))
- return $this;
- unset($this->attributes[$attr]);
- return $this;
- }
- public function prop($prop = null, $val = null) {
- if (!isset($prop) || (!isset($val) && !is_bool($val)))
- return $this;
- if ($val)
- $this->attributes[$prop]=$prop;
- else
- unset ($this->attributes[$prop]);
- return $this;
- }
- public function addClass($class = '') {
- $class = trim($class);
- if (isset($this->attributes['class'])) {
- $classList = explode(' ', $this->attributes['class']);
- if (!in_array($class, $classList))
- $this->attributes['class'].=" $class";
- } else
- $this->attributes['class'] = "$class";
- return $this;
- }
- public function removeClass($class = '') {
- $pattern = "~(^$class$|^$class\s|\s$class\s|\s$class$)~";
- $this->attributes['class'] = trim(preg_replace($pattern, ' ', $this->attributes['class']));
- if (empty($this->attributes['class']))
- $this->removeAttr('class');
- return $this;
- }
- public function append($html = '') {
- $this->html.=$html;
- return $this;
- }
- public function prepend($html = '') {
- $this->html = $html . $this->html;
- return $this;
- }
- public function appendTo($element = null) {
- if (!isset($element))
- return $this;
- $element->append($this);
- return $this;
- }
- public function prependTo($element = null) {
- if (!isset($element))
- return $this;
- $element->prepend($this);
- return $this;
- }
- public function __toString() {
- $element = "<$this->tag";
- foreach ($this->attributes as $attribute => $value) {
- $element.= " $attribute=\"$value\"";
- }
- if ($this->selfClosing) {
- $element.="/>";
- return $element;
- }
- $element.=">$this->html</$this->tag>";
- return $element;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement