9
9
use IteratorAggregate ;
10
10
use function array_key_exists ;
11
11
use function array_keys ;
12
- use function array_values ;
13
12
use function count ;
14
13
use function explode ;
15
14
use function ltrim ;
@@ -24,7 +23,7 @@ final class Parameters implements Countable, IteratorAggregate, StructuredField
24
23
{
25
24
private function __construct (
26
25
/** @var array<string, Item> */
27
- private array $ members = []
26
+ private array $ members
28
27
) {
29
28
}
30
29
@@ -46,7 +45,7 @@ public static function __set_state(array $properties): self
46
45
*/
47
46
public static function fromAssociative (iterable $ members = []): self
48
47
{
49
- $ instance = new self ();
48
+ $ instance = new self ([] );
50
49
foreach ($ members as $ key => $ member ) {
51
50
$ instance ->set ($ key , $ member );
52
51
}
@@ -65,7 +64,7 @@ public static function fromAssociative(iterable $members = []): self
65
64
*/
66
65
public static function fromPairs (iterable $ pairs = []): self
67
66
{
68
- $ instance = new self ();
67
+ $ instance = new self ([] );
69
68
foreach ($ pairs as [$ key , $ member ]) {
70
69
$ instance ->set ($ key , $ member );
71
70
}
@@ -80,7 +79,7 @@ public static function fromPairs(iterable $pairs = []): self
80
79
*/
81
80
public static function fromHttpValue (string $ httpValue ): self
82
81
{
83
- $ instance = new self ();
82
+ $ instance = new self ([] );
84
83
if ('' === $ httpValue ) {
85
84
return $ instance ;
86
85
}
@@ -105,9 +104,7 @@ public function toHttpValue(): string
105
104
$ returnValue = [];
106
105
107
106
foreach ($ this ->members as $ key => $ val ) {
108
- if (!$ val ->parameters ->isEmpty ()) {
109
- throw new ForbiddenStateError ('Parameters instances can not contain parameterized Items. ' );
110
- }
107
+ $ val = $ this ->validateMember ($ val );
111
108
112
109
$ value = '; ' .$ key ;
113
110
if ($ val ->value !== true ) {
@@ -120,6 +117,15 @@ public function toHttpValue(): string
120
117
return implode ('' , $ returnValue );
121
118
}
122
119
120
+ private function validateMember (Item $ item ): Item
121
+ {
122
+ if (!$ item ->parameters ->isEmpty ()) {
123
+ throw new ForbiddenStateError ('Parameters instances can not contain parameterized Items. ' );
124
+ }
125
+
126
+ return $ item ;
127
+ }
128
+
123
129
public function count (): int
124
130
{
125
131
return count ($ this ->members );
@@ -138,8 +144,8 @@ public function isEmpty(): bool
138
144
*/
139
145
public function getIterator (): Iterator
140
146
{
141
- foreach ($ this ->members as $ key => $ value ) {
142
- yield $ key => $ value ;
147
+ foreach ($ this ->members as $ key => $ member ) {
148
+ yield $ key => $ this -> validateMember ( $ member ) ;
143
149
}
144
150
}
145
151
@@ -151,7 +157,7 @@ public function getIterator(): Iterator
151
157
public function toPairs (): Iterator
152
158
{
153
159
foreach ($ this ->members as $ index => $ member ) {
154
- yield [$ index , $ member ];
160
+ yield [$ index , $ this -> validateMember ( $ member) ];
155
161
}
156
162
}
157
163
@@ -172,7 +178,7 @@ public function keys(): array
172
178
*/
173
179
public function values (): array
174
180
{
175
- return array_map (fn (Item $ item ): Token |ByteSequence |float |int |bool |string => $ item ->value , $ this ->members );
181
+ return array_map (fn (Item $ item ): Token |ByteSequence |float |int |bool |string => $ this -> validateMember ( $ item) ->value , $ this ->members );
176
182
}
177
183
178
184
/**
@@ -198,12 +204,7 @@ public function get(string $key): Item
198
204
throw InvalidOffset::dueToKeyNotFound ($ key );
199
205
}
200
206
201
- $ item = $ this ->members [$ key ];
202
- if (!$ item ->parameters ->isEmpty ()) {
203
- throw new ForbiddenStateError ('Parameters instances can not contain parameterized Items. ' );
204
- }
205
-
206
- return $ item ;
207
+ return $ this ->validateMember ($ this ->members [$ key ]);
207
208
}
208
209
209
210
/**
@@ -217,12 +218,7 @@ public function value(string $key): Token|ByteSequence|float|int|bool|string|nul
217
218
return null ;
218
219
}
219
220
220
- $ item = $ this ->members [$ key ];
221
- if (!$ item ->parameters ->isEmpty ()) {
222
- throw new ForbiddenStateError ('Parameters instances can not contain parameterized Items. ' );
223
- }
224
-
225
- return $ item ->value ;
221
+ return $ this ->validateMember ($ this ->members [$ key ])->value ;
226
222
}
227
223
228
224
/**
@@ -259,12 +255,15 @@ public function pair(int $index): array
259
255
throw InvalidOffset::dueToIndexNotFound ($ index );
260
256
}
261
257
262
- $ item = array_values ($ this ->members )[$ offset ];
263
- if (!$ item ->parameters ->isEmpty ()) {
264
- throw new ForbiddenStateError ('Parameters instances can not contain parameterized Items. ' );
258
+ foreach ($ this ->toPairs () as $ k => $ pair ) {
259
+ if ($ k === $ offset ) {
260
+ return $ pair ;
261
+ }
265
262
}
266
263
267
- return [array_keys ($ this ->members )[$ offset ], $ item ];
264
+ // @codeCoverageIgnoreStart
265
+ throw InvalidOffset::dueToIndexNotFound ($ index );
266
+ // @codeCoverageIgnoreEnd
268
267
}
269
268
270
269
/**
0 commit comments