From 82112ba83c3a3a24eee0a607e091e4c044534dc4 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Thu, 23 Aug 2018 20:09:00 -0600 Subject: [PATCH] simplifying, removing extraneous Flatrr use --- composer.json | 4 +- src/GenericTag.php | 95 ++++++++++++++++++---------------------- src/HtmlInterface.php | 4 +- tests/GenericTagTest.php | 6 +-- 4 files changed, 48 insertions(+), 61 deletions(-) diff --git a/composer.json b/composer.json index 01c4f8c..f4a8694 100644 --- a/composer.json +++ b/composer.json @@ -2,9 +2,7 @@ "name": "byjoby/html-object-strings", "description": "Abstraction layer for constructing arbitrary HTML tags in PHP", "type": "library", - "require": { - "byjoby/flatrr": "dev-master" - }, + "require": {}, "license": "MIT", "authors": [{ "name": "Joby Elliott", diff --git a/src/GenericTag.php b/src/GenericTag.php index b5978e4..562c24a 100644 --- a/src/GenericTag.php +++ b/src/GenericTag.php @@ -2,46 +2,45 @@ /* HTML Object Strings | https://gitlab.com/byjoby/html-object-strings | MIT License */ namespace HtmlObjectStrings; -use Flatrr\FlatArray; - -class GenericTag extends FlatArray implements HtmlInterface +class GenericTag implements HtmlInterface { const TAG = 'span'; const SELFCLOSING = false; - public function __construct(array $data = null) + public $tag = 'span'; + public $selfClosing = false; + public $content = null; + + protected $classes = []; + protected $attributes = []; + + public function __construct() { - parent::__construct($data); - $this->merge([ - 'classes' => [], - 'attributes' => [] - ]); $this->htmlInit(); } - protected function htmlContent() - { - if (is_array($this['content'])) { - $content = implode(PHP_EOL, $this['content']); - } else { - $content = $this['content']; - } - return $content; - } - - protected function htmlAttributes() - { - $attr = $this['attributes']; - if ($this->classes()) { - $attr['class'] = implode(' ', $this->classes()); - } - return $attr; - } - protected function htmlInit() { - $this['tag'] = static::TAG; - $this['selfclosing'] = static::SELFCLOSING; + $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) @@ -49,50 +48,42 @@ class GenericTag extends FlatArray implements HtmlInterface if (!$name) { return; } - $classes = $this['classes']; - $classes[] = $name; - $classes = array_unique($classes); - sort($classes); - unset($this['classes']); - $this['classes'] = $classes; + $this->classes[] = $name; + $this->classes = array_unique($this->classes); + sort($this->classes); } public function hasClass(string $name) : bool { - return in_array($name, $this['classes']); + return in_array($name, $this->classes); } public function removeClass(string $name) { - $classes = array_filter( - $this['classes'], + $this->classes = array_filter( + $this->classes, function ($e) use ($name) { return $e != $name; } ); - sort($classes); - unset($this['classes']); - $this['classes'] = $classes; + sort($this->classes); } public function classes() : array { - return $this['classes']; + return $this->classes; } public function attr(string $name, $value = null) { if ($value === false) { - unset($this['attributes.'.$name]); + unset($this->attributes[$name]); return null; } if ($value !== null) { - $this['attributes.'.$name] = $value; + $this->attributes[$name] = $value; } - if (isset($this['attributes.'.$name])) { - return $this['attributes.'.$name]; - } - return null; + return @$this->attributes[$name]; } public function data(string $name, $value = null) @@ -104,7 +95,7 @@ class GenericTag extends FlatArray implements HtmlInterface { $out = ''; //build opening tag - $out .= '<'.$this['tag']; + $out .= '<'.$this->tag; //build attributes if ($attr = $this->htmlAttributes()) { foreach ($attr as $key => $value) { @@ -117,14 +108,14 @@ class GenericTag extends FlatArray implements HtmlInterface } } //continue t close opening tag and add content and closing tag if needed - if ($this['selfclosing']) { + if ($this->selfClosing) { $out .= ' />'; } else { $out .= '>'; //build content $out .= $this->htmlContent(); //build closing tag - $out .= ''; + $out .= 'tag.'>'; } return $out; } diff --git a/src/HtmlInterface.php b/src/HtmlInterface.php index 49ec7e7..8b4d149 100644 --- a/src/HtmlInterface.php +++ b/src/HtmlInterface.php @@ -2,9 +2,7 @@ /* HTML Object Strings | https://gitlab.com/byjoby/html-object-strings | MIT License */ namespace HtmlObjectStrings; -use Flatrr\FlatArrayInterface; - -interface HtmlInterface extends FlatArrayInterface +interface HtmlInterface { public function attr(string $name, $value = null); public function data(string $name, $value = null); diff --git a/tests/GenericTagTest.php b/tests/GenericTagTest.php index a531fe7..83faf26 100644 --- a/tests/GenericTagTest.php +++ b/tests/GenericTagTest.php @@ -63,9 +63,9 @@ class GenericTagTest extends TestCase configured into the object */ $h = new GenericTag(); - $h['tag'] = 'div'; - $h['selfclosing'] = false; - $h['content'] = 'markup content'; + $h->tag = 'div'; + $h->selfClosing = false; + $h->content = 'markup content'; $h->attr('id', 'h'); $h->data('foo', 'bar'); $h->addClass('class-foo');