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('/