Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Icons/src/Iconify.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ final class Iconify
// -safe margin
private const MAX_ICONS_QUERY_LENGTH = 400;

// https://github.com/iconify/iconify/blob/00cc144b040b838bd86474ab83f0e50e6c6a12a1/packages/utils/src/icon/defaults.ts#L23-L30
private const DEFAULT_ICON_WIDTH = 16;
private const DEFAULT_ICON_HEIGHT = 16;

private HttpClientInterface $http;
private \ArrayObject $sets;
private int $maxIconsQueryLength;
Expand Down Expand Up @@ -81,13 +85,10 @@ public function fetchIcon(string $prefix, string $name): Icon

$height = $data['icons'][$name]['height'] ?? $data['height'] ?? $this->sets()[$prefix]['height'] ?? null;
$width = $data['icons'][$name]['width'] ?? $data['width'] ?? $this->sets()[$prefix]['width'] ?? null;
if (null === $width && null === $height) {
throw new \RuntimeException(\sprintf('The icon "%s:%s" does not have a width or height.', $prefix, $nameArg));
}

return new Icon($data['icons'][$name]['body'], [
'xmlns' => self::ATTR_XMLNS_URL,
'viewBox' => \sprintf('0 0 %s %s', $width ?? $height, $height ?? $width),
'viewBox' => \sprintf('0 0 %s %s', $width ?? $height ?? self::DEFAULT_ICON_WIDTH, $height ?? $width ?? self::DEFAULT_ICON_HEIGHT),
]);
}

Expand Down Expand Up @@ -135,7 +136,7 @@ public function fetchIcons(string $prefix, array $names): array

$icons[$iconName] = new Icon($iconData['body'], [
'xmlns' => self::ATTR_XMLNS_URL,
'viewBox' => \sprintf('0 0 %d %d', $width ?? $height, $height ?? $width),
'viewBox' => \sprintf('0 0 %d %d', $width ?? $height ?? self::DEFAULT_ICON_WIDTH, $height ?? $width ?? self::DEFAULT_ICON_HEIGHT),
]);
}

Expand Down
9 changes: 5 additions & 4 deletions src/Icons/tests/Unit/IconifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function testFetchIconUsesIconsetViewBoxHeight()
$this->assertEquals('0 0 17 17', $icon->getAttributes()['viewBox']);
}

public function testFetchIconThrowsWhenViewBoxCannotBeComputed()
public function testFetchIconSetsDefaultViewBoxTo16()
{
$iconify = new Iconify(
cache: new NullAdapter(),
Expand All @@ -138,10 +138,11 @@ public function testFetchIconThrowsWhenViewBoxCannotBeComputed()
]),
);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('The icon "bi:heart" does not have a width or height.');
$icon = $iconify->fetchIcon('bi', 'heart');

$iconify->fetchIcon('bi', 'heart');
$this->assertIsArray($icon->getAttributes());
$this->assertArrayHasKey('viewBox', $icon->getAttributes());
$this->assertEquals('0 0 16 16', $icon->getAttributes()['viewBox']);
}

public function testFetchIconThrowsWhenStatusCodeNot200()
Expand Down
Loading