Html/Attribute: allow boolean, cosmetics...

...and also allow for colons in attribute names
This commit is contained in:
Thomas Gelf 2018-01-22 09:43:30 +01:00
parent 01787cf748
commit 4dcf545ab2
2 changed files with 36 additions and 18 deletions

View File

@ -30,6 +30,10 @@ before switching to a new version.
* FEATURE: new Property Modifier to url-encode values * FEATURE: new Property Modifier to url-encode values
* FEATURE: new Property Modifier: uppercase the first character of each word * 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 ### Icinga Configuration
* FEATURE: support flapping settings for Icinga >= 2.8.0 (#330) * FEATURE: support flapping settings for Icinga >= 2.8.0 (#330)

View File

@ -19,7 +19,7 @@ class Attribute
/** @var string */ /** @var string */
protected $name; protected $name;
/** @var string|array */ /** @var string|array|bool|null */
protected $value; protected $value;
/** /**
@ -28,7 +28,7 @@ class Attribute
* @param $name * @param $name
* @param $value * @param $value
*/ */
public function __construct($name, $value) public function __construct($name, $value = null)
{ {
$this->setName($name)->setValue($value); $this->setName($name)->setValue($value);
} }
@ -43,6 +43,14 @@ class Attribute
return new static($name, $value); return new static($name, $value);
} }
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/** /**
* @param $name * @param $name
* @return $this * @return $this
@ -50,22 +58,15 @@ class Attribute
*/ */
public function setName($name) 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( throw new ProgrammingError(
'Attribute names with special characters are not yet allowed: %s', 'Attribute names with special characters are not yet allowed: %s',
$name $name
); );
} }
$this->name = $name; $this->name = $name;
return $this;
}
/** return $this;
* @return string
*/
public function getName()
{
return $this->name;
} }
/** /**
@ -77,12 +78,13 @@ class Attribute
} }
/** /**
* @param string|array $value * @param mixed $value
* @return $this * @return $this
*/ */
public function setValue($value) public function setValue($value)
{ {
$this->value = $value; $this->value = $value;
return $this; return $this;
} }
@ -93,7 +95,7 @@ class Attribute
public function addValue($value) public function addValue($value)
{ {
if (! is_array($this->value)) { if (! is_array($this->value)) {
$this->value = array($this->value); $this->value = [$this->value];
} }
if (is_array($value)) { if (is_array($value)) {
@ -105,16 +107,28 @@ class Attribute
return $this; return $this;
} }
/**
* @return bool
*/
public function isBoolean()
{
return is_bool($this->value);
}
/** /**
* @return string * @return string
*/ */
public function render() public function render()
{ {
return sprintf( if ($this->isBoolean() && $this->value) {
'%s="%s"', return $this->renderName();
$this->renderName(), } else {
$this->renderValue() return sprintf(
); '%s="%s"',
$this->renderName(),
$this->renderValue()
);
}
} }
/** /**