From dbd4ccb35d4beb41a0b6c64a1ce28f31c6287dac Mon Sep 17 00:00:00 2001 From: Bradley Date: Tue, 27 Jun 2023 10:01:35 +0100 Subject: [PATCH] Further refactoring --- src/Twig/Runtime/IconRuntime.php | 40 ++++++++++++++------------------ 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/Twig/Runtime/IconRuntime.php b/src/Twig/Runtime/IconRuntime.php index fa279f8..f5d679d 100644 --- a/src/Twig/Runtime/IconRuntime.php +++ b/src/Twig/Runtime/IconRuntime.php @@ -51,9 +51,13 @@ final class IconRuntime implements RuntimeExtensionInterface $extraClasses = $this->getExtraClasses($options['classes']); $classes = trim($colourClasses.' '.$extraClasses); - $svg = $this->addClassesToSvg($svg, $classes); - $svg = $this->addTitleToSvgIfNotNull($svg, $options['title']); - $svg = $this->setSvgHeightAndWidth($svg, $options['size']); + $this->addClassesToSvg($svg, $classes); + + if (null !== $options['title']) { + $this->addTitleToSvg($svg, $options['title']); + } + + $this->setSvgHeightAndWidth($svg, $options['size']); return $svg; } @@ -153,24 +157,20 @@ final class IconRuntime implements RuntimeExtensionInterface 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 = $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) { throw new \InvalidArgumentException('Title must not be an empty string!'); } - return preg_replace('/(\n?)/', "$1$title", $svg); + $svg = preg_replace('/(\n?)/', "$1$title", $svg); } 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)); } - private function setSvgHeightAndWidth(string $content, int $size): string - { - $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 + private function setSvgHeightAndWidth(string &$svg, int $size): void { if ($size < 0) { 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()); } }