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: 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)

View File

@ -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()
);
}
}
/**