Further refactoring
This commit is contained in:
@@ -51,9 +51,13 @@ final class IconRuntime implements RuntimeExtensionInterface
|
|||||||
$extraClasses = $this->getExtraClasses($options['classes']);
|
$extraClasses = $this->getExtraClasses($options['classes']);
|
||||||
|
|
||||||
$classes = trim($colourClasses.' '.$extraClasses);
|
$classes = trim($colourClasses.' '.$extraClasses);
|
||||||
$svg = $this->addClassesToSvg($svg, $classes);
|
$this->addClassesToSvg($svg, $classes);
|
||||||
$svg = $this->addTitleToSvgIfNotNull($svg, $options['title']);
|
|
||||||
$svg = $this->setSvgHeightAndWidth($svg, $options['size']);
|
if (null !== $options['title']) {
|
||||||
|
$this->addTitleToSvg($svg, $options['title']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setSvgHeightAndWidth($svg, $options['size']);
|
||||||
|
|
||||||
return $svg;
|
return $svg;
|
||||||
}
|
}
|
||||||
@@ -153,24 +157,20 @@ final class IconRuntime implements RuntimeExtensionInterface
|
|||||||
throw new ColourNotFound("The colour \"$colourName\" was not found!");
|
throw new ColourNotFound("The colour \"$colourName\" was not found!");
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addClassesToSvg(string $svg, string $classes): string
|
private function addClassesToSvg(string &$svg, string $classes): void
|
||||||
{
|
{
|
||||||
$xml = new \SimpleXMLElement($svg);
|
$xml = new \SimpleXMLElement($svg);
|
||||||
$xml = $this->addAttributeToXmlElement($xml, 'class', $classes);
|
$xml = $this->addAttributeToXmlElement($xml, 'class', $classes);
|
||||||
return $this->removeXMLDeclaration($xml->saveXML());
|
$svg = $this->removeXMLDeclaration($xml->saveXML());
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addTitleToSvgIfNotNull(string $svg, ?string $title): string
|
private function addTitleToSvg(string &$svg, ?string $title): void
|
||||||
{
|
{
|
||||||
if (null === $title) {
|
|
||||||
return $svg;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('' === $title) {
|
if ('' === $title) {
|
||||||
throw new \InvalidArgumentException('Title must not be an empty string!');
|
throw new \InvalidArgumentException('Title must not be an empty string!');
|
||||||
}
|
}
|
||||||
|
|
||||||
return preg_replace('/(<svg(.|\n)*?>\n?)/', "$1<title>$title</title>", $svg);
|
$svg = preg_replace('/(<svg(.|\n)*?>\n?)/', "$1<title>$title</title>", $svg);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addAttributeToXmlElement(\SimpleXMLElement $xml, string $attrName, mixed $attrValue): \SimpleXMLElement
|
private function addAttributeToXmlElement(\SimpleXMLElement $xml, string $attrName, mixed $attrValue): \SimpleXMLElement
|
||||||
@@ -189,20 +189,16 @@ final class IconRuntime implements RuntimeExtensionInterface
|
|||||||
return trim(preg_replace('/<\?xml.*\?>/', '', $content));
|
return trim(preg_replace('/<\?xml.*\?>/', '', $content));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setSvgHeightAndWidth(string $content, int $size): string
|
private function setSvgHeightAndWidth(string &$svg, int $size): void
|
||||||
{
|
|
||||||
$this->throwIfSizeIsNegative($size);
|
|
||||||
|
|
||||||
$svgAsXmlElement = new \SimpleXMLElement($content);
|
|
||||||
$svgAsXmlElement = $this->addAttributeToXmlElement($svgAsXmlElement, 'width', $size);
|
|
||||||
$svgAsXmlElement = $this->addAttributeToXmlElement($svgAsXmlElement, 'height', $size);
|
|
||||||
return $this->removeXMLDeclaration($svgAsXmlElement->saveXML());
|
|
||||||
}
|
|
||||||
|
|
||||||
private function throwIfSizeIsNegative(int $size): void
|
|
||||||
{
|
{
|
||||||
if ($size < 0) {
|
if ($size < 0) {
|
||||||
throw new \InvalidArgumentException('Size must not be negative');
|
throw new \InvalidArgumentException('Size must not be negative');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$svgAsXml = new \SimpleXMLElement($svg);
|
||||||
|
$svgAsXml = $this->addAttributeToXmlElement($svgAsXml, 'width', $size);
|
||||||
|
$svgAsXml = $this->addAttributeToXmlElement($svgAsXml, 'height', $size);
|
||||||
|
|
||||||
|
$svg = $this->removeXMLDeclaration($svgAsXml->saveXML());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user