|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Jose\Component\Core; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class AlgorithmManager |
|
18
|
|
|
*/ |
|
19
|
|
|
final class AlgorithmManager |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $algorithms = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* JWAManager constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param AlgorithmInterface[] $algorithms |
|
30
|
|
|
*/ |
|
31
|
|
|
private function __construct(array $algorithms) |
|
32
|
|
|
{ |
|
33
|
|
|
foreach ($algorithms as $algorithm) { |
|
34
|
|
|
$this->add($algorithm); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param AlgorithmInterface[] $algorithms |
|
40
|
|
|
* |
|
41
|
|
|
* @return AlgorithmManager |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function create(array $algorithms): AlgorithmManager |
|
44
|
|
|
{ |
|
45
|
|
|
return new self($algorithms); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $algorithm The algorithm |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool Returns true if the algorithm is supported |
|
52
|
|
|
*/ |
|
53
|
|
|
public function has(string $algorithm): bool |
|
54
|
|
|
{ |
|
55
|
|
|
return array_key_exists($algorithm, $this->algorithms); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return string[] Returns the list of names of supported algorithms |
|
60
|
|
|
*/ |
|
61
|
|
|
public function list(): array |
|
62
|
|
|
{ |
|
63
|
|
|
return array_keys($this->algorithms); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $algorithm The algorithm |
|
68
|
|
|
* |
|
69
|
|
|
* @return AlgorithmInterface Returns JWAInterface object if the algorithm is supported, else null |
|
70
|
|
|
*/ |
|
71
|
|
|
public function get(string $algorithm): AlgorithmInterface |
|
72
|
|
|
{ |
|
73
|
|
|
if (!$this->has($algorithm)) { |
|
74
|
|
|
throw new \InvalidArgumentException(sprintf('The algorithm "%s" is not supported.', $algorithm)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this->algorithms[$algorithm]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param AlgorithmInterface $algorithm |
|
82
|
|
|
* |
|
83
|
|
|
* @return AlgorithmManager |
|
84
|
|
|
*/ |
|
85
|
|
|
private function add(AlgorithmInterface $algorithm): AlgorithmManager |
|
86
|
|
|
{ |
|
87
|
|
|
$name = $algorithm->name(); |
|
88
|
|
|
if ($this->has($name)) { |
|
89
|
|
|
throw new \InvalidArgumentException(sprintf('The algorithm "%s" is already supported.', $name)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->algorithms[$name] = $algorithm; |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|