243 lines
7.9 KiB
PHP
243 lines
7.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pcm\IconBundle\Twig\Functions;
|
|
|
|
use Twig\Extension\AbstractExtension;
|
|
use Twig\TwigFunction;
|
|
|
|
final class IconExtension extends AbstractExtension
|
|
{
|
|
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)
|
|
{
|
|
if (empty($this->colours)) {
|
|
throw new \InvalidArgumentException('Colours array must contain at least one colour!');
|
|
}
|
|
|
|
$coloursContainsNonArray = array_reduce($this->colours,
|
|
fn($notArray, $path) => $notArray || !is_array($path));
|
|
|
|
if ($coloursContainsNonArray) {
|
|
throw new \TypeError('Colours array must only contain arrays!');
|
|
}
|
|
|
|
foreach ($this->colours as $colour) {
|
|
if (!(
|
|
array_key_exists('stroke', $colour) &&
|
|
array_key_exists('fill', $colour) &&
|
|
array_key_exists('fill-hover', $colour) &&
|
|
array_key_exists('stroke-hover', $colour) &&
|
|
array_key_exists('fill-group-hover', $colour) &&
|
|
array_key_exists('stroke-group-hover', $colour))
|
|
) {
|
|
throw new \Exception('Colours must contain a "stroke" and "fill" key!');
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getFunctions(): array
|
|
{
|
|
return [
|
|
new TwigFunction('icon', [$this, 'renderIcon'], [
|
|
'is_safe' => ['html']
|
|
])
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @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');
|
|
}
|
|
}
|
|
}
|
|
|
|
class IconNotFound extends \Exception {};
|
|
|
|
class ColourNotFound extends \Exception {};
|