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