Skip to content

Commit 842c851

Browse files
committed
Update internal parser codebase
1 parent ddf83ff commit 842c851

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1010

1111
### Fixed
1212

13-
- `InnerList::fromHttpValue` accepts Optional White Spaces like all the other named constructors at the start of its textual representation.
13+
- `InnerList::fromHttpValue` accepts Optional White Spaces at the start of its textual representation.
14+
- `Parameters::fromHttpValue` accepts Optional White Spaces at the start of its textual representation.
1415

1516
### Deprecated
1617

src/Parser.php

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
final class Parser
2626
{
2727
/**
28-
* Returns an OrderedList value object from an HTTP textual representation.
28+
* Returns an ordered list represented as a PHP list array from an HTTP textual representation.
2929
*
3030
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1
3131
*
@@ -36,18 +36,18 @@ final class Parser
3636
*/
3737
public static function parseList(string $httpValue): array
3838
{
39-
$members = [];
39+
$list = [];
4040
$remainder = ltrim($httpValue, ' ');
4141
while ('' !== $remainder) {
42-
[$members[], $offset] = self::parseItemOrInnerList($remainder);
42+
[$list[], $offset] = self::parseItemOrInnerList($remainder);
4343
$remainder = self::removeCommaSeparatedWhiteSpaces($remainder, $offset);
4444
}
4545

46-
return $members;
46+
return $list;
4747
}
4848

4949
/**
50-
* Returns a Dictionary value object from an HTTP textual representation.
50+
* Returns an ordered map represented as a PHP associative array from an HTTP textual representation.
5151
*
5252
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.2
5353
*
@@ -58,7 +58,7 @@ public static function parseList(string $httpValue): array
5858
*/
5959
public static function parseDictionary(string $httpValue): array
6060
{
61-
$members = [];
61+
$map = [];
6262
$remainder = ltrim($httpValue, ' ');
6363
while ('' !== $remainder) {
6464
[$key, $offset] = self::parseKey($remainder);
@@ -67,15 +67,15 @@ public static function parseDictionary(string $httpValue): array
6767
$remainder = '=?1'.$remainder;
6868
}
6969

70-
[$members[$key], $offset] = self::parseItemOrInnerList(substr($remainder, 1));
70+
[$map[$key], $offset] = self::parseItemOrInnerList(substr($remainder, 1));
7171
$remainder = self::removeCommaSeparatedWhiteSpaces($remainder, ++$offset);
7272
}
7373

74-
return $members;
74+
return $map;
7575
}
7676

7777
/**
78-
* Returns a InnerList value object from an HTTP textual representation.
78+
* Returns an inner list represented as a PHP list array from an HTTP textual representation.
7979
*
8080
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1.2
8181
*
@@ -91,52 +91,52 @@ public static function parseInnerList(string $httpValue): array
9191
throw new SyntaxError("The HTTP textual representation `$httpValue` for a inner list is missing a parenthesis.");
9292
}
9393

94-
[$members, $offset] = self::parseInnerListValue($remainder);
94+
[$list, $offset] = self::parseInnerListValue($remainder);
9595
$remainder = self::removeOptionalWhiteSpaces(substr($remainder, $offset));
9696

9797
if ('' !== $remainder) {
9898
throw new SyntaxError("The HTTP textual representation `$httpValue` for a inner list contains invalid data.");
9999
}
100100

101-
return $members;
101+
return $list;
102102
}
103103

104104
/**
105105
* Filter optional white spaces before and after comma.
106106
*
107107
* @see https://tools.ietf.org/html/rfc7230#section-3.2.3
108108
*/
109-
private static function removeCommaSeparatedWhiteSpaces(string $remainder, int $offset): string
109+
private static function removeCommaSeparatedWhiteSpaces(string $httpValue, int $offset): string
110110
{
111-
$remainder = self::removeOptionalWhiteSpaces(substr($remainder, $offset));
112-
if ('' === $remainder) {
113-
return $remainder;
111+
$httpValue = self::removeOptionalWhiteSpaces(substr($httpValue, $offset));
112+
if ('' === $httpValue) {
113+
return $httpValue;
114114
}
115115

116-
if (1 !== preg_match('/^(?<space>,[ \t]*)/', $remainder, $found)) {
116+
if (1 !== preg_match('/^(?<space>,[ \t]*)/', $httpValue, $found)) {
117117
throw new SyntaxError('The HTTP textual representation is missing an excepted comma.');
118118
}
119119

120-
$remainder = substr($remainder, strlen($found['space']));
121-
if ('' === $remainder) {
120+
$httpValue = substr($httpValue, strlen($found['space']));
121+
if ('' === $httpValue) {
122122
throw new SyntaxError('Unexpected end of line for The HTTP textual representation.');
123123
}
124124

125-
return $remainder;
125+
return $httpValue;
126126
}
127127

128128
/**
129129
* Remove optional white spaces before field value.
130130
*
131131
* @see https://tools.ietf.org/html/rfc7230#section-3.2.3
132132
*/
133-
private static function removeOptionalWhiteSpaces(string $value): string
133+
private static function removeOptionalWhiteSpaces(string $httpValue): string
134134
{
135-
return ltrim($value, " \t");
135+
return ltrim($httpValue, " \t");
136136
}
137137

138138
/**
139-
* Returns an Item or an InnerList value object from an HTTP textual representation.
139+
* Returns an Item value object or an inner list as a PHP list array from an HTTP textual representation.
140140
*
141141
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1.1
142142
*
@@ -161,7 +161,7 @@ private static function parseItemOrInnerList(string $httpValue): array
161161
}
162162

163163
/**
164-
* Returns an InnerList value object from an HTTP textual representation and the consumed offset.
164+
* Returns an inner list represented as a PHP list array from an HTTP textual representation and the consumed offset in a tuple.
165165
*
166166
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1.2
167167
*
@@ -172,7 +172,7 @@ private static function parseItemOrInnerList(string $httpValue): array
172172
*/
173173
private static function parseInnerListValue(string $httpValue): array
174174
{
175-
$members = [];
175+
$list = [];
176176
$remainder = substr($httpValue, 1);
177177
while ('' !== $remainder) {
178178
$remainder = ltrim($remainder, ' ');
@@ -182,7 +182,7 @@ private static function parseInnerListValue(string $httpValue): array
182182
[$parameters, $offset] = self::parseParameters($remainder);
183183
$remainder = substr($remainder, $offset);
184184

185-
return [[$members, $parameters], strlen($httpValue) - strlen($remainder)];
185+
return [[$list, $parameters], strlen($httpValue) - strlen($remainder)];
186186
}
187187

188188
[$value, $offset] = self::parseBareItem($remainder);
@@ -191,7 +191,7 @@ private static function parseInnerListValue(string $httpValue): array
191191
[$parameters, $offset] = self::parseParameters($remainder);
192192
$remainder = substr($remainder, $offset);
193193

194-
$members[] = Item::from($value, $parameters);
194+
$list[] = Item::from($value, $parameters);
195195

196196
if ('' !== $remainder && !in_array($remainder[0], [' ', ')'], true)) {
197197
throw new SyntaxError("The HTTP textual representation `$remainder` for a inner list is using invalid characters.");
@@ -202,7 +202,7 @@ private static function parseInnerListValue(string $httpValue): array
202202
}
203203

204204
/**
205-
* Returns a Item or an InnerList value object from an HTTP textual representation.
205+
* Returns an Item value from an HTTP textual representation and the consumed offset in a tuple.
206206
*
207207
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.3.1
208208
*
@@ -222,36 +222,36 @@ private static function parseBareItem(string $httpValue): array
222222
}
223223

224224
/**
225-
* Returns a Parameters value object from an HTTP textual representation.
225+
* Returns an parameters container represented as a PHP associative array from an HTTP textual representation and the consumed offset in a tuple.
226226
*
227227
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.3.2
228228
*
229-
* @return array{0:array<array-key, Token|ByteSequence|float|int|bool|string>, 1:int}
229+
* @return array{0:array<string, Token|ByteSequence|float|int|bool|string>, 1:int}
230230
*/
231231
private static function parseParameters(string $httpValue): array
232232
{
233-
$parameters = [];
233+
$map = [];
234234
$remainder = $httpValue;
235235
while ('' !== $remainder && ';' === $remainder[0]) {
236236
$remainder = ltrim(substr($remainder, 1), ' ');
237237

238238
[$key, $keyOffset] = self::parseKey($remainder);
239-
$parameters[$key] = true;
239+
$map[$key] = true;
240240

241241
$remainder = substr($remainder, $keyOffset);
242242
if ('' !== $remainder && '=' === $remainder[0]) {
243243
$remainder = substr($remainder, 1);
244244

245-
[$parameters[$key], $offset] = self::parseBareItem($remainder);
245+
[$map[$key], $offset] = self::parseBareItem($remainder);
246246
$remainder = substr($remainder, $offset);
247247
}
248248
}
249249

250-
return [$parameters, strlen($httpValue) - strlen($remainder)];
250+
return [$map, strlen($httpValue) - strlen($remainder)];
251251
}
252252

253253
/**
254-
* Returns a Dictionary or a Parameter string key from an HTTP textual representation.
254+
* Returns a Dictionary or a Parameter string key from an HTTP textual representation and the consumed offset in a tuple.
255255
*
256256
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.3.3
257257
*
@@ -267,7 +267,7 @@ private static function parseKey(string $httpValue): array
267267
}
268268

269269
/**
270-
* Returns a boolean from an HTTP textual representation.
270+
* Returns a boolean from an HTTP textual representation and the consumed offset in a tuple.
271271
*
272272
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.8
273273
*
@@ -283,7 +283,7 @@ private static function parseBoolean(string $httpValue): array
283283
}
284284

285285
/**
286-
* Returns a int or a float from an HTTP textual representation.
286+
* Returns a int or a float from an HTTP textual representation and the consumed offset in a tuple.
287287
*
288288
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.4
289289
*
@@ -301,7 +301,7 @@ private static function parseNumber(string $httpValue): array
301301
}
302302

303303
/**
304-
* Returns a string from an HTTP textual representation.
304+
* Returns a string from an HTTP textual representation and the consumed offset in a tuple.
305305
*
306306
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.5
307307
*
@@ -345,7 +345,7 @@ private static function parseString(string $httpValue): array
345345
}
346346

347347
/**
348-
* Returns a Token from an HTTP textual representation.
348+
* Returns a Token from an HTTP textual representation and the consumed offset in a tuple.
349349
*
350350
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.6
351351
*
@@ -359,7 +359,7 @@ private static function parseToken(string $httpValue): array
359359
}
360360

361361
/**
362-
* Returns a Byte Sequence from an HTTP textual representation.
362+
* Returns a Byte Sequence from an HTTP textual representation and the consumed offset in a tuple.
363363
*
364364
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.7
365365
*

0 commit comments

Comments
 (0)