simplifying, removing extraneous Flatrr use
This commit is contained in:
parent
d063480d27
commit
82112ba83c
4 changed files with 48 additions and 61 deletions
|
@ -2,9 +2,7 @@
|
||||||
"name": "byjoby/html-object-strings",
|
"name": "byjoby/html-object-strings",
|
||||||
"description": "Abstraction layer for constructing arbitrary HTML tags in PHP",
|
"description": "Abstraction layer for constructing arbitrary HTML tags in PHP",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"require": {
|
"require": {},
|
||||||
"byjoby/flatrr": "dev-master"
|
|
||||||
},
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"authors": [{
|
"authors": [{
|
||||||
"name": "Joby Elliott",
|
"name": "Joby Elliott",
|
||||||
|
|
|
@ -2,46 +2,45 @@
|
||||||
/* HTML Object Strings | https://gitlab.com/byjoby/html-object-strings | MIT License */
|
/* HTML Object Strings | https://gitlab.com/byjoby/html-object-strings | MIT License */
|
||||||
namespace HtmlObjectStrings;
|
namespace HtmlObjectStrings;
|
||||||
|
|
||||||
use Flatrr\FlatArray;
|
class GenericTag implements HtmlInterface
|
||||||
|
|
||||||
class GenericTag extends FlatArray implements HtmlInterface
|
|
||||||
{
|
{
|
||||||
const TAG = 'span';
|
const TAG = 'span';
|
||||||
const SELFCLOSING = false;
|
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();
|
$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()
|
protected function htmlInit()
|
||||||
{
|
{
|
||||||
$this['tag'] = static::TAG;
|
$this->tag = static::TAG;
|
||||||
$this['selfclosing'] = static::SELFCLOSING;
|
$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)
|
public function addClass(string $name)
|
||||||
|
@ -49,50 +48,42 @@ class GenericTag extends FlatArray implements HtmlInterface
|
||||||
if (!$name) {
|
if (!$name) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$classes = $this['classes'];
|
$this->classes[] = $name;
|
||||||
$classes[] = $name;
|
$this->classes = array_unique($this->classes);
|
||||||
$classes = array_unique($classes);
|
sort($this->classes);
|
||||||
sort($classes);
|
|
||||||
unset($this['classes']);
|
|
||||||
$this['classes'] = $classes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasClass(string $name) : bool
|
public function hasClass(string $name) : bool
|
||||||
{
|
{
|
||||||
return in_array($name, $this['classes']);
|
return in_array($name, $this->classes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeClass(string $name)
|
public function removeClass(string $name)
|
||||||
{
|
{
|
||||||
$classes = array_filter(
|
$this->classes = array_filter(
|
||||||
$this['classes'],
|
$this->classes,
|
||||||
function ($e) use ($name) {
|
function ($e) use ($name) {
|
||||||
return $e != $name;
|
return $e != $name;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
sort($classes);
|
sort($this->classes);
|
||||||
unset($this['classes']);
|
|
||||||
$this['classes'] = $classes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function classes() : array
|
public function classes() : array
|
||||||
{
|
{
|
||||||
return $this['classes'];
|
return $this->classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function attr(string $name, $value = null)
|
public function attr(string $name, $value = null)
|
||||||
{
|
{
|
||||||
if ($value === false) {
|
if ($value === false) {
|
||||||
unset($this['attributes.'.$name]);
|
unset($this->attributes[$name]);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if ($value !== null) {
|
if ($value !== null) {
|
||||||
$this['attributes.'.$name] = $value;
|
$this->attributes[$name] = $value;
|
||||||
}
|
}
|
||||||
if (isset($this['attributes.'.$name])) {
|
return @$this->attributes[$name];
|
||||||
return $this['attributes.'.$name];
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function data(string $name, $value = null)
|
public function data(string $name, $value = null)
|
||||||
|
@ -104,7 +95,7 @@ class GenericTag extends FlatArray implements HtmlInterface
|
||||||
{
|
{
|
||||||
$out = '';
|
$out = '';
|
||||||
//build opening tag
|
//build opening tag
|
||||||
$out .= '<'.$this['tag'];
|
$out .= '<'.$this->tag;
|
||||||
//build attributes
|
//build attributes
|
||||||
if ($attr = $this->htmlAttributes()) {
|
if ($attr = $this->htmlAttributes()) {
|
||||||
foreach ($attr as $key => $value) {
|
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
|
//continue t close opening tag and add content and closing tag if needed
|
||||||
if ($this['selfclosing']) {
|
if ($this->selfClosing) {
|
||||||
$out .= ' />';
|
$out .= ' />';
|
||||||
} else {
|
} else {
|
||||||
$out .= '>';
|
$out .= '>';
|
||||||
//build content
|
//build content
|
||||||
$out .= $this->htmlContent();
|
$out .= $this->htmlContent();
|
||||||
//build closing tag
|
//build closing tag
|
||||||
$out .= '</'.$this['tag'].'>';
|
$out .= '</'.$this->tag.'>';
|
||||||
}
|
}
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,7 @@
|
||||||
/* HTML Object Strings | https://gitlab.com/byjoby/html-object-strings | MIT License */
|
/* HTML Object Strings | https://gitlab.com/byjoby/html-object-strings | MIT License */
|
||||||
namespace HtmlObjectStrings;
|
namespace HtmlObjectStrings;
|
||||||
|
|
||||||
use Flatrr\FlatArrayInterface;
|
interface HtmlInterface
|
||||||
|
|
||||||
interface HtmlInterface extends FlatArrayInterface
|
|
||||||
{
|
{
|
||||||
public function attr(string $name, $value = null);
|
public function attr(string $name, $value = null);
|
||||||
public function data(string $name, $value = null);
|
public function data(string $name, $value = null);
|
||||||
|
|
|
@ -63,9 +63,9 @@ class GenericTagTest extends TestCase
|
||||||
configured into the object
|
configured into the object
|
||||||
*/
|
*/
|
||||||
$h = new GenericTag();
|
$h = new GenericTag();
|
||||||
$h['tag'] = 'div';
|
$h->tag = 'div';
|
||||||
$h['selfclosing'] = false;
|
$h->selfClosing = false;
|
||||||
$h['content'] = 'markup content';
|
$h->content = 'markup content';
|
||||||
$h->attr('id', 'h');
|
$h->attr('id', 'h');
|
||||||
$h->data('foo', 'bar');
|
$h->data('foo', 'bar');
|
||||||
$h->addClass('class-foo');
|
$h->addClass('class-foo');
|
||||||
|
|
Loading…
Reference in a new issue