Skip to content

Commit

Permalink
style object, fixes and upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
hisan92 committed Aug 2, 2021
1 parent c609ad2 commit 9a52dd5
Show file tree
Hide file tree
Showing 9 changed files with 1,375 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hisan/htmlbuilder",
"description": "Lightweight HTML Builder for PHP",
"version": "0.0.1-dev",
"version": "0.1.1-dev",
"type": "library",
"license": "MIT",
"keywords": ["xml", "html", "html5", "dom", "dom-builder", "html-builder"],
Expand Down
2 changes: 1 addition & 1 deletion src/HTML/Exceptions/InvalidElementException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HTMLBuilder\HTML\Exceptions;

class InvalidElementException extends \Exception
class InvalidElementException extends \RuntimeException
{
protected $message = 'The provided element tag is not a valid HTML Element.';
}
8 changes: 8 additions & 0 deletions src/HTML/Exceptions/InvalidStylePropertyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace HTMLBuilder\HTML\Exceptions;

class InvalidStylePropertyException extends \RuntimeException
{
protected $message = 'The provided property is not valid.';
}
32 changes: 27 additions & 5 deletions src/HTML/Node/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ protected function __construct($tag, $options = [], ...$children)
{
$this->id = static::hashid();
$this->tag = $tag;
$this->style = new Style;
$this->children = new Collection;
$this->validAttrs = $options['props']['validAttrs'];
$this->obsoleteAttrs = $options['props']['obsoleteAttrs'];
Expand All @@ -121,6 +120,15 @@ protected function __construct($tag, $options = [], ...$children)
if (is_string($options['classes']))
$this->classes = explode(' ', $options['classes']);
}

if (isset($options['style'])) {
if ($options['style'] instanceof Style)
$this->style = $options['style'];
else
$this->style = new Style($options['style']);
} else {
$this->style = new Style;
}
}

public function __clone()
Expand All @@ -132,10 +140,12 @@ public function __clone()
public function __toString()
{
return "<{$this->tag}"
. implode(' ', $this->attributes($this->data, 'data-%s="%s"'))
. implode(' ', $this->attributes($this->attributes))
. (count($this->classes)
? sprintf(' class="%s"', $this->classes()) : '')
. implode(' ', $this->attributes($this->data, 'data-%s="%s"'))
. implode(' ', $this->attributes($this->attributes)) . '>'
. ($this->style->isDefined()
? sprintf(' style="%s"', (string) $this->style) : '') . '>'
. (string) $this->children
. (! static::selfClosed ? "</{$this->tag}>" : '')
;
Expand All @@ -156,7 +166,7 @@ public static function create($tag, $options = [], ...$children)
{
$validElements = static::getValidElements();

throw_if(! isset($validElements[$tag]), InvalidElementException::class);
throw_unless(isset($validElements[$tag]), InvalidElementException::class);

$props = $validElements[$tag];

Expand Down Expand Up @@ -285,10 +295,22 @@ public function classes()
}

/**
* Get Style instance.
*
* @return \HTMLBuilder\HTML\Property\Style
*/
public function style()
{
return $this->style;
}

/**
* Get/set data-{key} property.
*
* @param string $name
* @param string|int $value
*
* @return $this
* @return $this|string
*/
public function data($name, $value = null)
{
Expand Down
Loading

0 comments on commit 9a52dd5

Please sign in to comment.