Skip to content
This repository was archived by the owner on Jul 7, 2025. It is now read-only.

Commit 9db4307

Browse files
author
Nate Wiebe
committed
Add icon aliases
1 parent fc3f19a commit 9db4307

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Feather\Exception;
4+
5+
class AliasDefinedException extends \Exception
6+
{
7+
8+
}

src/IconManager.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Feather;
44

5+
use Feather\Exception\AliasDefinedException;
56
use Feather\Exception\IconNotFoundException;
67

78
class IconManager
@@ -10,6 +11,8 @@ class IconManager
1011

1112
private $icons;
1213

14+
private $aliases = [];
15+
1316
public function __construct()
1417
{
1518
$this->attributes = require \implode(DIRECTORY_SEPARATOR, [\dirname(__FILE__), '..', 'resources', 'attributes.php']);
@@ -23,10 +26,37 @@ public function getIconNames(): array
2326

2427
public function getIcon(string $name, array $attributes = []): Icon
2528
{
29+
$name = $this->normalizeIconName($name);
30+
2631
if (!isset($this->icons[$name])) {
2732
throw new IconNotFoundException(\sprintf('Icon `%s` not found', $name));
2833
}
2934

3035
return new Icon($name, \array_merge($this->attributes, $attributes), $this->icons[$name]);
3136
}
37+
38+
public function addAlias(string $alias, string $iconName): self
39+
{
40+
if (isset($this->aliases[$alias])) {
41+
throw new AliasDefinedException(\sprintf('Alias `%s` already defined', $alias));
42+
}
43+
44+
if (!isset($this->icons[$iconName])) {
45+
throw new IconNotFoundException(\sprintf('Icon `%s` not found', $iconName));
46+
}
47+
48+
$this->aliases[$alias] = $iconName;
49+
50+
return $this;
51+
}
52+
53+
public function getIconAliases(): array
54+
{
55+
return $this->aliases;
56+
}
57+
58+
private function normalizeIconName(string $name): string
59+
{
60+
return $this->aliases[$name] ?? $name;
61+
}
3262
}

0 commit comments

Comments
 (0)