Skip to content

Commit

Permalink
[Icons] Add aliases when fetching multiples icons with Iconify
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal committed Dec 25, 2024
1 parent 07cbb0b commit 8ee5230
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Icons/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.23.0

- Add support for aliases when fetching multiple icons with `Iconify`

## 2.20.0

- Add `aliases` configuration option to define icon alternative names.
Expand Down
4 changes: 3 additions & 1 deletion src/Icons/src/Iconify.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ public function fetchIcons(string $prefix, array $names): array
$data = $response->toArray();

$icons = [];
foreach ($data['icons'] as $iconName => $iconData) {
foreach ($names as $iconName) {
$iconData = $data['icons'][$data['aliases'][$iconName]['parent'] ?? $iconName];

$height = $iconData['height'] ?? $data['height'] ??= $this->sets()[$prefix]['height'] ?? null;
$width = $iconData['width'] ?? $data['width'] ??= $this->sets()[$prefix]['width'] ?? null;

Expand Down
42 changes: 41 additions & 1 deletion src/Icons/tests/Unit/IconifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,47 @@ public function testFetchIcons(): void
$icons = $iconify->fetchIcons('bi', ['heart', 'bar']);

$this->assertCount(2, $icons);
$this->assertSame(['heart', 'bar'], array_keys($icons));
$this->assertSame(['bar', 'heart'], array_keys($icons));
$this->assertContainsOnlyInstancesOf(Icon::class, $icons);
}

public function testFetchIconsByAliases(): void
{
$iconify = new Iconify(
cache: new NullAdapter(),
endpoint: 'https://example.com',
http: new MockHttpClient([
new JsonMockResponse([
'mdi' => [],
]),
new JsonMockResponse([
'aliases' => [
'capsule' => [
'parent' => 'pill',
],
'sign' => [
'parent' => 'draw',
],
],
'icons' => [
'pill' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
],
'glasses' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
],
'draw' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
],
],
]),
]),
);

$icons = $iconify->fetchIcons('mdi', ['capsule', 'sign', 'glasses']);

$this->assertCount(3, $icons);
$this->assertSame(['capsule', 'glasses', 'sign'], array_keys($icons));
$this->assertContainsOnlyInstancesOf(Icon::class, $icons);
}

Expand Down

0 comments on commit 8ee5230

Please sign in to comment.