From 4dcf545ab207c1c4599f84b319cf3a7a5d897dfc Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 22 Jan 2018 09:43:30 +0100 Subject: [PATCH] Html/Attribute: allow boolean, cosmetics... ...and also allow for colons in attribute names --- doc/82-Changelog.md | 4 +++ library/vendor/ipl/Html/Attribute.php | 50 +++++++++++++++++---------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index 47d0a0c7..c29ca10b 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -30,6 +30,10 @@ before switching to a new version. * FEATURE: new Property Modifier to url-encode values * FEATURE: new Property Modifier: uppercase the first character of each word +### Internals +* FEATURE: Html/Attribute now allows boolean properties +* FEATURE: Html/Attribute allows colons in attribute names (required for SVGs) + ### Icinga Configuration * FEATURE: support flapping settings for Icinga >= 2.8.0 (#330) diff --git a/library/vendor/ipl/Html/Attribute.php b/library/vendor/ipl/Html/Attribute.php index dcc26ae2..69a7e986 100644 --- a/library/vendor/ipl/Html/Attribute.php +++ b/library/vendor/ipl/Html/Attribute.php @@ -19,7 +19,7 @@ class Attribute /** @var string */ protected $name; - /** @var string|array */ + /** @var string|array|bool|null */ protected $value; /** @@ -28,7 +28,7 @@ class Attribute * @param $name * @param $value */ - public function __construct($name, $value) + public function __construct($name, $value = null) { $this->setName($name)->setValue($value); } @@ -43,6 +43,14 @@ class Attribute return new static($name, $value); } + /** + * @return string + */ + public function getName() + { + return $this->name; + } + /** * @param $name * @return $this @@ -50,22 +58,15 @@ class Attribute */ public function setName($name) { - if (! preg_match('/^[a-z][a-z-]*$/i', $name)) { + if (! preg_match('/^[a-z][a-z:-]*$/i', $name)) { throw new ProgrammingError( 'Attribute names with special characters are not yet allowed: %s', $name ); } $this->name = $name; - return $this; - } - /** - * @return string - */ - public function getName() - { - return $this->name; + return $this; } /** @@ -77,12 +78,13 @@ class Attribute } /** - * @param string|array $value + * @param mixed $value * @return $this */ public function setValue($value) { $this->value = $value; + return $this; } @@ -93,7 +95,7 @@ class Attribute public function addValue($value) { if (! is_array($this->value)) { - $this->value = array($this->value); + $this->value = [$this->value]; } if (is_array($value)) { @@ -105,16 +107,28 @@ class Attribute return $this; } + /** + * @return bool + */ + public function isBoolean() + { + return is_bool($this->value); + } + /** * @return string */ public function render() { - return sprintf( - '%s="%s"', - $this->renderName(), - $this->renderValue() - ); + if ($this->isBoolean() && $this->value) { + return $this->renderName(); + } else { + return sprintf( + '%s="%s"', + $this->renderName(), + $this->renderValue() + ); + } } /**