Files
pcm-icon-bundle/src/Twig/Runtime/IconRuntime.php
2023-06-27 10:01:35 +01:00

205 lines
6.5 KiB
PHP

<?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;
public const DEFAULT_COLOUR = 'primary';
private const DEFAULT_OPTIONS = [
'icon' => null,
'title' => null,
'size' => self::DEFAULT_SIZE,
'colour' => self::DEFAULT_COLOUR,
'hover' => null,
'classes' => [],
];
public function __construct(private array $directories, private array $colours)
{}
/**
* @param array $options
* ```
* $options = [
* 'icon' => (string) **REQUIRED** Icon name without trailing `.svg`
* 'title' => (?string) Title 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. Not recommended.
* ]
* ```
*
* @return string Processed SVG
*/
public function renderIcon(array $userOptions): string
{
$options = $this->mergeWithDefaultOptions($userOptions);
$svg = $this->getSvg($options['icon']);
$svg = $this->sanitiseSvg($svg);
$colourClasses = $this->getColourClasses($options['colour'], $options['hover']);
$extraClasses = $this->getExtraClasses($options['classes']);
$classes = trim($colourClasses.' '.$extraClasses);
$this->addClassesToSvg($svg, $classes);
if (null !== $options['title']) {
$this->addTitleToSvg($svg, $options['title']);
}
$this->setSvgHeightAndWidth($svg, $options['size']);
return $svg;
}
private function getExtraClasses(array $extraClasses): string
{
return implode(' ', $extraClasses);
}
private function mergeWithDefaultOptions(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 getSvg(string $iconName): string
{
$iconFilepath = $this->findSvgFilepath($iconName);
return file_get_contents($iconFilepath);
}
private function sanitiseSvg(string &$svg): string
{
$this->removeExistingTitleElement($svg);
$this->removeBlackStrokeAttributes($svg);
$this->removeStrokeCurrentColor($svg);
return $this->removeBlackFillAttributes($svg);
}
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 removeExistingTitleElement(string &$svg): void
{
$svg = preg_replace('/<title>.*<\/title>/', '', $svg);
}
private function removeBlackStrokeAttributes(string &$svg): void
{
$svg = preg_replace('/stroke(=|:)"?\s*(#0{6}|#000|rgb\(\s*0,\s*0,\s*0\s*\)|black)\s*"?/', '', $svg);
}
private function removeStrokeCurrentColor(string &$svg): void
{
$svg = \preg_replace('/stroke=(\'|")currentColor(\'|")/', '', $svg);
}
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): void
{
$xml = new \SimpleXMLElement($svg);
$xml = $this->addAttributeToXmlElement($xml, 'class', $classes);
$svg = $this->removeXMLDeclaration($xml->saveXML());
}
private function addTitleToSvg(string &$svg, ?string $title): void
{
if ('' === $title) {
throw new \InvalidArgumentException('Title must not be an empty string!');
}
$svg = 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 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());
}
}