Skip to content

Commit f049743

Browse files
committed
[Icons] Add aliases when fetching multiples icons with Iconify
1 parent 07cbb0b commit f049743

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/Icons/src/Iconify.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ public function fetchIcons(string $prefix, array $names): array
126126
$data = $response->toArray();
127127

128128
$icons = [];
129-
foreach ($data['icons'] as $iconName => $iconData) {
129+
foreach ($names as $iconName) {
130+
$iconData = $data['icons'][$data['aliases'][$iconName]['parent'] ?? $iconName] ?? null;
131+
if (!$iconData) {
132+
continue;
133+
}
134+
130135
$height = $iconData['height'] ?? $data['height'] ??= $this->sets()[$prefix]['height'] ?? null;
131136
$width = $iconData['width'] ?? $data['width'] ??= $this->sets()[$prefix]['width'] ?? null;
132137

src/Icons/tests/Unit/IconifyTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,47 @@ public function testFetchIcons(): void
190190
$icons = $iconify->fetchIcons('bi', ['heart', 'bar']);
191191

192192
$this->assertCount(2, $icons);
193-
$this->assertSame(['heart', 'bar'], array_keys($icons));
193+
$this->assertSame(['bar', 'heart'], array_keys($icons));
194+
$this->assertContainsOnlyInstancesOf(Icon::class, $icons);
195+
}
196+
197+
public function testFetchIconsByAliases(): void
198+
{
199+
$iconify = new Iconify(
200+
cache: new NullAdapter(),
201+
endpoint: 'https://example.com',
202+
http: new MockHttpClient([
203+
new JsonMockResponse([
204+
'mdi' => [],
205+
]),
206+
new JsonMockResponse([
207+
'aliases' => [
208+
'capsule' => [
209+
'parent' => 'pill',
210+
],
211+
'sign' => [
212+
'parent' => 'draw',
213+
],
214+
],
215+
'icons' => [
216+
'pill' => [
217+
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
218+
],
219+
'glasses' => [
220+
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
221+
],
222+
'draw' => [
223+
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
224+
],
225+
],
226+
]),
227+
]),
228+
);
229+
230+
$icons = $iconify->fetchIcons('mdi', ['capsule', 'sign', 'glasses']);
231+
232+
$this->assertCount(3, $icons);
233+
$this->assertSame(['capsule', 'glasses', 'sign'], array_keys($icons));
194234
$this->assertContainsOnlyInstancesOf(Icon::class, $icons);
195235
}
196236

0 commit comments

Comments
 (0)