Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Core/AlgorithmManagerFactory.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 is_string;
9
10 /**
11 * @see \Jose\Tests\Component\Core\AlgorithmManagerFactoryTest
12 */
13 class AlgorithmManagerFactory
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->name(), $algorithm);
24 }
25 }
26
27 /**
28 * Adds an algorithm.
29 *
30 * Each algorithm is identified by an alias hence it is allowed to have the same algorithm twice (or more). This can
31 * be helpful when an algorithm have several configuration options.
32 */
33 public function add(string $alias, Algorithm $algorithm): void
34 {
35 $this->algorithms[$alias] = $algorithm;
36 }
37
38 /**
39 * Returns the list of aliases.
40 *
41 * @return string[]
42 */
43 public function aliases(): array
44 {
45 return array_keys($this->algorithms);
46 }
47
48 /**
49 * Returns all algorithms supported by this factory. This is an associative array. Keys are the aliases of the
50 * algorithms.
51 *
52 * @return Algorithm[]
53 */
54 public function all(): array
55 {
56 return $this->algorithms;
57 }
58
59 /**
60 * Create an algorithm manager using the given aliases.
61 *
62 * @param string[] $aliases
63 */
64 public function create(array $aliases): AlgorithmManager
65 {
66 $algorithms = [];
67 foreach ($aliases as $alias) {
68 if (! is_string($alias)) {
69 throw new InvalidArgumentException('Invalid alias');
70 }
71 if (! isset($this->algorithms[$alias])) {
72 throw new InvalidArgumentException(sprintf(
73 'The algorithm with the alias "%s" is not supported.',
74 $alias
75 ));
76 }
77 $algorithms[] = $this->algorithms[$alias];
78 }
79
80 return new AlgorithmManager($algorithms);
81 }
82 }