4
4
5
5
namespace Bakame \Http \StructuredFields ;
6
6
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
+ */
7
14
final class Parser
8
15
{
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
+ */
9
21
public static function parseList (string $ httpValue ): OrderedList
10
22
{
11
23
$ elements = [];
@@ -32,6 +44,11 @@ public static function parseList(string $httpValue): OrderedList
32
44
return OrderedList::fromElements ($ elements );
33
45
}
34
46
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
+ */
35
52
public static function parseDictionary (string $ httpValue ): Dictionary
36
53
{
37
54
$ elements = [];
@@ -64,6 +81,11 @@ public static function parseDictionary(string $httpValue): Dictionary
64
81
return Dictionary::fromAssociative ($ elements );
65
82
}
66
83
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
+ */
67
89
private static function parseItemOrInnerList (string &$ httpValue ): InnerList |Item
68
90
{
69
91
if ($ httpValue [0 ] === '( ' ) {
@@ -73,6 +95,11 @@ private static function parseItemOrInnerList(string &$httpValue): InnerList|Item
73
95
return Item::from (self ::parseBareItem ($ httpValue ), self ::parseParameters ($ httpValue ));
74
96
}
75
97
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
+ */
76
103
private static function parseInnerList (string &$ httpValue ): InnerList
77
104
{
78
105
$ elements = [];
@@ -96,6 +123,11 @@ private static function parseInnerList(string &$httpValue): InnerList
96
123
throw new SyntaxError ("Unexpected end of line for The HTTP textual representation ` $ httpValue` for a inner list. " );
97
124
}
98
125
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
+ */
99
131
private static function parseBareItem (string &$ httpValue ): bool |float |int |string |ByteSequence |Token
100
132
{
101
133
return match (true ) {
@@ -109,6 +141,11 @@ private static function parseBareItem(string &$httpValue): bool|float|int|string
109
141
};
110
142
}
111
143
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
+ */
112
149
private static function parseParameters (string &$ httpValue ): Parameters
113
150
{
114
151
$ parameters = [];
@@ -128,6 +165,11 @@ private static function parseParameters(string &$httpValue): Parameters
128
165
return Parameters::fromAssociative ($ parameters );
129
166
}
130
167
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
+ */
131
173
private static function parseKey (string &$ httpValue ): string
132
174
{
133
175
if (1 !== preg_match ('/^[a-z*][a-z0-9.*_-]*/ ' , $ httpValue , $ matches )) {
@@ -139,6 +181,11 @@ private static function parseKey(string &$httpValue): string
139
181
return $ matches [0 ];
140
182
}
141
183
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
+ */
142
189
private static function parseBoolean (string &$ httpValue ): bool
143
190
{
144
191
if (1 !== preg_match ('/^\?[01]/ ' , $ httpValue )) {
@@ -152,6 +199,11 @@ private static function parseBoolean(string &$httpValue): bool
152
199
return $ value ;
153
200
}
154
201
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
+ */
155
207
private static function parseNumber (string &$ httpValue ): int |float
156
208
{
157
209
if (1 !== preg_match ('/^(-?\d+(?:\.\d+)?)(?:[^\d.]|$)/ ' , $ httpValue , $ number_matches )) {
@@ -168,6 +220,11 @@ private static function parseNumber(string &$httpValue): int|float
168
220
};
169
221
}
170
222
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
+ */
171
228
private static function parseString (string &$ httpValue ): string
172
229
{
173
230
// parseString is only called if first character is a double quote, so
@@ -209,6 +266,11 @@ private static function parseString(string &$httpValue): string
209
266
throw new SyntaxError ('Invalid end of string ' );
210
267
}
211
268
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
+ */
212
274
private static function parseToken (string &$ httpValue ): Token
213
275
{
214
276
preg_match ('/^([a-z*][a-z0-9:\/ ' .preg_quote ("!#$%&'*+-.^_`|~ " ).']*)/i ' , $ httpValue , $ matches );
@@ -218,6 +280,11 @@ private static function parseToken(string &$httpValue): Token
218
280
return new Token ($ matches [1 ]);
219
281
}
220
282
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
+ */
221
288
private static function parseByteSequence (string &$ httpValue ): ByteSequence
222
289
{
223
290
if (1 !== preg_match ('/^:([a-z0-9+\/=]*):/i ' , $ httpValue , $ matches )) {
0 commit comments