diff --git a/src/Exception/AliasDefinedException.php b/src/Exception/AliasDefinedException.php new file mode 100644 index 0000000..40fcaa2 --- /dev/null +++ b/src/Exception/AliasDefinedException.php @@ -0,0 +1,8 @@ +attributes = require \implode(DIRECTORY_SEPARATOR, [\dirname(__FILE__), '..', 'resources', 'attributes.php']); @@ -23,10 +26,37 @@ public function getIconNames(): array public function getIcon(string $name, array $attributes = []): Icon { + $name = $this->normalizeIconName($name); + if (!isset($this->icons[$name])) { throw new IconNotFoundException(\sprintf('Icon `%s` not found', $name)); } return new Icon($name, \array_merge($this->attributes, $attributes), $this->icons[$name]); } + + public function addAlias(string $alias, string $iconName): self + { + if (isset($this->aliases[$alias])) { + throw new AliasDefinedException(\sprintf('Alias `%s` already defined', $alias)); + } + + if (!isset($this->icons[$iconName])) { + throw new IconNotFoundException(\sprintf('Icon `%s` not found', $iconName)); + } + + $this->aliases[$alias] = $iconName; + + return $this; + } + + public function getIconAliases(): array + { + return $this->aliases; + } + + private function normalizeIconName(string $name): string + { + return $this->aliases[$name] ?? $name; + } }