diff --git a/README.md b/README.md index f13a9ad..c653f8c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # html-object-strings +An HTML string-building library, used for building HTML strings from simple and easy-to-use objects. diff --git a/src/GenericTag.php b/src/GenericTag.php index 562c24a..fa8a561 100644 --- a/src/GenericTag.php +++ b/src/GenericTag.php @@ -2,18 +2,13 @@ /* HTML Object Strings | https://gitlab.com/byjoby/html-object-strings | MIT License */ namespace HtmlObjectStrings; -class GenericTag implements HtmlInterface +class GenericTag implements TagInterface { + use TagTrait; + const TAG = 'span'; const SELFCLOSING = false; - public $tag = 'span'; - public $selfClosing = false; - public $content = null; - - protected $classes = []; - protected $attributes = []; - public function __construct() { $this->htmlInit(); @@ -24,104 +19,4 @@ class GenericTag implements HtmlInterface $this->tag = static::TAG; $this->selfClosing = static::SELFCLOSING; } - - protected function htmlContent() - { - if (is_array($this->content)) { - return implode(PHP_EOL, $this->content); - } else { - return $this->content; - } - } - - protected function htmlAttributes() - { - $attr = $this->attributes; - if ($this->classes()) { - $attr['class'] = implode(' ', $this->classes()); - } - return $attr; - } - - public function addClass(string $name) - { - if (!$name) { - return; - } - $this->classes[] = $name; - $this->classes = array_unique($this->classes); - sort($this->classes); - } - - public function hasClass(string $name) : bool - { - return in_array($name, $this->classes); - } - - public function removeClass(string $name) - { - $this->classes = array_filter( - $this->classes, - function ($e) use ($name) { - return $e != $name; - } - ); - sort($this->classes); - } - - public function classes() : array - { - return $this->classes; - } - - public function attr(string $name, $value = null) - { - if ($value === false) { - unset($this->attributes[$name]); - return null; - } - if ($value !== null) { - $this->attributes[$name] = $value; - } - return @$this->attributes[$name]; - } - - public function data(string $name, $value = null) - { - return $this->attr("data-$name", $value); - } - - public function string() : string - { - $out = ''; - //build opening tag - $out .= '<'.$this->tag; - //build attributes - if ($attr = $this->htmlAttributes()) { - foreach ($attr as $key => $value) { - if (!"$value") { - $out .= " $key"; - } else { - $value = htmlspecialchars($value); - $out .= " $key=\"$value\""; - } - } - } - //continue t close opening tag and add content and closing tag if needed - if ($this->selfClosing) { - $out .= ' />'; - } else { - $out .= '>'; - //build content - $out .= $this->htmlContent(); - //build closing tag - $out .= 'tag.'>'; - } - return $out; - } - - public function __toString() - { - return $this->string(); - } } diff --git a/src/HtmlInterface.php b/src/TagInterface.php similarity index 95% rename from src/HtmlInterface.php rename to src/TagInterface.php index 8b4d149..b52867d 100644 --- a/src/HtmlInterface.php +++ b/src/TagInterface.php @@ -2,7 +2,7 @@ /* HTML Object Strings | https://gitlab.com/byjoby/html-object-strings | MIT License */ namespace HtmlObjectStrings; -interface HtmlInterface +interface TagInterface { public function attr(string $name, $value = null); public function data(string $name, $value = null); diff --git a/src/TagTrait.php b/src/TagTrait.php new file mode 100644 index 0000000..430bf14 --- /dev/null +++ b/src/TagTrait.php @@ -0,0 +1,113 @@ +content)) { + return implode(PHP_EOL, $this->content); + } else { + return $this->content; + } + } + + protected function htmlAttributes() + { + $attr = $this->attributes; + if ($this->classes()) { + $attr['class'] = implode(' ', $this->classes()); + } + return $attr; + } + + public function addClass(string $name) + { + if (!$name) { + return; + } + $this->classes[] = $name; + $this->classes = array_unique($this->classes); + sort($this->classes); + } + + public function hasClass(string $name) : bool + { + return in_array($name, $this->classes); + } + + public function removeClass(string $name) + { + $this->classes = array_filter( + $this->classes, + function ($e) use ($name) { + return $e != $name; + } + ); + sort($this->classes); + } + + public function classes() : array + { + return $this->classes; + } + + public function attr(string $name, $value = null) + { + if ($value === false) { + unset($this->attributes[$name]); + return null; + } + if ($value !== null) { + $this->attributes[$name] = $value; + } + return @$this->attributes[$name]; + } + + public function data(string $name, $value = null) + { + return $this->attr("data-$name", $value); + } + + public function string() : string + { + $out = ''; + //build opening tag + $out .= '<'.$this->tag; + //build attributes + if ($attr = $this->htmlAttributes()) { + foreach ($attr as $key => $value) { + if (!"$value") { + $out .= " $key"; + } else { + $value = htmlspecialchars($value); + $out .= " $key=\"$value\""; + } + } + } + //continue t close opening tag and add content and closing tag if needed + if ($this->selfClosing) { + $out .= ' />'; + } else { + $out .= '>'; + //build content + $out .= $this->htmlContent(); + //build closing tag + $out .= 'tag.'>'; + } + return $out; + } + + public function __toString() + { + return $this->string(); + } +}