Skip to content

Commit

Permalink
Priorize style option, remove "fa-" from icon name if set, added miss…
Browse files Browse the repository at this point in the history
…ing fa-icon class
  • Loading branch information
Stefan Brauner committed Dec 22, 2023
1 parent a8a7be3 commit d641294
Showing 1 changed file with 55 additions and 24 deletions.
79 changes: 55 additions & 24 deletions src/Twig/SvgExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,26 @@ public function getFunctions(): array
}

/**
* Gibt ein FontAwesome-Icon zurück.
* Inline svg output of the Font Awesome icon.
*
* @param array{style?: string, color?: string, secondaryColor?: string, class?: string, title?: string, size?: string} $options
*/
public function fontAwesomeIcon(string $icon, array $options = []): string
{
$style = $this->getIconStyle($icon, $options);
$iconDocument = $this->getIconXml($icon, $style);
$iconName = $this->getIconName($icon);

$iconDocument = $this->getIconXml($iconName, $style);

/** @var \DOMElement $svgRoot */
$svgRoot = $iconDocument->getElementsByTagName('svg')->item(0);

if (\array_key_exists('class', $options)) {
$svgRoot->setAttribute('class', 'fa-icon ' . $options['class']);
} else {
$svgRoot->setAttribute('class', 'fa-icon');
}

$svgRoot->setAttribute('role', 'img');

/** @var \DOMElement $primaryPath */
Expand Down Expand Up @@ -123,13 +128,11 @@ public function fontAwesomeIcon(string $icon, array $options = []): string
return $iconDocument->saveHTML();
}

/**
* Search for the needed icon file.
*/
private function getIconFile(string $icon, string $style): string
{
$prefix = explode(' ', $icon)[0] ?? '';
if (\in_array($prefix ?? 'fa', ['fab', 'fad', 'far', 'fal', 'fas'], true)) {
$icon = str_replace($prefix . ' ', '', $icon);
}

// Check assets-directory
$path = $this->projectDir . '/assets/font-awesome/' . $style . '/' . $icon . '.svg';
if (file_exists($path)) {
Expand All @@ -146,6 +149,9 @@ private function getIconFile(string $icon, string $style): string
throw new \RuntimeException('FontAwesome icon "' . $icon . '" not found.');
}

/**
* Read the svg file and return it as xml document.
*/
private function getIconXml(string $icon, string $style): \DOMDocument
{
$content = file_get_contents($this->getIconFile($icon, $style));
Expand All @@ -157,25 +163,14 @@ private function getIconXml(string $icon, string $style): \DOMDocument
}

/**
* Gets the icon style by options array or icon name.
*
* @param array{style?: string, color?: string, secondaryColor?: string, class?: string, title?: string} $options
*/
private function getIconStyle(string $icon, array $options): string
{
if (!\array_key_exists('style', $options)) {
$prefix = explode(' ', $icon);

return match ($prefix[0] ?? '') {
'fab' => 'brands',
'fad' => 'duotone',
'far' => 'regular',
'fal' => 'light',
'fat' => 'thin',
default => 'solid',
};
}

$style = $options['style'];
if (!\in_array($style,
// style option with higher priority
if (\in_array($options['style'] ?? '',
[
'brands',
'duotone',
Expand All @@ -189,9 +184,45 @@ private function getIconStyle(string $icon, array $options): string
'thin',
], true
)) {
throw new \RuntimeException('Invalid FontAwesome style "' . $style . '.');
return $options['style'];
}

// style option is set, but invalid
if (!empty($options['style'])) {
throw new \RuntimeException('Invalid FontAwesome style "' . $options['style'] . '.');
}

// if no style option is set, check for prefix, otherwise use "solid" as the default value
$prefix = explode(' ', $icon);

return match ($prefix[0] ?? '') {
'fab' => 'brands',
'fad' => 'duotone',
'far' => 'regular',
'fal' => 'light',
'fat' => 'thin',
default => 'solid',
};
}

/**
* Gets the name of the icon without optional style prefixes.
*/
private function getIconName(string $icon): string
{
// removes the prefixes
$icon = strtolower($icon);

$prefix = explode(' ', $icon)[0] ?? '';
if (\in_array($prefix ?? 'fa', ['fab', 'fad', 'far', 'fal', 'fas'], true)) {
$icon = str_replace($prefix . ' ', '', $icon);
}

// removes "fa-" in the icon name
if (str_starts_with($icon, 'fa-')) {
$icon = substr($icon, 3);
}

return $style;
return $icon;
}
}

0 comments on commit d641294

Please sign in to comment.