moved a lot of behavior to a trait

This commit is contained in:
Joby Elliott 2018-08-23 20:45:37 -06:00
parent 82112ba83c
commit 7b57cb4ee1
4 changed files with 118 additions and 109 deletions

View file

@ -1,2 +1,3 @@
# html-object-strings
An HTML string-building library, used for building HTML strings from simple and easy-to-use objects.

View file

@ -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 .= '</'.$this->tag.'>';
}
return $out;
}
public function __toString()
{
return $this->string();
}
}

View file

@ -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);

113
src/TagTrait.php Normal file
View file

@ -0,0 +1,113 @@
<?php
/* HTML Object Strings | https://gitlab.com/byjoby/html-object-strings | MIT License */
namespace HtmlObjectStrings;
trait TagTrait
{
public $tag = 'span';
public $selfClosing = false;
public $content = null;
protected $classes = [];
protected $attributes = [];
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 .= '</'.$this->tag.'>';
}
return $out;
}
public function __toString()
{
return $this->string();
}
}