Allow using a space-separated string as the value for additional classes
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -225,19 +225,26 @@ class IconRuntimeTest extends TestCase
|
||||
$this->assertMatchesRegularExpression('/<svg.+class=".*group-hover:stroke-white.*".*>/', $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('/<svg.+class=".*abc.*".*>/', $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
|
||||
{
|
||||
$contents = $this->icon->renderIcon(['icon' => self::ICON, 'classes' => ['abc']]);
|
||||
|
||||
Reference in New Issue
Block a user