Split extension into extension and runtime classes to match how the maker bundle creates twig extensions
This commit is contained in:
202
src/Twig/Runtime/IconRuntime.php
Normal file
202
src/Twig/Runtime/IconRuntime.php
Normal file
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\IconBundle\Twig\Runtime;
|
||||
|
||||
use Pcm\IconBundle\Exception\ColourNotFound;
|
||||
use Pcm\IconBundle\Exception\IconNotFound;
|
||||
use Twig\Extension\RuntimeExtensionInterface;
|
||||
|
||||
final class IconRuntime implements RuntimeExtensionInterface
|
||||
{
|
||||
public const DEFAULT_SIZE = 32;
|
||||
|
||||
private const DEFAULT_OPTIONS = [
|
||||
'icon' => null,
|
||||
'title' => null,
|
||||
'size' => self::DEFAULT_SIZE,
|
||||
'colour' => 'primary',
|
||||
'hover' => null,
|
||||
'classes' => [],
|
||||
];
|
||||
|
||||
public function __construct(private array $directories, private array $colours) {}
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
* ```
|
||||
* $options = [
|
||||
* 'icon' => (string) REQUIRED Which icon to use
|
||||
* 'title' => (?string) Text to appear on mouse hover
|
||||
* 'size' => (int) Height and width in px
|
||||
* 'colour' => (string) Main colour
|
||||
* 'hover' => (?string) Hover colour
|
||||
* 'classes' => (array) Additional classes to add to the icon.
|
||||
* Use with caution as this can potentially
|
||||
* cause Tailwind class conflicts!
|
||||
* ]
|
||||
* ```
|
||||
*/
|
||||
public function renderIcon(array $userOptions): string
|
||||
{
|
||||
$options = $this->getMergedOptions($userOptions);
|
||||
$svg = $this->getSanitisedIconSvg($options['icon']);
|
||||
|
||||
$colourClasses = $this->getColourClasses($options['colour'], $options['hover']);
|
||||
$extraClasses = $this->getExtraClasses($options['classes']);
|
||||
|
||||
$svg = $this->addClassesToSvg($svg, trim($colourClasses.' '.$extraClasses));
|
||||
$svg = $this->addTitleToSvgIfNotNull($svg, $options['title']);
|
||||
$svg = $this->setSvgHeightAndWidth($svg, $options['size']);
|
||||
|
||||
return $svg;
|
||||
}
|
||||
|
||||
private function getExtraClasses(array $extraClasses): string
|
||||
{
|
||||
return implode(' ', $extraClasses);
|
||||
}
|
||||
|
||||
private function getMergedOptions(array $userOptions): array
|
||||
{
|
||||
$this->throwIfUnrecognisedOptionExists($userOptions);
|
||||
return $this->mergeUserOptionsWithDefaults($userOptions);
|
||||
}
|
||||
|
||||
private function mergeUserOptionsWithDefaults(array $userOptions): array
|
||||
{
|
||||
return array_merge(self::DEFAULT_OPTIONS, $userOptions);
|
||||
}
|
||||
|
||||
private function throwIfUnrecognisedOptionExists(array $options): void
|
||||
{
|
||||
foreach (array_keys($options) as $key) {
|
||||
if (!key_exists($key, self::DEFAULT_OPTIONS)) {
|
||||
throw new \InvalidArgumentException("Unrecognised option '{$key}'!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getSanitisedIconSvg(string $iconName): string
|
||||
{
|
||||
$iconFilepath = $this->findSvgFilepath($iconName);
|
||||
$rawSvgMarkup = $this->getSvgMarkup($iconFilepath);
|
||||
$cleanSvgMarkup = $this->removeExistingTitleElement($rawSvgMarkup);
|
||||
$cleanSvgMarkup = $this->removeBlackStrokeAttributes($cleanSvgMarkup);
|
||||
|
||||
return $this->removeBlackFillAttributes($cleanSvgMarkup);
|
||||
}
|
||||
|
||||
private function findSvgFilepath(string $iconName): string
|
||||
{
|
||||
foreach ($this->directories as $directory) {
|
||||
$potentialFilepath = sprintf('%s/%s.svg', $directory, $iconName);
|
||||
|
||||
if (file_exists($potentialFilepath)) {
|
||||
return $potentialFilepath;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IconNotFound(sprintf('File "%s.svg" not found in %s', $iconName, implode(', ', $this->directories)));
|
||||
}
|
||||
|
||||
private function getSvgMarkup(string $filepath): string
|
||||
{
|
||||
return file_get_contents($filepath);
|
||||
}
|
||||
|
||||
private function removeExistingTitleElement(string $svg): string
|
||||
{
|
||||
return preg_replace('/<title>.*<\/title>/', '', $svg);
|
||||
}
|
||||
|
||||
private function removeBlackStrokeAttributes(string $content): string
|
||||
{
|
||||
return preg_replace('/stroke(=|:)"?\s*(#0{6}|#000|rgb\(\s*0,\s*0,\s*0\s*\)|black)\s*"?/', '', $content);
|
||||
}
|
||||
|
||||
private function removeBlackFillAttributes(string $content): string
|
||||
{
|
||||
return preg_replace('/fill(=|:)"?\s*(#0{6}|#000|rgb\(\s*0,\s*0,\s*0\s*\)|black)\s*"?/', '', $content);
|
||||
}
|
||||
|
||||
private function getColourClasses(string $primaryColour, ?string $hoverColour): string
|
||||
{
|
||||
$mainColour = $this->getColour($primaryColour);
|
||||
$colourClasses = "{$mainColour['stroke']} {$mainColour['fill']}";
|
||||
|
||||
if (null !== $hoverColour) {
|
||||
$hoverColour = $this->getColour($hoverColour);
|
||||
$colourClasses .= " cursor-pointer {$hoverColour['stroke-hover']} {$hoverColour['fill-hover']} {$hoverColour['stroke-group-hover']} {$hoverColour['fill-group-hover']}";
|
||||
}
|
||||
|
||||
return $colourClasses;
|
||||
}
|
||||
|
||||
private function getColour(string $colourName): array
|
||||
{
|
||||
if (array_key_exists($colourName, $this->colours)) {
|
||||
return $this->colours[$colourName];
|
||||
}
|
||||
|
||||
throw new ColourNotFound("The colour \"$colourName\" was not found!");
|
||||
}
|
||||
|
||||
private function addClassesToSvg(string $svg, string $classes): string
|
||||
{
|
||||
$xml = new \SimpleXMLElement($svg);
|
||||
$xml = $this->addAttributeToXmlElement($xml, 'class', $classes);
|
||||
return $this->removeXMLDeclaration($xml->saveXML());
|
||||
}
|
||||
|
||||
private function addTitleToSvgIfNotNull(string $svg, ?string $title): string
|
||||
{
|
||||
if (null === $title) {
|
||||
return $svg;
|
||||
}
|
||||
|
||||
$this->throwIfTitleIsEmpty($title);
|
||||
|
||||
return preg_replace('/(<svg(.|\n)*?>\n?)/', "$1<title>$title</title>", $svg);
|
||||
}
|
||||
|
||||
private function addAttributeToXmlElement(\SimpleXMLElement $xml, string $attrName, mixed $attrValue): \SimpleXMLElement
|
||||
{
|
||||
if (isset($xml->attributes()->$attrName)) {
|
||||
$xml->attributes()->$attrName = strval($attrValue);
|
||||
} else {
|
||||
$xml->addAttribute($attrName, strval($attrValue));
|
||||
}
|
||||
|
||||
return $xml;
|
||||
}
|
||||
|
||||
private function removeXMLDeclaration(string $content): string
|
||||
{
|
||||
return trim(preg_replace('/<\?xml.*\?>/', '', $content));
|
||||
}
|
||||
|
||||
private function throwIfTitleIsEmpty(string $title): void
|
||||
{
|
||||
if ('' === $title) {
|
||||
throw new \InvalidArgumentException('Title string must not be empty!');
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
if ($size < 0) {
|
||||
throw new \InvalidArgumentException('Size must not be negative');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user