Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add icon aliases #42

Merged
merged 1 commit into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Exception/AliasDefinedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Feather\Exception;

class AliasDefinedException extends \Exception
{

}
30 changes: 30 additions & 0 deletions src/IconManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Feather;

use Feather\Exception\AliasDefinedException;
use Feather\Exception\IconNotFoundException;

class IconManager
Expand All @@ -10,6 +11,8 @@ class IconManager

private $icons;

private $aliases = [];

public function __construct()
{
$this->attributes = require \implode(DIRECTORY_SEPARATOR, [\dirname(__FILE__), '..', 'resources', 'attributes.php']);
Expand All @@ -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;
}
}