<?php
 declare(strict_types=1); namespace Jose\Component\Core; use InvalidArgumentException; use function is_string; use function sprintf; final class AlgorithmManagerFactory { private array $algorithms = []; public function __construct(iterable $algorithms = []) { foreach ($algorithms as $algorithm) { $this->add($algorithm->name(), $algorithm); } } public function add(string $alias, Algorithm $algorithm): void { $this->algorithms[$alias] = $algorithm; } public function aliases(): array { return array_keys($this->algorithms); } public function all(): array { return $this->algorithms; } public function create(array $aliases): AlgorithmManager { $algorithms = []; foreach ($aliases as $alias) { if (! is_string($alias)) { throw new InvalidArgumentException('Invalid alias'); } if (! isset($this->algorithms[$alias])) { throw new InvalidArgumentException(sprintf( 'The algorithm with the alias "%s" is not supported.', $alias )); } $algorithms[] = $this->algorithms[$alias]; } return new AlgorithmManager($algorithms); } } 