6
6
7
7
use function in_array ;
8
8
use function ltrim ;
9
- use function ord ;
10
9
use function preg_match ;
11
10
use function strlen ;
12
11
use function substr ;
@@ -29,8 +28,8 @@ final class Parser
29
28
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1
30
29
*
31
30
* @return array<array{
32
- * 0:array<Item|ByteSequence|Token|bool|int|float|string>,
33
- * 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
31
+ * 0:array<Item|ByteSequence|Token|bool|int|float|string>,
32
+ * 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
34
33
* }|Item|ByteSequence|Token|bool|int|float|string>
35
34
*/
36
35
public static function parseList (string $ httpValue ): array
@@ -51,17 +50,17 @@ public static function parseList(string $httpValue): array
51
50
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.2
52
51
*
53
52
* @return array<string, Item|ByteSequence|Token|array{
54
- * 0:array<Item|ByteSequence|Token|bool|int|float|string>,
55
- * 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
53
+ * 0:array<Item|ByteSequence|Token|bool|int|float|string>,
54
+ * 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
56
55
* }|bool|int|float|string>
57
56
*/
58
57
public static function parseDictionary (string $ httpValue ): array
59
58
{
60
59
$ map = [];
61
60
$ remainder = ltrim ($ httpValue , ' ' );
62
61
while ('' !== $ remainder ) {
63
- [ $ key, $ offset ] = self :: parseKey ($ remainder );
64
- $ remainder = substr ($ remainder , $ offset );
62
+ $ key = MapKey:: fromStringBeginning ($ remainder )-> value ;
63
+ $ remainder = substr ($ remainder , strlen ( $ key ) );
65
64
if ('' === $ remainder || '= ' !== $ remainder [0 ]) {
66
65
$ remainder = '=?1 ' .$ remainder ;
67
66
}
@@ -79,8 +78,8 @@ public static function parseDictionary(string $httpValue): array
79
78
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1.2
80
79
*
81
80
* @return array{
82
- * 0:array<Item|ByteSequence|Token|bool|int|float|string>,
83
- * 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
81
+ * 0:array<Item|ByteSequence|Token|bool|int|float|string>,
82
+ * 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
84
83
* }
85
84
*/
86
85
public static function parseInnerList (string $ httpValue ): array
@@ -92,7 +91,6 @@ public static function parseInnerList(string $httpValue): array
92
91
93
92
[$ list , $ offset ] = self ::parseInnerListValue ($ remainder );
94
93
$ remainder = self ::removeOptionalWhiteSpaces (substr ($ remainder , $ offset ));
95
-
96
94
if ('' !== $ remainder ) {
97
95
throw new SyntaxError ("The HTTP textual representation ` $ httpValue` for a inner list contains invalid data. " );
98
96
}
@@ -140,8 +138,8 @@ private static function removeOptionalWhiteSpaces(string $httpValue): string
140
138
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1.1
141
139
*
142
140
* @return array{0: array{
143
- * 0:array<Item|ByteSequence|Token|bool|int|float|string>,
144
- * 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
141
+ * 0:array<Item|ByteSequence|Token|bool|int|float|string>,
142
+ * 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
145
143
* }|Item, 1:int}
146
144
*/
147
145
private static function parseItemOrInnerList (string $ httpValue ): array
@@ -191,7 +189,6 @@ private static function parseInnerListValue(string $httpValue): array
191
189
$ remainder = substr ($ remainder , $ offset );
192
190
193
191
$ list [] = Item::from ($ value , $ parameters );
194
-
195
192
if ('' !== $ remainder && !in_array ($ remainder [0 ], [' ' , ') ' ], true )) {
196
193
throw new SyntaxError ("The HTTP textual representation ` $ remainder` for a inner list is using invalid characters. " );
197
194
}
@@ -233,10 +230,10 @@ private static function parseParameters(string $httpValue): array
233
230
while ('' !== $ remainder && '; ' === $ remainder [0 ]) {
234
231
$ remainder = ltrim (substr ($ remainder , 1 ), ' ' );
235
232
236
- [ $ key, $ keyOffset ] = self :: parseKey ($ remainder );
233
+ $ key = MapKey:: fromStringBeginning ($ remainder )-> value ;
237
234
$ map [$ key ] = true ;
238
235
239
- $ remainder = substr ($ remainder , $ keyOffset );
236
+ $ remainder = substr ($ remainder , strlen ( $ key ) );
240
237
if ('' !== $ remainder && '= ' === $ remainder [0 ]) {
241
238
$ remainder = substr ($ remainder , 1 );
242
239
@@ -248,22 +245,6 @@ private static function parseParameters(string $httpValue): array
248
245
return [$ map , strlen ($ httpValue ) - strlen ($ remainder )];
249
246
}
250
247
251
- /**
252
- * Returns a Dictionary or a Parameter string key from an HTTP textual representation and the consumed offset in a tuple.
253
- *
254
- * @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.3.3
255
- *
256
- * @return array{0:string, 1:int}
257
- */
258
- private static function parseKey (string $ httpValue ): array
259
- {
260
- if (1 !== preg_match ('/^(?<key>[a-z*][a-z0-9.*_-]*)/ ' , $ httpValue , $ matches )) {
261
- throw new SyntaxError ("Invalid character in the HTTP textual representation of a key ` $ httpValue`. " );
262
- }
263
-
264
- return [$ matches ['key ' ], strlen ($ matches ['key ' ])];
265
- }
266
-
267
248
/**
268
249
* Returns a boolean from an HTTP textual representation and the consumed offset in a tuple.
269
250
*
@@ -319,7 +300,7 @@ private static function parseString(string $httpValue): array
319
300
return [$ output , $ offset ];
320
301
}
321
302
322
- if (ord ( $ char ) <= 0x1f || ord ( $ char ) >= 0x7f ) {
303
+ if (1 === preg_match ( " /[^ \x20 - \x7E ]/ " , $ char )) {
323
304
throw new SyntaxError ("Invalid character in the HTTP textual representation of a string ` $ httpValue`. " );
324
305
}
325
306
0 commit comments