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\Checker; |
15
|
|
|
|
16
|
|
|
use Jose\Component\Core\Converter\JsonConverterInterface; |
17
|
|
|
|
18
|
|
|
final class ClaimCheckerManagerFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ClaimCheckerInterface[] |
22
|
|
|
*/ |
23
|
|
|
private $checkers = []; |
24
|
|
|
/** |
25
|
|
|
* @var JsonConverterInterface |
26
|
|
|
*/ |
27
|
|
|
private $payloadEncoder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ClaimCheckerManager constructor. |
31
|
|
|
* |
32
|
|
|
* @param JsonConverterInterface $payloadEncoder |
33
|
|
|
*/ |
34
|
|
|
public function __construct(JsonConverterInterface $payloadEncoder) |
35
|
|
|
{ |
36
|
|
|
$this->payloadEncoder = $payloadEncoder; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string[] $aliases |
41
|
|
|
* |
42
|
|
|
* @return ClaimCheckerManager |
43
|
|
|
*/ |
44
|
|
|
public function create(array $aliases): ClaimCheckerManager |
45
|
|
|
{ |
46
|
|
|
$checkers = []; |
47
|
|
|
foreach ($aliases as $alias) { |
48
|
|
|
if (array_key_exists($alias, $this->checkers)) { |
49
|
|
|
$checkers[] = $this->checkers[$alias]; |
50
|
|
|
} else { |
51
|
|
|
throw new \InvalidArgumentException(sprintf('The claim checker with the alias "%s" is not supported.', $alias)); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return new ClaimCheckerManager($this->payloadEncoder, $checkers); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $alias |
60
|
|
|
* @param ClaimCheckerInterface $checker |
61
|
|
|
* |
62
|
|
|
* @return ClaimCheckerManagerFactory |
63
|
|
|
*/ |
64
|
|
|
public function add(string $alias, ClaimCheckerInterface $checker): ClaimCheckerManagerFactory |
65
|
|
|
{ |
66
|
|
|
if (array_key_exists($alias, $this->checkers)) { |
67
|
|
|
throw new \InvalidArgumentException(sprintf('The alias "%s" already exists.', $alias)); |
68
|
|
|
} |
69
|
|
|
$this->checkers[$alias] = $checker; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string[] |
76
|
|
|
*/ |
77
|
|
|
public function aliases(): array |
78
|
|
|
{ |
79
|
|
|
return array_keys($this->checkers); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|