2
2
3
3
namespace kamermans \OAuth2 \Utils ;
4
4
5
+ use ArrayAccess ;
6
+ use Traversable ;
7
+ use IteratorAggregate ;
8
+ use Countable ;
9
+ use ArrayIterator ;
10
+
5
11
/**
6
12
* Key value pair collection object
7
13
*/
8
14
class Collection implements
9
- \ ArrayAccess,
10
- \ IteratorAggregate,
11
- \ Countable
15
+ ArrayAccess,
16
+ IteratorAggregate,
17
+ Countable
12
18
{
13
19
14
20
/** @var array */
@@ -22,44 +28,37 @@ public function __construct(array $data = [])
22
28
$ this ->data = $ data ;
23
29
}
24
30
25
- #[\ReturnTypeWillChange]
26
- public function getIterator ()
31
+ public function getIterator (): Traversable
27
32
{
28
- return new \ ArrayIterator ($ this ->data );
33
+ return new ArrayIterator ($ this ->data );
29
34
}
30
35
31
- #[\ReturnTypeWillChange]
32
- public function offsetGet ($ offset )
36
+ public function offsetGet ($ offset ): ?string
33
37
{
34
38
return isset ($ this ->data [$ offset ]) ? $ this ->data [$ offset ] : null ;
35
39
}
36
40
37
- #[\ReturnTypeWillChange]
38
- public function offsetSet ($ offset , $ value )
41
+ public function offsetSet ($ offset , $ value ): void
39
42
{
40
43
$ this ->data [$ offset ] = $ value ;
41
44
}
42
45
43
- #[\ReturnTypeWillChange]
44
- public function offsetExists ($ offset )
46
+ public function offsetExists ($ offset ): bool
45
47
{
46
48
return isset ($ this ->data [$ offset ]);
47
49
}
48
50
49
- #[\ReturnTypeWillChange]
50
- public function offsetUnset ($ offset )
51
+ public function offsetUnset ($ offset ): void
51
52
{
52
53
unset($ this ->data [$ offset ]);
53
54
}
54
-
55
- #[\ReturnTypeWillChange]
56
- public function toArray ()
55
+
56
+ public function toArray (): array
57
57
{
58
58
return $ this ->data ;
59
59
}
60
60
61
- #[\ReturnTypeWillChange]
62
- public function count ()
61
+ public function count (): int
63
62
{
64
63
return count ($ this ->data );
65
64
}
@@ -79,7 +78,8 @@ public static function fromConfig(
79
78
array $ config = [],
80
79
array $ defaults = [],
81
80
array $ required = []
82
- ) {
81
+ ): Collection
82
+ {
83
83
$ data = $ config + $ defaults ;
84
84
85
85
if ($ missing = array_diff ($ required , array_keys ($ data ))) {
@@ -97,7 +97,7 @@ public static function fromConfig(
97
97
*
98
98
* @return Collection
99
99
*/
100
- public function clear ()
100
+ public function clear (): Collection
101
101
{
102
102
$ this ->data = [];
103
103
@@ -111,7 +111,7 @@ public function clear()
111
111
*
112
112
* @return mixed|null Value of the key or NULL
113
113
*/
114
- public function get ($ key )
114
+ public function get (string $ key ): string
115
115
{
116
116
return isset ($ this ->data [$ key ]) ? $ this ->data [$ key ] : null ;
117
117
}
@@ -120,11 +120,11 @@ public function get($key)
120
120
* Set a key value pair
121
121
*
122
122
* @param string $key Key to set
123
- * @param mixed $value Value to set
123
+ * @param string $value Value to set
124
124
*
125
125
* @return Collection Returns a reference to the object
126
126
*/
127
- public function set ($ key , $ value )
127
+ public function set (string $ key , string $ value ): Collection
128
128
{
129
129
$ this ->data [$ key ] = $ value ;
130
130
@@ -141,7 +141,7 @@ public function set($key, $value)
141
141
*
142
142
* @return Collection Returns a reference to the object.
143
143
*/
144
- public function add ($ key , $ value )
144
+ public function add ($ key , $ value ): Collection
145
145
{
146
146
if (!array_key_exists ($ key , $ this ->data )) {
147
147
$ this ->data [$ key ] = $ value ;
@@ -161,7 +161,7 @@ public function add($key, $value)
161
161
*
162
162
* @return Collection
163
163
*/
164
- public function remove ($ key )
164
+ public function remove (string $ key ): Collection
165
165
{
166
166
unset($ this ->data [$ key ]);
167
167
@@ -173,7 +173,7 @@ public function remove($key)
173
173
*
174
174
* @return array
175
175
*/
176
- public function getKeys ()
176
+ public function getKeys (): array
177
177
{
178
178
return array_keys ($ this ->data );
179
179
}
@@ -185,7 +185,7 @@ public function getKeys()
185
185
*
186
186
* @return bool
187
187
*/
188
- public function hasKey ($ key )
188
+ public function hasKey (string $ key ): bool
189
189
{
190
190
return array_key_exists ($ key , $ this ->data );
191
191
}
@@ -195,12 +195,13 @@ public function hasKey($key)
195
195
*
196
196
* @param string $value Value to search for
197
197
*
198
- * @return mixed Returns the key if the value was found FALSE if the value
198
+ * @return mixed Returns the key if the value was found or NULL if the value
199
199
* was not found.
200
200
*/
201
- public function hasValue ($ value )
201
+ public function hasValue (string $ value ): ? string
202
202
{
203
- return array_search ($ value , $ this ->data , true );
203
+ $ val = array_search ($ value , $ this ->data , true );
204
+ return $ val === false ? null : $ val ;
204
205
}
205
206
206
207
/**
@@ -210,7 +211,7 @@ public function hasValue($value)
210
211
*
211
212
* @return Collection Returns a reference to the object
212
213
*/
213
- public function replace (array $ data )
214
+ public function replace (array $ data ): Collection
214
215
{
215
216
$ this ->data = $ data ;
216
217
@@ -224,7 +225,7 @@ public function replace(array $data)
224
225
*
225
226
* @return Collection Returns a reference to the object.
226
227
*/
227
- public function merge ($ data )
228
+ public function merge (Traversable $ data ): Collection
228
229
{
229
230
foreach ($ data as $ key => $ value ) {
230
231
$ this ->add ($ key , $ value );
@@ -241,7 +242,7 @@ public function merge($data)
241
242
*
242
243
* @return self
243
244
*/
244
- public function overwriteWith ($ data )
245
+ public function overwriteWith (Traversable $ data ): Collection
245
246
{
246
247
if (is_array ($ data )) {
247
248
$ this ->data = $ data + $ this ->data ;
@@ -272,7 +273,7 @@ public function overwriteWith($data)
272
273
*
273
274
* @return Collection
274
275
*/
275
- public function map (callable $ closure , array $ context = [])
276
+ public function map (callable $ closure , array $ context = []): Collection
276
277
{
277
278
$ collection = new static ();
278
279
foreach ($ this as $ key => $ value ) {
@@ -295,7 +296,7 @@ public function map(callable $closure, array $context = [])
295
296
*
296
297
* @return Collection
297
298
*/
298
- public function filter (callable $ closure )
299
+ public function filter (callable $ closure ): Collection
299
300
{
300
301
$ collection = new static ();
301
302
foreach ($ this ->data as $ key => $ value ) {
0 commit comments