1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 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
|
|
|
/** |
17
|
|
|
* This manager handles as many claim checkers as needed. |
18
|
|
|
*/ |
19
|
|
|
class ClaimCheckerManager |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ClaimChecker[] |
23
|
|
|
*/ |
24
|
|
|
private $checkers = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* ClaimCheckerManager constructor. |
28
|
|
|
* |
29
|
|
|
* @param ClaimChecker[] $checkers |
30
|
|
|
*/ |
31
|
|
|
private function __construct(array $checkers) |
32
|
|
|
{ |
33
|
|
|
foreach ($checkers as $checker) { |
34
|
|
|
$this->add($checker); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* This method creates the ClaimCheckerManager. |
40
|
|
|
* The argument is a list of claim checkers objects. |
41
|
|
|
* |
42
|
|
|
* @param ClaimChecker[] $checkers |
43
|
|
|
* |
44
|
|
|
* @return ClaimCheckerManager |
45
|
|
|
*/ |
46
|
|
|
public static function create(array $checkers): self |
47
|
|
|
{ |
48
|
|
|
return new self($checkers); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ClaimChecker $checker |
53
|
|
|
* |
54
|
|
|
* @return ClaimCheckerManager |
55
|
|
|
*/ |
56
|
|
|
private function add(ClaimChecker $checker): self |
57
|
|
|
{ |
58
|
|
|
$claim = $checker->supportedClaim(); |
59
|
|
|
$this->checkers[$claim] = $checker; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* This method returns all checkers handled by this manager. |
66
|
|
|
* |
67
|
|
|
* @return ClaimChecker[] |
68
|
|
|
*/ |
69
|
|
|
public function getCheckers(): array |
70
|
|
|
{ |
71
|
|
|
return $this->checkers; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* This method checks all the claims passed as argument. |
76
|
|
|
* All claims are checked against the claim checkers. |
77
|
|
|
* If one fails, the InvalidClaimException is thrown. |
78
|
|
|
* |
79
|
|
|
* This method returns an array with all checked claims. |
80
|
|
|
* It is up to the implementor to decide use the claims that have not been checked. |
81
|
|
|
* |
82
|
|
|
* @param array $claims |
83
|
|
|
* @param string[] $mandatoryClaims |
84
|
|
|
* |
85
|
|
|
* @throws InvalidClaimException |
86
|
|
|
* @throws MissingMandatoryClaimException |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function check(array $claims, array $mandatoryClaims = []): array |
91
|
|
|
{ |
92
|
|
|
$this->checkMandatoryClaims($mandatoryClaims, $claims); |
93
|
|
|
$checkedClaims = []; |
94
|
|
|
foreach ($this->checkers as $claim => $checker) { |
95
|
|
|
if (array_key_exists($claim, $claims)) { |
96
|
|
|
$checker->checkClaim($claims[$claim]); |
97
|
|
|
$checkedClaims[$claim] = $claims[$claim]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $checkedClaims; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string[] $mandatoryClaims |
106
|
|
|
* @param array $claims |
107
|
|
|
* |
108
|
|
|
* @throws MissingMandatoryClaimException |
109
|
|
|
*/ |
110
|
|
|
private function checkMandatoryClaims(array $mandatoryClaims, array $claims) |
111
|
|
|
{ |
112
|
|
|
if (empty($mandatoryClaims)) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
$diff = array_keys(array_diff_key(array_flip($mandatoryClaims), $claims)); |
116
|
|
|
|
117
|
|
|
if (!empty($diff)) { |
118
|
|
|
throw new MissingMandatoryClaimException(sprintf('The following claims are mandatory: %s.', implode(', ', $diff)), $diff); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|