@@ -14,167 +14,167 @@ |
||
14 | 14 | */ |
15 | 15 | class Claims implements \Countable, \IteratorAggregate |
16 | 16 | { |
17 | - use TypedClaims; |
|
18 | - |
|
19 | - /** |
|
20 | - * Claims. |
|
21 | - * |
|
22 | - * @var Claim[] |
|
23 | - */ |
|
24 | - protected $_claims; |
|
25 | - |
|
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param Claim ...$claims Zero or more claims |
|
30 | - */ |
|
31 | - public function __construct(Claim ...$claims) |
|
32 | - { |
|
33 | - $this->_claims = []; |
|
34 | - foreach ($claims as $claim) { |
|
35 | - $this->_claims[$claim->name()] = $claim; |
|
36 | - } |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Convert to string. |
|
41 | - * |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - public function __toString(): string |
|
45 | - { |
|
46 | - return $this->toJSON(); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Initialize from a JSON string. |
|
51 | - * |
|
52 | - * @param string $json JSON |
|
53 | - * |
|
54 | - * @throws \UnexpectedValueException If JSON is malformed |
|
55 | - * |
|
56 | - * @return self |
|
57 | - */ |
|
58 | - public static function fromJSON(string $json): self |
|
59 | - { |
|
60 | - $claims = []; |
|
61 | - $fields = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
62 | - if (!is_array($fields)) { |
|
63 | - throw new \UnexpectedValueException('Invalid JSON.'); |
|
64 | - } |
|
65 | - foreach ($fields as $name => $value) { |
|
66 | - $claims[] = Claim::fromNameAndValue($name, $value); |
|
67 | - } |
|
68 | - return new self(...$claims); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Get self with Claim objects added. |
|
73 | - * |
|
74 | - * @param Claim ...$claims One or more Claim objects |
|
75 | - * |
|
76 | - * @return self |
|
77 | - */ |
|
78 | - public function withClaims(Claim ...$claims): self |
|
79 | - { |
|
80 | - $obj = clone $this; |
|
81 | - foreach ($claims as $claim) { |
|
82 | - $obj->_claims[$claim->name()] = $claim; |
|
83 | - } |
|
84 | - return $obj; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Get all claims. |
|
89 | - * |
|
90 | - * @return Claim[] |
|
91 | - */ |
|
92 | - public function all(): array |
|
93 | - { |
|
94 | - return $this->_claims; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Check whether claim is present. |
|
99 | - * |
|
100 | - * @param string $name Claim name |
|
101 | - * |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public function has(string $name): bool |
|
105 | - { |
|
106 | - return isset($this->_claims[$name]); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Get claim by name. |
|
111 | - * |
|
112 | - * @param string $name Claim name |
|
113 | - * |
|
114 | - * @throws \LogicException If claim is not present |
|
115 | - * |
|
116 | - * @return Claim |
|
117 | - */ |
|
118 | - public function get(string $name): Claim |
|
119 | - { |
|
120 | - if (!isset($this->_claims[$name])) { |
|
121 | - throw new \LogicException("Claim {$name} not set."); |
|
122 | - } |
|
123 | - return $this->_claims[$name]; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Convert to a JSON. |
|
128 | - * |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - public function toJSON(): string |
|
132 | - { |
|
133 | - $data = []; |
|
134 | - foreach ($this->_claims as $claim) { |
|
135 | - $data[$claim->name()] = $claim->value(); |
|
136 | - } |
|
137 | - return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Check whether a claims set is valid in the given context. |
|
142 | - * |
|
143 | - * @param ValidationContext $ctx Validation context |
|
144 | - * |
|
145 | - * @return bool |
|
146 | - */ |
|
147 | - public function isValid(ValidationContext $ctx): bool |
|
148 | - { |
|
149 | - try { |
|
150 | - $ctx->validate($this); |
|
151 | - } catch (\RuntimeException $e) { |
|
152 | - return false; |
|
153 | - } |
|
154 | - return true; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Get the number of claims. |
|
159 | - * |
|
160 | - * @see \Countable::count() |
|
161 | - * |
|
162 | - * @return int |
|
163 | - */ |
|
164 | - public function count(): int |
|
165 | - { |
|
166 | - return count($this->_claims); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Get iterator for Claim objects keyed by claim name. |
|
171 | - * |
|
172 | - * @see \IteratorAggregate::getIterator() |
|
173 | - * |
|
174 | - * @return \ArrayIterator |
|
175 | - */ |
|
176 | - public function getIterator(): \ArrayIterator |
|
177 | - { |
|
178 | - return new \ArrayIterator($this->_claims); |
|
179 | - } |
|
17 | + use TypedClaims; |
|
18 | + |
|
19 | + /** |
|
20 | + * Claims. |
|
21 | + * |
|
22 | + * @var Claim[] |
|
23 | + */ |
|
24 | + protected $_claims; |
|
25 | + |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param Claim ...$claims Zero or more claims |
|
30 | + */ |
|
31 | + public function __construct(Claim ...$claims) |
|
32 | + { |
|
33 | + $this->_claims = []; |
|
34 | + foreach ($claims as $claim) { |
|
35 | + $this->_claims[$claim->name()] = $claim; |
|
36 | + } |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Convert to string. |
|
41 | + * |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + public function __toString(): string |
|
45 | + { |
|
46 | + return $this->toJSON(); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Initialize from a JSON string. |
|
51 | + * |
|
52 | + * @param string $json JSON |
|
53 | + * |
|
54 | + * @throws \UnexpectedValueException If JSON is malformed |
|
55 | + * |
|
56 | + * @return self |
|
57 | + */ |
|
58 | + public static function fromJSON(string $json): self |
|
59 | + { |
|
60 | + $claims = []; |
|
61 | + $fields = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
62 | + if (!is_array($fields)) { |
|
63 | + throw new \UnexpectedValueException('Invalid JSON.'); |
|
64 | + } |
|
65 | + foreach ($fields as $name => $value) { |
|
66 | + $claims[] = Claim::fromNameAndValue($name, $value); |
|
67 | + } |
|
68 | + return new self(...$claims); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Get self with Claim objects added. |
|
73 | + * |
|
74 | + * @param Claim ...$claims One or more Claim objects |
|
75 | + * |
|
76 | + * @return self |
|
77 | + */ |
|
78 | + public function withClaims(Claim ...$claims): self |
|
79 | + { |
|
80 | + $obj = clone $this; |
|
81 | + foreach ($claims as $claim) { |
|
82 | + $obj->_claims[$claim->name()] = $claim; |
|
83 | + } |
|
84 | + return $obj; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Get all claims. |
|
89 | + * |
|
90 | + * @return Claim[] |
|
91 | + */ |
|
92 | + public function all(): array |
|
93 | + { |
|
94 | + return $this->_claims; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Check whether claim is present. |
|
99 | + * |
|
100 | + * @param string $name Claim name |
|
101 | + * |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public function has(string $name): bool |
|
105 | + { |
|
106 | + return isset($this->_claims[$name]); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Get claim by name. |
|
111 | + * |
|
112 | + * @param string $name Claim name |
|
113 | + * |
|
114 | + * @throws \LogicException If claim is not present |
|
115 | + * |
|
116 | + * @return Claim |
|
117 | + */ |
|
118 | + public function get(string $name): Claim |
|
119 | + { |
|
120 | + if (!isset($this->_claims[$name])) { |
|
121 | + throw new \LogicException("Claim {$name} not set."); |
|
122 | + } |
|
123 | + return $this->_claims[$name]; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Convert to a JSON. |
|
128 | + * |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + public function toJSON(): string |
|
132 | + { |
|
133 | + $data = []; |
|
134 | + foreach ($this->_claims as $claim) { |
|
135 | + $data[$claim->name()] = $claim->value(); |
|
136 | + } |
|
137 | + return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Check whether a claims set is valid in the given context. |
|
142 | + * |
|
143 | + * @param ValidationContext $ctx Validation context |
|
144 | + * |
|
145 | + * @return bool |
|
146 | + */ |
|
147 | + public function isValid(ValidationContext $ctx): bool |
|
148 | + { |
|
149 | + try { |
|
150 | + $ctx->validate($this); |
|
151 | + } catch (\RuntimeException $e) { |
|
152 | + return false; |
|
153 | + } |
|
154 | + return true; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Get the number of claims. |
|
159 | + * |
|
160 | + * @see \Countable::count() |
|
161 | + * |
|
162 | + * @return int |
|
163 | + */ |
|
164 | + public function count(): int |
|
165 | + { |
|
166 | + return count($this->_claims); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Get iterator for Claim objects keyed by claim name. |
|
171 | + * |
|
172 | + * @see \IteratorAggregate::getIterator() |
|
173 | + * |
|
174 | + * @return \ArrayIterator |
|
175 | + */ |
|
176 | + public function getIterator(): \ArrayIterator |
|
177 | + { |
|
178 | + return new \ArrayIterator($this->_claims); |
|
179 | + } |
|
180 | 180 | } |
@@ -15,28 +15,28 @@ |
||
15 | 15 | */ |
16 | 16 | trait Base64UIntValue |
17 | 17 | { |
18 | - use Base64URLValue; |
|
18 | + use Base64URLValue; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Initialize parameter from base10 number. |
|
22 | - * |
|
23 | - * @param int|string $number |
|
24 | - * |
|
25 | - * @return self |
|
26 | - */ |
|
27 | - public static function fromNumber($number): Parameter |
|
28 | - { |
|
29 | - $data = BigInt::fromBase10($number)->base256(); |
|
30 | - return static::fromString($data); |
|
31 | - } |
|
20 | + /** |
|
21 | + * Initialize parameter from base10 number. |
|
22 | + * |
|
23 | + * @param int|string $number |
|
24 | + * |
|
25 | + * @return self |
|
26 | + */ |
|
27 | + public static function fromNumber($number): Parameter |
|
28 | + { |
|
29 | + $data = BigInt::fromBase10($number)->base256(); |
|
30 | + return static::fromString($data); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Get value as a number. |
|
35 | - * |
|
36 | - * @return BigInt |
|
37 | - */ |
|
38 | - public function number(): BigInt |
|
39 | - { |
|
40 | - return BigInt::fromBase256(Base64::urlDecode($this->value())); |
|
41 | - } |
|
33 | + /** |
|
34 | + * Get value as a number. |
|
35 | + * |
|
36 | + * @return BigInt |
|
37 | + */ |
|
38 | + public function number(): BigInt |
|
39 | + { |
|
40 | + return BigInt::fromBase256(Base64::urlDecode($this->value())); |
|
41 | + } |
|
42 | 42 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\Parameter\Feature; |
6 | 6 |