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