Merge default options into user options
This commit is contained in:
@@ -10,19 +10,16 @@ use Twig\Extension\RuntimeExtensionInterface;
|
|||||||
|
|
||||||
final class IconRuntime implements RuntimeExtensionInterface
|
final class IconRuntime implements RuntimeExtensionInterface
|
||||||
{
|
{
|
||||||
public const DEFAULT_SIZE = 32;
|
private const OPTIONS = [
|
||||||
public const DEFAULT_COLOUR = 'primary';
|
|
||||||
|
|
||||||
private const DEFAULT_OPTIONS = [
|
|
||||||
'icon' => null,
|
'icon' => null,
|
||||||
'title' => null,
|
'title' => null,
|
||||||
'size' => self::DEFAULT_SIZE,
|
'size' => null,
|
||||||
'colour' => self::DEFAULT_COLOUR,
|
'colour' => null,
|
||||||
'hover' => null,
|
'hover' => null,
|
||||||
'classes' => [],
|
'classes' => [],
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(private array $directories, private array $colours)
|
public function __construct(private array $defaultOptions, private array $directories, private array $colours)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,13 +73,17 @@ final class IconRuntime implements RuntimeExtensionInterface
|
|||||||
|
|
||||||
private function mergeUserOptionsWithDefaults(array $userOptions): array
|
private function mergeUserOptionsWithDefaults(array $userOptions): array
|
||||||
{
|
{
|
||||||
return array_merge(self::DEFAULT_OPTIONS, $userOptions);
|
$options = self::OPTIONS;
|
||||||
|
$options['size'] = $this->defaultOptions['size'];
|
||||||
|
$options['colour'] = $this->defaultOptions['colour'];
|
||||||
|
|
||||||
|
return array_merge($options, $userOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function throwIfUnrecognisedOptionExists(array $options): void
|
private function throwIfUnrecognisedOptionExists(array $options): void
|
||||||
{
|
{
|
||||||
foreach (array_keys($options) as $key) {
|
foreach (array_keys($options) as $key) {
|
||||||
if (!key_exists($key, self::DEFAULT_OPTIONS)) {
|
if (!key_exists($key, self::OPTIONS)) {
|
||||||
throw new \InvalidArgumentException("Unrecognised option '{$key}'!");
|
throw new \InvalidArgumentException("Unrecognised option '{$key}'!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user