Source of file HTMLTag.php
Size: 5,967 Bytes - Last Modified: 2021-12-24T06:50:04+00:00
/var/www/docs.ssmods.com/process/src/src/view/HTMLTag.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 | <?php namespace gorriecoe\HTMLTag\View; use SilverStripe\View\ViewableData; use SilverStripe\Core\Convert; use InvalidArgumentException; /** * HTMLTag * * @package silverstripe-htmltag */ class HTMLTag extends ViewableData { /** * List of HTML5 void elements * @see https://www.w3.org/TR/html51/syntax.html#void-elements * @var array */ private static $void_elements = [ 'area' => null, 'base' => 'href', 'br' => null, 'col' => null, 'embed' => null, 'hr' => null, 'img' => 'src', 'input' => 'value', 'keygen' => null, 'link' => 'href', 'menuitem' => null, 'meta' => null, 'param' => null, 'source' => null, 'track' => null, 'wbr' => null ]; /** * @var string|null */ protected $string = null; /** * @var string|null */ protected $tag = null; /** * @var array */ protected $attributes = []; /** * @var array */ protected $classes = []; /** * @var string|null */ protected $classPrefix = null; public function __construct($string = null, $tag = null) { $this->string = $string; $this->tag = strtolower($tag); parent::__construct($string); } /** * Defines a HTML tag type * @param string $tag HTML tag type * @return HTMLTag $this */ public function setTag($tag) { $this->tag = strtolower($tag); return $this; } /** * Defines an HTML attribute to add * @param string $name Attribute name * @param string $value Attribute value * @return HTMLTag $this */ public function addAttribute($name = null, $value) { if ($value) { $this->attributes[$name] = $value; } return $this; } /** * @alias addAttribute */ public function setAttribute($name = null, $value) { $this->addAttribute($name, $value); return $this; } /** * Defines an HTML attribute to remove * @param string $name Attribute name * @return HTMLTag $this */ public function removeAttribute($name = null) { unset($this->attributes[$name]); return $this; } /** * Defines a CSS classes * @param string $value CSS class * @return HTMLTag $this */ public function setID($value) { $this->setAttribute('id', $value); return $this; } /** * Defines CSS classes * @param string $value CSS class * @return HTMLTag $this */ public function setClass($value) { $classes = []; foreach (explode(' ', $value) as $class) { $classes[$class] = $class; } $this->classes = $classes; return $this; } /** * Defines a CSS classes to add * @param string $value CSS class * @return HTMLTag $this */ public function addClass($value) { foreach (explode(' ', $value) as $class) { $this->classes[$class] = $class; } return $this; } /** * Defines a CSS classes to remove * @param string $value CSS class * @return HTMLTag $this */ public function removeClass($value) { foreach (explode(' ', $value) as $class) { unset($this->classes[$class]); } return $this; } /** * Defines a CSS class prefix * @param string $value CSS class * @return HTMLTag $this */ public function setPrefix($value) { $this->classPrefix = $value; return $this; } /** * Gets list of void elements * @return array */ public function getVoidElements() { return $this->config()->get('void_elements'); } /** * Returns true if the current tag is a void element * @return boolean */ public function isVoidElement() { return in_array( $this->tag, array_keys($this->VoidElements) ); } /** * Returns the rendered html markup * @return string */ public function Render() { $string = $this->string; $tag = $this->tag; $classes = $this->classes; if (Count($classes)) { if ($classPrefix = $this->classPrefix) { foreach ($classes as $key => $value) { $prefixedClass = $classPrefix . '__' . $value; $classes[$prefixedClass] = $prefixedClass; } } $this->addAttribute('class', implode(' ', $classes)); } if ($this->isVoidElement() && $string) { if ($voidAttributeName = $this->VoidElements[$tag]) { $this->addAttribute($voidAttributeName, $string); } else { throw new InvalidArgumentException("Void element \"{$tag}\" cannot have string"); } } $attributes = []; foreach ($this->attributes as $name => $value) { $attributes[] = sprintf( '%s="%s"', $name, Convert::raw2att($value) ); } $attributes = Count($attributes) ? ' ' . implode(' ', $attributes) : ''; if ($this->isVoidElement()) { return "<{$tag}{$attributes} />"; } if (!isset($string) || !$string || $string == '') { return null; } return "<{$tag}{$attributes}>{$string}</{$tag}>"; } /** * Returns the raw string value without html markup * @return string */ public function Raw() { return $this->string; } /** * Returns the rendered html markup * @return string */ public function forTemplate() { return $this->Render(); } } |