| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Cose\Algorithm; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use InvalidArgumentException; |
| 9 |
use function sprintf; |
| 10 |
|
| 11 |
final class ManagerFactory |
| 12 |
{ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
private array $algorithms = []; |
| 17 |
|
| 18 |
public static function create(): self |
| 19 |
{ |
| 20 |
return new self(); |
| 21 |
} |
| 22 |
|
| 23 |
public function add(string $alias, Algorithm $algorithm): self |
| 24 |
{ |
| 25 |
$this->algorithms[$alias] = $algorithm; |
| 26 |
|
| 27 |
return $this; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function list(): iterable |
| 34 |
{ |
| 35 |
yield from array_keys($this->algorithms); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function all(): iterable |
| 42 |
{ |
| 43 |
yield from $this->algorithms; |
| 44 |
} |
| 45 |
|
| 46 |
public function generate(string ...$aliases): Manager |
| 47 |
{ |
| 48 |
$manager = Manager::create(); |
| 49 |
foreach ($aliases as $alias) { |
| 50 |
if (! array_key_exists($alias, $this->algorithms)) { |
| 51 |
throw new InvalidArgumentException(sprintf('The algorithm with alias "%s" is not supported', $alias)); |
| 52 |
} |
| 53 |
$manager->add($this->algorithms[$alias]); |
| 54 |
} |
| 55 |
|
| 56 |
return $manager; |
| 57 |
} |
| 58 |
} |