@@ -15,7 +15,7 @@ final class Dictionary implements Countable, IteratorAggregate, StructuredField
15
15
{
16
16
private function __construct (
17
17
/** @var array<string, Item|InnerList> */
18
- private array $ elements = []
18
+ private array $ members = []
19
19
) {
20
20
}
21
21
@@ -25,13 +25,13 @@ private function __construct(
25
25
* its keys represent the dictionary entry key
26
26
* its values represent the dictionary entry value
27
27
*
28
- * @param iterable<string, InnerList|Item|ByteSequence|Token|bool|int|float|string> $elements
28
+ * @param iterable<string, InnerList|Item|ByteSequence|Token|bool|int|float|string> $members
29
29
*/
30
- public static function fromAssociative (iterable $ elements = []): self
30
+ public static function fromAssociative (iterable $ members = []): self
31
31
{
32
32
$ instance = new self ();
33
- foreach ($ elements as $ index => $ element ) {
34
- $ instance ->set ($ index , $ element );
33
+ foreach ($ members as $ index => $ member ) {
34
+ $ instance ->set ($ index , $ member );
35
35
}
36
36
37
37
return $ instance ;
@@ -40,17 +40,17 @@ public static function fromAssociative(iterable $elements = []): self
40
40
/**
41
41
* Returns a new instance from a pair iterable construct.
42
42
*
43
- * Each element is composed of an array with two elements
44
- * the first element represents the instance entry key
45
- * the second element represents the instance entry value
43
+ * Each member is composed of an array with two elements
44
+ * the first member represents the instance entry key
45
+ * the second member represents the instance entry value
46
46
*
47
47
* @param iterable<array{0:string, 1:InnerList|Item|ByteSequence|Token|bool|int|float|string}> $pairs
48
48
*/
49
49
public static function fromPairs (iterable $ pairs = []): self
50
50
{
51
51
$ instance = new self ();
52
- foreach ($ pairs as [$ key , $ element ]) {
53
- $ instance ->set ($ key , $ element );
52
+ foreach ($ pairs as [$ key , $ member ]) {
53
+ $ instance ->set ($ key , $ member );
54
54
}
55
55
56
56
return $ instance ;
@@ -63,67 +63,16 @@ public static function fromPairs(iterable $pairs = []): self
63
63
*/
64
64
public static function fromHttpValue (string $ httpValue ): self
65
65
{
66
- $ instance = new self ();
67
- $ httpValue = trim ($ httpValue , ' ' );
68
- if ('' === $ httpValue ) {
69
- return $ instance ;
70
- }
71
-
72
- if (1 === preg_match ("/[^ \x20- \x7E\t]/ " , $ httpValue ) || str_starts_with ($ httpValue , "\t" )) {
73
- throw new SyntaxError ("The HTTP textual representation ` $ httpValue` for dictionary contains invalid characters. " );
74
- }
75
-
76
- $ parser = fn (string $ element ): Item |InnerList => str_starts_with ($ element , '( ' )
77
- ? InnerList::fromHttpValue ($ element )
78
- : Item::fromHttpValue ($ element );
79
-
80
- return array_reduce (explode (', ' , $ httpValue ), function (self $ instance , string $ element ) use ($ parser ): self {
81
- [$ key , $ value ] = self ::extractPair ($ element );
82
-
83
- $ instance ->set ($ key , $ parser ($ value ));
84
-
85
- return $ instance ;
86
- }, $ instance );
87
- }
88
-
89
- /**
90
- * Extracts a dictionary pair from an HTTP textual representation.
91
- *
92
- * @throws SyntaxError
93
- *
94
- * @return array{0:string, 1:string}
95
- */
96
- private static function extractPair (string $ pair ): array
97
- {
98
- $ pair = trim ($ pair );
99
-
100
- if ('' === $ pair ) {
101
- throw new SyntaxError ('The HTTP textual representation for a dictionary pair can not be empty. ' );
102
- }
103
-
104
- if (1 !== preg_match ('/^(?<key>[a-z*][a-z0-9.*_-]*)(=)?(?<value>.*)/ ' , $ pair , $ found )) {
105
- throw new SyntaxError ("The HTTP textual representation ` $ pair` for a dictionary pair contains invalid characters. " );
106
- }
107
-
108
- if (rtrim ($ found ['key ' ]) !== $ found ['key ' ] || ltrim ($ found ['value ' ]) !== $ found ['value ' ]) {
109
- throw new SyntaxError ("The HTTP textual representation ` $ pair` for a dictionary pair contains invalid characters. " );
110
- }
111
-
112
- $ found ['value ' ] = trim ($ found ['value ' ]);
113
- if ('' === $ found ['value ' ] || str_starts_with ($ found ['value ' ], '; ' )) {
114
- $ found ['value ' ] = '?1 ' .$ found ['value ' ];
115
- }
116
-
117
- return [$ found ['key ' ], $ found ['value ' ]];
66
+ return Parser::parseDictionary ($ httpValue );
118
67
}
119
68
120
69
public function toHttpValue (): string
121
70
{
122
71
$ returnValue = [];
123
- foreach ($ this ->elements as $ key => $ element ) {
72
+ foreach ($ this ->members as $ key => $ member ) {
124
73
$ returnValue [] = match (true ) {
125
- $ element instanceof Item && true === $ element ->value () => $ key .$ element ->parameters ()->toHttpValue (),
126
- default => $ key .'= ' .$ element ->toHttpValue (),
74
+ $ member instanceof Item && true === $ member ->value () => $ key .$ member ->parameters ()->toHttpValue (),
75
+ default => $ key .'= ' .$ member ->toHttpValue (),
127
76
};
128
77
}
129
78
@@ -132,21 +81,21 @@ public function toHttpValue(): string
132
81
133
82
public function count (): int
134
83
{
135
- return count ($ this ->elements );
84
+ return count ($ this ->members );
136
85
}
137
86
138
87
public function isEmpty (): bool
139
88
{
140
- return [] === $ this ->elements ;
89
+ return [] === $ this ->members ;
141
90
}
142
91
143
92
/**
144
93
* @return Iterator<string, Item|InnerList>
145
94
*/
146
95
public function getIterator (): Iterator
147
96
{
148
- foreach ($ this ->elements as $ index => $ element ) {
149
- yield $ index => $ element ;
97
+ foreach ($ this ->members as $ index => $ member ) {
98
+ yield $ index => $ member ;
150
99
}
151
100
}
152
101
@@ -157,8 +106,8 @@ public function getIterator(): Iterator
157
106
*/
158
107
public function toPairs (): Iterator
159
108
{
160
- foreach ($ this ->elements as $ index => $ element ) {
161
- yield [$ index , $ element ];
109
+ foreach ($ this ->members as $ index => $ member ) {
110
+ yield [$ index , $ member ];
162
111
}
163
112
}
164
113
@@ -167,21 +116,21 @@ public function toPairs(): Iterator
167
116
*/
168
117
public function keys (): array
169
118
{
170
- return array_keys ($ this ->elements );
119
+ return array_keys ($ this ->members );
171
120
}
172
121
173
122
public function has (string $ key ): bool
174
123
{
175
- return array_key_exists ($ key , $ this ->elements );
124
+ return array_key_exists ($ key , $ this ->members );
176
125
}
177
126
178
127
public function get (string $ key ): Item |InnerList
179
128
{
180
- if (!array_key_exists ($ key , $ this ->elements )) {
129
+ if (!array_key_exists ($ key , $ this ->members )) {
181
130
throw InvalidOffset::dueToKeyNotFound ($ key );
182
131
}
183
132
184
- return $ this ->elements [$ key ];
133
+ return $ this ->members [$ key ];
185
134
}
186
135
187
136
public function hasPair (int $ index ): bool
@@ -191,10 +140,10 @@ public function hasPair(int $index): bool
191
140
192
141
private function filterIndex (int $ index ): int |null
193
142
{
194
- $ max = count ($ this ->elements );
143
+ $ max = count ($ this ->members );
195
144
196
145
return match (true ) {
197
- [] === $ this ->elements , 0 > $ max + $ index , 0 > $ max - $ index - 1 => null ,
146
+ [] === $ this ->members , 0 > $ max + $ index , 0 > $ max - $ index - 1 => null ,
198
147
0 > $ index => $ max + $ index ,
199
148
default => $ index ,
200
149
};
@@ -211,19 +160,19 @@ public function pair(int $index): array
211
160
}
212
161
213
162
return [
214
- array_keys ($ this ->elements )[$ offset ],
215
- array_values ($ this ->elements )[$ offset ],
163
+ array_keys ($ this ->members )[$ offset ],
164
+ array_values ($ this ->members )[$ offset ],
216
165
];
217
166
}
218
167
219
168
/**
220
- * Add an element at the end of the instance if the key is new otherwise update the value associated with the key.
169
+ * Add a member at the end of the instance if the key is new otherwise update the value associated with the key.
221
170
*/
222
- public function set (string $ key , InnerList |Item |ByteSequence |Token |bool |int |float |string $ element ): void
171
+ public function set (string $ key , InnerList |Item |ByteSequence |Token |bool |int |float |string $ member ): void
223
172
{
224
173
self ::validateKey ($ key );
225
174
226
- $ this ->elements [$ key ] = self ::filterElement ( $ element );
175
+ $ this ->members [$ key ] = self ::filterMember ( $ member );
227
176
}
228
177
229
178
/**
@@ -236,54 +185,54 @@ private static function validateKey(string $key): void
236
185
}
237
186
}
238
187
239
- private static function filterElement (InnerList |Item |ByteSequence |Token |bool |int |float |string $ element ): InnerList |Item
188
+ private static function filterMember (InnerList |Item |ByteSequence |Token |bool |int |float |string $ member ): InnerList |Item
240
189
{
241
190
return match (true ) {
242
- $ element instanceof InnerList, $ element instanceof Item => $ element ,
243
- default => Item::from ($ element ),
191
+ $ member instanceof InnerList, $ member instanceof Item => $ member ,
192
+ default => Item::from ($ member ),
244
193
};
245
194
}
246
195
247
196
/**
248
- * Delete elements associated with the list of submitted keys.
197
+ * Delete members associated with the list of submitted keys.
249
198
*/
250
199
public function delete (string ...$ keys ): void
251
200
{
252
201
foreach ($ keys as $ key ) {
253
- unset($ this ->elements [$ key ]);
202
+ unset($ this ->members [$ key ]);
254
203
}
255
204
}
256
205
257
206
/**
258
- * Remove all elements from the instance.
207
+ * Remove all members from the instance.
259
208
*/
260
209
public function clear (): void
261
210
{
262
- $ this ->elements = [];
211
+ $ this ->members = [];
263
212
}
264
213
265
214
/**
266
- * Add an element at the end of the instance if the key is new delete any previous reference to the key.
215
+ * Add a member at the end of the instance if the key is new delete any previous reference to the key.
267
216
*/
268
- public function append (string $ key , InnerList |Item |ByteSequence |Token |bool |int |float |string $ element ): void
217
+ public function append (string $ key , InnerList |Item |ByteSequence |Token |bool |int |float |string $ member ): void
269
218
{
270
219
self ::validateKey ($ key );
271
220
272
- unset($ this ->elements [$ key ]);
221
+ unset($ this ->members [$ key ]);
273
222
274
- $ this ->elements [$ key ] = self ::filterElement ( $ element );
223
+ $ this ->members [$ key ] = self ::filterMember ( $ member );
275
224
}
276
225
277
226
/**
278
- * Add an element at the beginning of the instance if the key is new delete any previous reference to the key.
227
+ * Add a member at the beginning of the instance if the key is new delete any previous reference to the key.
279
228
*/
280
- public function prepend (string $ key , InnerList |Item |ByteSequence |Token |bool |int |float |string $ element ): void
229
+ public function prepend (string $ key , InnerList |Item |ByteSequence |Token |bool |int |float |string $ member ): void
281
230
{
282
231
self ::validateKey ($ key );
283
232
284
- unset($ this ->elements [$ key ]);
233
+ unset($ this ->members [$ key ]);
285
234
286
- $ this ->elements = [...[$ key => self ::filterElement ( $ element )], ...$ this ->elements ];
235
+ $ this ->members = [...[$ key => self ::filterMember ( $ member )], ...$ this ->members ];
287
236
}
288
237
289
238
/**
@@ -292,7 +241,7 @@ public function prepend(string $key, InnerList|Item|ByteSequence|Token|bool|int|
292
241
public function merge (self ...$ others ): void
293
242
{
294
243
foreach ($ others as $ other ) {
295
- $ this ->elements = [...$ this ->elements , ...$ other ->elements ];
244
+ $ this ->members = [...$ this ->members , ...$ other ->members ];
296
245
}
297
246
}
298
247
}
0 commit comments