From bb7862c6ac763df884b7d990702f75e9e1916911 Mon Sep 17 00:00:00 2001 From: Bradley Date: Thu, 11 Jan 2024 15:57:42 +0000 Subject: [PATCH] Allow using a space-separated string as the value for additional classes --- src/Twig/Runtime/IconRuntime.php | 19 ++++++++++--------- tests/Twig/Runtime/IconRuntimeTest.php | 13 ++++++++++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Twig/Runtime/IconRuntime.php b/src/Twig/Runtime/IconRuntime.php index 10ba367..9cf400b 100644 --- a/src/Twig/Runtime/IconRuntime.php +++ b/src/Twig/Runtime/IconRuntime.php @@ -16,7 +16,7 @@ final class IconRuntime implements RuntimeExtensionInterface 'size' => null, 'colour' => null, 'hover' => null, - 'classes' => [], + 'classes' => "", ]; public function __construct(private array $defaultOptions, private array $directories, private array $colours) @@ -26,12 +26,13 @@ final class IconRuntime implements RuntimeExtensionInterface * @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. + * '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' => (string[]|string) Additional classes to add to the icon, given as + * an array of strings or a space-separated string. * ] * ``` * @@ -60,9 +61,9 @@ final class IconRuntime implements RuntimeExtensionInterface return $svg; } - private function getExtraClasses(array $extraClasses): string + private function getExtraClasses(array|string $extraClasses): string { - return implode(' ', $extraClasses); + return \is_array($extraClasses) ? implode(' ', $extraClasses) : $extraClasses; } private function mergeWithDefaultOptions(array $userOptions): array diff --git a/tests/Twig/Runtime/IconRuntimeTest.php b/tests/Twig/Runtime/IconRuntimeTest.php index 6b7b68c..0ea2abf 100644 --- a/tests/Twig/Runtime/IconRuntimeTest.php +++ b/tests/Twig/Runtime/IconRuntimeTest.php @@ -225,19 +225,26 @@ class IconRuntimeTest extends TestCase $this->assertMatchesRegularExpression('//', $contents); } - public function testExtraClassesThrowsIfNotAnArray(): void + public function testExtraClassesThrowsIfNotAnArrayOrString(): void { $this->expectException(\TypeError::class); - $this->icon->renderIcon(['icon' => self::ICON, 'classes' => 'string_value']); + $this->icon->renderIcon(['icon' => self::ICON, 'classes' => 1]); } - public function testExtraClassesGetAdded(): void + public function testExtraClassesGetAddedFromArray(): void { $contents = $this->icon->renderIcon(['icon' => self::ICON, 'classes' => ['abc', 'def']]); $this->assertMatchesRegularExpression('//', $contents); $this->assertMatchesRegularExpression('//', $contents); } + public function testExtraClassesGetAddedFromString(): void + { + $contents = $this->icon->renderIcon(['icon' => self::ICON, 'classes' => 'ghi jkl']); + $this->assertMatchesRegularExpression('//', $contents); + $this->assertMatchesRegularExpression('//', $contents); + } + public function testAddingExtraClassesDoesntStripAwayColourClasses(): void { $contents = $this->icon->renderIcon(['icon' => self::ICON, 'classes' => ['abc']]);