|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWT\Header; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\JWX\JWT\Parameter\JWTParameter; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Represents a header used in JWS and JWE. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Header implements \Countable, \IteratorAggregate |
|
13
|
|
|
{ |
|
14
|
|
|
use TypedHeader; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Parameters. |
|
18
|
|
|
* |
|
19
|
|
|
* @var JWTParameter[] |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $_parameters; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param JWTParameter ...$params Parameters |
|
27
|
|
|
*/ |
|
28
|
188 |
|
public function __construct(JWTParameter ...$params) |
|
29
|
|
|
{ |
|
30
|
188 |
|
$this->_parameters = []; |
|
31
|
188 |
|
foreach ($params as $param) { |
|
32
|
116 |
|
$this->_parameters[$param->name()] = $param; |
|
33
|
|
|
} |
|
34
|
188 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Initialize from an array representing a JSON object. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $members |
|
40
|
|
|
* |
|
41
|
|
|
* @return self |
|
42
|
|
|
*/ |
|
43
|
52 |
|
public static function fromArray(array $members): self |
|
44
|
|
|
{ |
|
45
|
52 |
|
$params = []; |
|
46
|
52 |
|
foreach ($members as $name => $value) { |
|
47
|
52 |
|
$params[] = JWTParameter::fromNameAndValue($name, $value); |
|
48
|
|
|
} |
|
49
|
52 |
|
return new self(...$params); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initialize from a JSON. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $json |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \UnexpectedValueException |
|
58
|
|
|
* |
|
59
|
|
|
* @return self |
|
60
|
|
|
*/ |
|
61
|
39 |
|
public static function fromJSON(string $json): self |
|
62
|
|
|
{ |
|
63
|
39 |
|
$members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
64
|
39 |
|
if (!is_array($members)) { |
|
65
|
1 |
|
throw new \UnexpectedValueException('Invalid JSON.'); |
|
66
|
|
|
} |
|
67
|
38 |
|
return self::fromArray($members); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get self with parameters added. |
|
72
|
|
|
* |
|
73
|
|
|
* @param JWTParameter ...$param |
|
74
|
|
|
* |
|
75
|
|
|
* @return self |
|
76
|
|
|
*/ |
|
77
|
35 |
|
public function withParameters(JWTParameter ...$params): self |
|
78
|
|
|
{ |
|
79
|
35 |
|
$obj = clone $this; |
|
80
|
35 |
|
foreach ($params as $param) { |
|
81
|
35 |
|
$obj->_parameters[$param->name()] = $param; |
|
82
|
|
|
} |
|
83
|
35 |
|
return $obj; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get all parameters. |
|
88
|
|
|
* |
|
89
|
|
|
* @return JWTParameter[] |
|
90
|
|
|
*/ |
|
91
|
51 |
|
public function parameters(): array |
|
92
|
|
|
{ |
|
93
|
51 |
|
return $this->_parameters; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Whether parameters are present. |
|
98
|
|
|
* |
|
99
|
|
|
* Returns false if any of the given parameters is not set. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string ...$names Parameter names |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
175 |
|
public function has(string ...$names): bool |
|
106
|
|
|
{ |
|
107
|
175 |
|
foreach ($names as $name) { |
|
108
|
175 |
|
if (!isset($this->_parameters[$name])) { |
|
109
|
175 |
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
144 |
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get a parameter. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $name Parameter name |
|
119
|
|
|
* |
|
120
|
|
|
* @throws \LogicException |
|
121
|
|
|
* |
|
122
|
|
|
* @return JWTParameter |
|
123
|
|
|
*/ |
|
124
|
122 |
|
public function get(string $name): JWTParameter |
|
125
|
|
|
{ |
|
126
|
122 |
|
if (!$this->has($name)) { |
|
127
|
1 |
|
throw new \LogicException("Parameter {$name} doesn't exists."); |
|
128
|
|
|
} |
|
129
|
121 |
|
return $this->_parameters[$name]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Convert to a JSON. |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
76 |
|
public function toJSON(): string |
|
138
|
|
|
{ |
|
139
|
76 |
|
if (empty($this->_parameters)) { |
|
140
|
1 |
|
return ''; |
|
141
|
|
|
} |
|
142
|
75 |
|
$data = []; |
|
143
|
75 |
|
foreach ($this->_parameters as $param) { |
|
144
|
75 |
|
$data[$param->name()] = $param->value(); |
|
145
|
|
|
} |
|
146
|
75 |
|
return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get the number of parameters. |
|
151
|
|
|
* |
|
152
|
|
|
* @see \Countable::count() |
|
153
|
|
|
* |
|
154
|
|
|
* @return int |
|
155
|
|
|
*/ |
|
156
|
3 |
|
public function count(): int |
|
157
|
|
|
{ |
|
158
|
3 |
|
return count($this->_parameters); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get iterator for the parameters. |
|
163
|
|
|
* |
|
164
|
|
|
* @see \IteratorAggregate::getIterator() |
|
165
|
|
|
* |
|
166
|
|
|
* @return \ArrayIterator |
|
167
|
|
|
*/ |
|
168
|
1 |
|
public function getIterator(): \ArrayIterator |
|
169
|
|
|
{ |
|
170
|
1 |
|
return new \ArrayIterator($this->_parameters); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|