@@ -162,25 +162,25 @@ public static function fromHttpValue(Stringable|string $httpValue): self
162
162
{
163
163
$ itemString = trim ((string ) $ httpValue , ' ' );
164
164
165
- [$ value , $ parameters ] = match (true ) {
165
+ [$ value , $ offset ] = match (true ) {
166
166
1 === preg_match ("/[ \r\t\n]|[^ \x20- \x7E]/ " , $ itemString ),
167
167
'' === $ itemString => throw new SyntaxError ('The HTTP textual representation " ' .$ httpValue .'" for an item contains invalid characters. ' ),
168
168
'" ' === $ itemString [0 ] => self ::parseString ($ itemString ),
169
- ': ' === $ itemString [0 ] => self ::parseBytesSequence ($ itemString ),
170
- '? ' === $ itemString [0 ] => self ::parseBoolean ($ itemString ),
169
+ ': ' === $ itemString [0 ] => self ::parseByteSequence ($ itemString ),
170
+ '? ' === $ itemString [0 ] => Parser ::parseBoolean ($ itemString ),
171
171
'@ ' === $ itemString [0 ] => self ::parseDate ($ itemString ),
172
172
1 === preg_match ('/^(-?\d)/ ' , $ itemString ) => self ::parseNumber ($ itemString ),
173
173
1 === preg_match ('/^([a-z*])/i ' , $ itemString ) => self ::parseToken ($ itemString ),
174
174
default => throw new SyntaxError ('The HTTP textual representation " ' .$ httpValue .'" for an item is unknown or unsupported. ' ),
175
175
};
176
176
177
- return new self ($ value , Parameters::fromHttpValue ($ parameters ));
177
+ return new self ($ value , Parameters::fromHttpValue (substr ( $ itemString , $ offset ) ));
178
178
}
179
179
180
180
/**
181
181
* Parses an HTTP textual representation of an Item as a Token Data Type.
182
182
*
183
- * @return array{0:Token, 1:string }
183
+ * @return array{0:Token, 1:int }
184
184
*/
185
185
private static function parseToken (string $ string ): array
186
186
{
@@ -195,42 +195,28 @@ private static function parseToken(string $string): array
195
195
196
196
return [
197
197
Token::fromString ($ found ['token ' ]),
198
- substr ( $ string , strlen ($ found ['token ' ]) ),
198
+ strlen ($ found ['token ' ]),
199
199
];
200
200
}
201
201
202
- /**
203
- * Parses an HTTP textual representation of an Item as a Boolean Data Type.
204
- *
205
- * @return array{0:bool, 1:string}
206
- */
207
- private static function parseBoolean (string $ string ): array
208
- {
209
- if (1 !== preg_match ('/^\?[01]/ ' , $ string )) {
210
- throw new SyntaxError ("The HTTP textual representation \"$ string \" for a Boolean contains invalid characters. " );
211
- }
212
-
213
- return [$ string [1 ] === '1 ' , substr ($ string , 2 )];
214
- }
215
-
216
202
/**
217
203
* Parses an HTTP textual representation of an Item as a Byte Sequence Type.
218
204
*
219
- * @return array{0:ByteSequence, 1:string }
205
+ * @return array{0:ByteSequence, 1:int }
220
206
*/
221
- private static function parseBytesSequence (string $ string ): array
207
+ private static function parseByteSequence (string $ string ): array
222
208
{
223
209
if (1 !== preg_match ('/^:(?<bytes>[a-z\d+\/=]*):/i ' , $ string , $ matches )) {
224
210
throw new SyntaxError ("The HTTP textual representation \"$ string \" for a Byte sequence contains invalid characters. " );
225
211
}
226
212
227
- return [ByteSequence::fromEncoded ($ matches ['bytes ' ]), substr ( $ string , strlen ($ matches [0 ]) )];
213
+ return [ByteSequence::fromEncoded ($ matches ['bytes ' ]), strlen ($ matches [0 ])];
228
214
}
229
215
230
216
/**
231
217
* Parses an HTTP textual representation of an Item as a Data Type number.
232
218
*
233
- * @return array{0:int|float, 1:string }
219
+ * @return array{0:int|float, 1:int }
234
220
*/
235
221
private static function parseNumber (string $ string ): array
236
222
{
@@ -249,31 +235,35 @@ private static function parseNumber(string $string): array
249
235
default => throw new SyntaxError ("The HTTP textual representation \"$ string \" for a Number contain too many digits. " ),
250
236
};
251
237
252
- return [$ number , substr ( $ string , strlen ($ found ['number ' ]) )];
238
+ return [$ number , strlen ($ found ['number ' ])];
253
239
}
254
240
255
241
/**
256
242
* Parses an HTTP textual representation of an Item as a Data Type number.
257
243
*
258
- * @return array{0:DateTimeImmutable, 1:string}
244
+ * @throws SyntaxError
245
+ *
246
+ * @return array{0:DateTimeImmutable, 1:int}
259
247
*/
260
248
private static function parseDate (string $ string ): array
261
249
{
262
- [$ timestamp , $ parameters ] = self ::parseNumber (substr ($ string , 1 ));
250
+ [$ timestamp , $ offset ] = self ::parseNumber (substr ($ string , 1 ));
263
251
if (!is_int ($ timestamp )) {
264
- throw new SyntaxError ("The HTTP textual representation \"$ string \" for a date contains invalid characters. " );
252
+ throw new SyntaxError ("The HTTP textual representation \"$ string \" for a Date contains invalid characters. " );
265
253
}
266
254
267
255
return [
268
256
(new DateTimeImmutable ('NOW ' , new DateTimeZone ('UTC ' )))->setTimestamp ($ timestamp ),
269
- $ parameters ,
257
+ ++ $ offset ,
270
258
];
271
259
}
272
260
273
261
/**
274
262
* Parses an HTTP textual representation of an Item as a String Data Type.
275
263
*
276
- * @return array{0:string, 1:string}
264
+ * @throws SyntaxError
265
+ *
266
+ * @return array{0:string, 1:int}
277
267
*/
278
268
private static function parseString (string $ string ): array
279
269
{
@@ -286,7 +276,7 @@ private static function parseString(string $string): array
286
276
$ string = substr ($ string , 1 );
287
277
288
278
if ($ char === '" ' ) {
289
- return [$ returnValue , $ string ];
279
+ return [$ returnValue , strlen ( $ originalString ) - strlen ( $ string) ];
290
280
}
291
281
292
282
if ($ char !== '\\' ) {
0 commit comments