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