22
33namespace Feather ;
44
5+ use Feather \Exception \AliasDefinedException ;
56use Feather \Exception \IconNotFoundException ;
67
78class 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 = [], ?string $ altText = null ): 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 , $ this ->icons [$ name ], \array_merge ($ this ->attributes , $ attributes ), $ altText );
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