Skip to content

Commit 754fac9

Browse files
committed
Mark the parser as internal
1 parent 08ba905 commit 754fac9

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

src/Parser.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,20 @@
44

55
namespace Bakame\Http\StructuredFields;
66

7+
/**
8+
* A parser to create HTTP Structured Fields value objects from HTTP textual representation.
9+
*
10+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2
11+
*
12+
* @internal Use OrderedList::fromHttpValue() or Dictionary::fromHttpValue() instead
13+
*/
714
final class Parser
815
{
16+
/**
17+
* Returns an OrderedList value object from an HTTP textual representation.
18+
*
19+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1
20+
*/
921
public static function parseList(string $httpValue): OrderedList
1022
{
1123
$elements = [];
@@ -32,6 +44,11 @@ public static function parseList(string $httpValue): OrderedList
3244
return OrderedList::fromElements($elements);
3345
}
3446

47+
/**
48+
* Returns a Dictionary value object from an HTTP textual representation.
49+
*
50+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.2
51+
*/
3552
public static function parseDictionary(string $httpValue): Dictionary
3653
{
3754
$elements = [];
@@ -64,6 +81,11 @@ public static function parseDictionary(string $httpValue): Dictionary
6481
return Dictionary::fromAssociative($elements);
6582
}
6683

84+
/**
85+
* Returns an Item or an InnerList value object from an HTTP textual representation.
86+
*
87+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1.1
88+
*/
6789
private static function parseItemOrInnerList(string &$httpValue): InnerList|Item
6890
{
6991
if ($httpValue[0] === '(') {
@@ -73,6 +95,11 @@ private static function parseItemOrInnerList(string &$httpValue): InnerList|Item
7395
return Item::from(self::parseBareItem($httpValue), self::parseParameters($httpValue));
7496
}
7597

98+
/**
99+
* Returns an InnerList value object from an HTTP textual representation.
100+
*
101+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1.2
102+
*/
76103
private static function parseInnerList(string &$httpValue): InnerList
77104
{
78105
$elements = [];
@@ -96,6 +123,11 @@ private static function parseInnerList(string &$httpValue): InnerList
96123
throw new SyntaxError("Unexpected end of line for The HTTP textual representation `$httpValue` for a inner list.");
97124
}
98125

126+
/**
127+
* Returns a Item or an InnerList value object from an HTTP textual representation.
128+
*
129+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.3.1
130+
*/
99131
private static function parseBareItem(string &$httpValue): bool|float|int|string|ByteSequence|Token
100132
{
101133
return match (true) {
@@ -109,6 +141,11 @@ private static function parseBareItem(string &$httpValue): bool|float|int|string
109141
};
110142
}
111143

144+
/**
145+
* Returns a Parameters value object from an HTTP textual representation.
146+
*
147+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.3.2
148+
*/
112149
private static function parseParameters(string &$httpValue): Parameters
113150
{
114151
$parameters = [];
@@ -128,6 +165,11 @@ private static function parseParameters(string &$httpValue): Parameters
128165
return Parameters::fromAssociative($parameters);
129166
}
130167

168+
/**
169+
* Returns a Dictionary or a Parameter string key from an HTTP textual representation.
170+
*
171+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.3.3
172+
*/
131173
private static function parseKey(string &$httpValue): string
132174
{
133175
if (1 !== preg_match('/^[a-z*][a-z0-9.*_-]*/', $httpValue, $matches)) {
@@ -139,6 +181,11 @@ private static function parseKey(string &$httpValue): string
139181
return $matches[0];
140182
}
141183

184+
/**
185+
* Returns a boolean from an HTTP textual representation.
186+
*
187+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.8
188+
*/
142189
private static function parseBoolean(string &$httpValue): bool
143190
{
144191
if (1 !== preg_match('/^\?[01]/', $httpValue)) {
@@ -152,6 +199,11 @@ private static function parseBoolean(string &$httpValue): bool
152199
return $value;
153200
}
154201

202+
/**
203+
* Returns a int or a float from an HTTP textual representation.
204+
*
205+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.4
206+
*/
155207
private static function parseNumber(string &$httpValue): int|float
156208
{
157209
if (1 !== preg_match('/^(-?\d+(?:\.\d+)?)(?:[^\d.]|$)/', $httpValue, $number_matches)) {
@@ -168,6 +220,11 @@ private static function parseNumber(string &$httpValue): int|float
168220
};
169221
}
170222

223+
/**
224+
* Returns a string from an HTTP textual representation.
225+
*
226+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.5
227+
*/
171228
private static function parseString(string &$httpValue): string
172229
{
173230
// parseString is only called if first character is a double quote, so
@@ -209,6 +266,11 @@ private static function parseString(string &$httpValue): string
209266
throw new SyntaxError('Invalid end of string');
210267
}
211268

269+
/**
270+
* Returns a Token from an HTTP textual representation.
271+
*
272+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.6
273+
*/
212274
private static function parseToken(string &$httpValue): Token
213275
{
214276
preg_match('/^([a-z*][a-z0-9:\/'.preg_quote("!#$%&'*+-.^_`|~").']*)/i', $httpValue, $matches);
@@ -218,6 +280,11 @@ private static function parseToken(string &$httpValue): Token
218280
return new Token($matches[1]);
219281
}
220282

283+
/**
284+
* Returns a Byte Sequence from an HTTP textual representation.
285+
*
286+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.7
287+
*/
221288
private static function parseByteSequence(string &$httpValue): ByteSequence
222289
{
223290
if (1 !== preg_match('/^:([a-z0-9+\/=]*):/i', $httpValue, $matches)) {

src/StructuredFieldTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public function it_can_pass_http_wg_tests(TestUnit $test): void
2222
$this->expectException(SyntaxError::class);
2323
}
2424

25-
$item = TestHeaderType::from($test->headerType)->fromHttpValue(implode(',', $test->raw));
25+
$structuredField = TestHeaderType::from($test->headerType)->newStructuredField(implode(',', $test->raw));
2626

2727
if (!$test->mustFail) {
28-
self::assertSame(implode(',', $test->canonical), $item->toHttpValue());
28+
self::assertSame(implode(',', $test->canonical), $structuredField->toHttpValue());
2929
}
3030
}
3131

src/TestHeaderType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ enum TestHeaderType: string
1010
case List = 'list';
1111
case Item = 'item';
1212

13-
public function fromHttpValue(string $input): StructuredField
13+
public function newStructuredField(string $input): StructuredField
1414
{
1515
return match ($this) {
1616
self::Dictionary => Dictionary::fromHttpValue($input),

0 commit comments

Comments
 (0)