Allow using a space-separated string as the value for additional classes

This commit is contained in:
2024-01-11 15:57:42 +00:00
parent c1ac0faf78
commit bb7862c6ac
2 changed files with 20 additions and 12 deletions

View File

@@ -16,7 +16,7 @@ final class IconRuntime implements RuntimeExtensionInterface
'size' => null, 'size' => null,
'colour' => null, 'colour' => null,
'hover' => null, 'hover' => null,
'classes' => [], 'classes' => "",
]; ];
public function __construct(private array $defaultOptions, private array $directories, private array $colours) public function __construct(private array $defaultOptions, private array $directories, private array $colours)
@@ -26,12 +26,13 @@ final class IconRuntime implements RuntimeExtensionInterface
* @param array $options * @param array $options
* ``` * ```
* $options = [ * $options = [
* 'icon' => (string) **REQUIRED** Icon name without trailing `.svg` * 'icon' => (string) **REQUIRED** Icon name without trailing `.svg`
* 'title' => (?string) Title text to appear on mouse hover * 'title' => (?string) Title text to appear on mouse hover
* 'size' => (int) Height and width in px * 'size' => (int) Height and width in px
* 'colour' => (string) Main colour * 'colour' => (string) Main colour
* 'hover' => (?string) Hover colour * 'hover' => (?string) Hover colour
* 'classes' => (array) Additional classes to add to the icon. Not recommended. * '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; 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 private function mergeWithDefaultOptions(array $userOptions): array

View File

@@ -225,19 +225,26 @@ class IconRuntimeTest extends TestCase
$this->assertMatchesRegularExpression('/<svg.+class=".*group-hover:stroke-white.*".*>/', $contents); $this->assertMatchesRegularExpression('/<svg.+class=".*group-hover:stroke-white.*".*>/', $contents);
} }
public function testExtraClassesThrowsIfNotAnArray(): void public function testExtraClassesThrowsIfNotAnArrayOrString(): void
{ {
$this->expectException(\TypeError::class); $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']]); $contents = $this->icon->renderIcon(['icon' => self::ICON, 'classes' => ['abc', 'def']]);
$this->assertMatchesRegularExpression('/<svg.+class=".*abc.*".*>/', $contents); $this->assertMatchesRegularExpression('/<svg.+class=".*abc.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*def.*".*>/', $contents); $this->assertMatchesRegularExpression('/<svg.+class=".*def.*".*>/', $contents);
} }
public function testExtraClassesGetAddedFromString(): void
{
$contents = $this->icon->renderIcon(['icon' => self::ICON, 'classes' => 'ghi jkl']);
$this->assertMatchesRegularExpression('/<svg.+class=".*ghi.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*jkl.*".*>/', $contents);
}
public function testAddingExtraClassesDoesntStripAwayColourClasses(): void public function testAddingExtraClassesDoesntStripAwayColourClasses(): void
{ {
$contents = $this->icon->renderIcon(['icon' => self::ICON, 'classes' => ['abc']]); $contents = $this->icon->renderIcon(['icon' => self::ICON, 'classes' => ['abc']]);