Skip to content

Commit 8ee5230

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

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/Icons/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.23.0
4+
5+
- Add support for aliases when fetching multiple icons with `Iconify`
6+
37
## 2.20.0
48

59
- Add `aliases` configuration option to define icon alternative names.

src/Icons/src/Iconify.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ 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];
131+
130132
$height = $iconData['height'] ?? $data['height'] ??= $this->sets()[$prefix]['height'] ?? null;
131133
$width = $iconData['width'] ?? $data['width'] ??= $this->sets()[$prefix]['width'] ?? null;
132134

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)