Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Core/AlgorithmManager.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\Core;
6
7 use InvalidArgumentException;
8 use function array_key_exists;
9
10 class AlgorithmManager
11 {
12 /**
13 * @var array<string, Algorithm>
14 */
15 private array $algorithms = [];
16
17 /**
18 * @param Algorithm[] $algorithms
19 */
20 public function __construct(iterable $algorithms)
21 {
22 foreach ($algorithms as $algorithm) {
23 $this->add($algorithm);
24 }
25 }
26
27 /**
28 * Returns true if the algorithm is supported.
29 *
30 * @param string $algorithm The algorithm
31 */
32 public function has(string $algorithm): bool
33 {
34 return array_key_exists($algorithm, $this->algorithms);
35 }
36
37 /**
38 * @return array<string, Algorithm>
39 */
40 public function all(): array
41 {
42 return $this->algorithms;
43 }
44
45 /**
46 * Returns the list of names of supported algorithms.
47 *
48 * @return string[]
49 */
50 public function list(): array
51 {
52 return array_keys($this->algorithms);
53 }
54
55 /**
56 * Returns the algorithm if supported, otherwise throw an exception.
57 *
58 * @param string $algorithm The algorithm
59 */
60 public function get(string $algorithm): Algorithm
61 {
62 if (! $this->has($algorithm)) {
63 throw new InvalidArgumentException(sprintf('The algorithm "%s" is not supported.', $algorithm));
64 }
65
66 return $this->algorithms[$algorithm];
67 }
68
69 /**
70 * Adds an algorithm to the manager.
71 */
72 public function add(Algorithm $algorithm): void
73 {
74 $name = $algorithm->name();
75 $this->algorithms[$name] = $algorithm;
76 }
77 }