1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
7
|
|
|
* |
8
|
|
|
* This software may be modified and distributed under the terms |
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jose\Object; |
13
|
|
|
|
14
|
|
|
use Assert\Assertion; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class JWKSet. |
18
|
|
|
*/ |
19
|
|
|
final class JWKSet implements JWKSetInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $position = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $keys = []; |
30
|
|
|
|
31
|
|
|
public function __construct(array $keys = []) |
32
|
|
|
{ |
33
|
|
|
if (array_key_exists('keys', $keys)) { |
34
|
|
|
foreach ($keys['keys'] as $value) { |
35
|
|
|
$this->addKey(new JWK($value)); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function hasKey($index) |
44
|
|
|
{ |
45
|
|
|
return array_key_exists($index, $this->keys); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getKey($index) |
52
|
|
|
{ |
53
|
|
|
Assertion::keyExists($this->keys, $index, 'Undefined index.'); |
54
|
|
|
|
55
|
|
|
return $this->keys[$index]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getKeys() |
62
|
|
|
{ |
63
|
|
|
return $this->keys; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function addKey(JWKInterface $key) |
70
|
|
|
{ |
71
|
|
|
$this->keys[] = $key; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function removeKey($key) |
78
|
|
|
{ |
79
|
|
|
if (isset($this->keys[$key])) { |
80
|
|
|
unset($this->keys[$key]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function jsonSerialize() |
88
|
|
|
{ |
89
|
|
|
return ['keys' => array_values($this->getKeys())]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function count($mode = COUNT_NORMAL) |
96
|
|
|
{ |
97
|
|
|
return count($this->getKeys(), $mode); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function current() |
104
|
|
|
{ |
105
|
|
|
return isset($this->keys[$this->position]) ? $this->keys[$this->position] : null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function key() |
112
|
|
|
{ |
113
|
|
|
return $this->position; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function next() |
120
|
|
|
{ |
121
|
|
|
$this->position++; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function rewind() |
128
|
|
|
{ |
129
|
|
|
$this->position = 0; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
public function valid() |
136
|
|
|
{ |
137
|
|
|
return $this->current() instanceof JWKInterface; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function countKeys() |
144
|
|
|
{ |
145
|
|
|
return count($this->keys); |
146
|
|
|
} |
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function offsetExists($offset) |
151
|
|
|
{ |
152
|
|
|
return $this->hasKey($offset); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function offsetGet($offset) |
159
|
|
|
{ |
160
|
|
|
return $this->getKey($offset); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function offsetSet($offset, $value) |
167
|
|
|
{ |
168
|
|
|
$this->addKey($value); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
|
public function offsetUnset($offset) |
175
|
|
|
{ |
176
|
|
|
$this->removeKey($offset); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|