@@ -60,6 +60,19 @@ echo $list->get(-1)->value; //returns '/member/*/comments'
60
60
Parsing and Serializing Structured Fields
61
61
------------
62
62
63
+ ``` php
64
+ use Bakame\Http\StructuredFields;
65
+
66
+ $dictionary = StructuredFields\Dictionary::fromHttpValue("a=?0, b, c=?1; foo=bar");
67
+ echo $dictionary->toHttpValue(); // 'a=?0, b, c;foo=bar'
68
+
69
+ $list = StructuredFields\OrderedList::fromHttpValue('("foo"; a=1;b=2);lvl=5, ("bar" "baz");lvl=1');
70
+ echo $list->toHttpValue(); // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
71
+
72
+ $item = StructuredFields\Item::fromHttpValue('"foo";a=1;b=2');
73
+ echo $item->toHttpValue(); // "foo";a=1;b=2
74
+ ```
75
+
63
76
an HTTP field value can be:
64
77
65
78
- a Dictionary,
@@ -72,19 +85,6 @@ representation of the field and to serialize the value object back to the textua
72
85
- Parsing is done via a common named constructor ` fromHttpValue ` which expects the Header or Trailer string value.
73
86
- Serializing is done via a common ` toHttpValue ` public method. The method returns the normalized string representation suited for HTTP textual representation.
74
87
75
- ``` php
76
- use Bakame\Http\StructuredFields;
77
-
78
- $dictionary = StructuredFields\Dictionary::fromHttpValue("a=?0, b, c=?1; foo=bar");
79
- echo $dictionary->toHttpValue(); // "a=?0, b, c;foo=bar"
80
-
81
- $list = StructuredFields\OrderedList::fromHttpValue('("foo"; a=1;b=2);lvl=5, ("bar" "baz");lvl=1');
82
- echo $list->toHttpValue(); // "("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1"
83
-
84
- $item = StructuredFields\Item::fromHttpValue('"foo";a=1;b=2"');
85
- echo $item->toHttpValue(); // "foo";a=1;b=2
86
- ```
87
-
88
88
Building Structured Fields
89
89
------------
90
90
@@ -115,53 +115,67 @@ keys are strings and the value are bare items. Their public API is covered in su
115
115
116
116
** An item without any parameter associated to it is said to be a bare item.**
117
117
118
- #### Token Data type
118
+ #### Token type
119
+
120
+ ``` php
121
+ use Bakame\Http\StructuredFields;
122
+
123
+ $token = StructuredFields\Token::fromString('bar')]));
124
+
125
+ echo $token->toString(); //displays 'bar'
126
+ echo $dictionary->toHttpValue(); //displays 'bar'
127
+ ```
119
128
120
129
The Token data type is a special string as defined in the RFC. To distinguish it from a normal string, the ` Bakame\Http\StructuredFields\Token ` class is used.
121
130
122
131
To instantiate the class you are required to use the ` Token::fromString ` named constructor.
123
- The class also exposes the complementat=ry public methods ` Token::toString ` as well as the ` Token::toHttpValue ` to enable its textual representation.
132
+ The class also exposes the complementary public methods ` Token::toString ` as well as the ` Token::toHttpValue ` to enable its textual representation.
133
+
134
+ #### Byte Sequence type
124
135
136
+ ``` php
137
+ use Bakame\Http\StructuredFields;
125
138
126
- #### Byte Sequence Data type
139
+ $sequenceFromDecoded = StructuredFields\ByteSequence::fromDecoded("Hello World");
140
+ $sequenceFromEncoded = StructuredFields\ByteSequence::fromEncoded("SGVsbG8gV29ybGQ=");
141
+
142
+ echo $sequenceFromEncoded->decoded(); //displays 'Hello World'
143
+ echo $sequenceFromDecoded->encoded(); //displays 'SGVsbG8gV29ybGQ='
144
+ echo $sequenceFromDecoded->toHttpValue(); //displays ':SGVsbG8gV29ybGQ=:'
145
+ echo $sequenceFromEncoded->toHttpValue(); //displays ':SGVsbG8gV29ybGQ=:'
146
+ ```
127
147
128
148
The Byte Sequence data type is a special string as defined in the RFC to represent base64 encoded data. To distinguish it from a normal string,
129
149
the ` Bakame\Http\StructuredFields\ByteSequence ` class is used.
130
150
131
151
To instantiate the class you are required to use the ` ByteSequence::fromDecoded ` or ` ByteSequence::fromEncoded ` named constructors.
132
- The class also exposes the complementat=ry public methods ` ByteSequence::decoded ` , ` ByteSequence::encoded ` as well as
152
+ The class also exposes the complementary public methods ` ByteSequence::decoded ` , ` ByteSequence::encoded ` as well as
133
153
the ` ByteSequence::toHttpValue ` to enable its textual representation.
134
154
135
155
#### Usages
136
156
137
- Instantiation via type recognition is done using the ` Item::from ` named constructor.
138
-
139
- - The first argument represents one of the six (6) item type value;
140
- - The second argument, which is optional, MUST be an iterable construct where its index represents the parameter key and its value an item or a item type value;
141
-
142
157
``` php
143
158
use Bakame\Http\StructuredFields;
144
159
145
160
$item = StructuredFields\Item::from("hello world", ["a" => true]);
146
- $item->value; //returns "hello world"
147
- $item->isString(); //return true
148
- $item->isToken(); //return false
161
+ $item->value; //returns "hello world"
162
+ $item->isString(); //returns true
163
+ $item->isToken(); //returns false
149
164
$item->parameters->value("a"); //returns true
150
165
```
151
166
152
- Conversely, the ` Item::fromPair ` is an alternative to the ` Item::from `
153
- which expects a tuple composed by an array as a list where:
167
+ Instantiation via type recognition is done using the ` Item::from ` named constructor.
154
168
155
- - The first member on index ` 0 ` represents one of the six (6) item type value;
156
- - The second optional member, on index ` 1 ` , MUST be an iterable construct containing tuples of key- value pairs ;
169
+ - The first argument represents one of the six (6) item type value;
170
+ - The second argument, which is optional , MUST be an iterable construct where its index represents the parameter key and its value an item or a item type value ;
157
171
158
172
``` php
159
173
use Bakame\Http\StructuredFields;
160
174
161
175
$item = StructuredFields\Item::fromPair([
162
176
"hello world",
163
177
[
164
- ["a", StructuredFields\ByteSequence::fromDecoded("Hello World")]
178
+ ["a", StructuredFields\ByteSequence::fromDecoded("Hello World")],
165
179
]
166
180
]);
167
181
$item->value; //returns "hello world"
@@ -171,6 +185,12 @@ $item->parameters->value("a")->decoded(); //returns 'Hello World'
171
185
echo $item->toHttpValue(); //returns "hello world";a=:SGVsbG8gV29ybGQ=:
172
186
```
173
187
188
+ Conversely, the ` Item::fromPair ` is an alternative to the ` Item::from `
189
+ which expects a tuple composed by an array as a list where:
190
+
191
+ - The first member on index ` 0 ` represents one of the six (6) item type value;
192
+ - The second optional member, on index ` 1 ` , MUST be an iterable construct containing tuples of key-value pairs;
193
+
174
194
Once instantiated, accessing ` Item ` properties is done via two (2) readonly properties:
175
195
176
196
- ` Item::value ` which returns the instance underlying value
@@ -192,7 +212,17 @@ $item->isInteger(); //return true
192
212
193
213
### Containers
194
214
195
- The package exposes different ordered maps and lists with different requirements via the following value objects:
215
+ ``` php
216
+ use Bakame\Http\StructuredFields;
217
+
218
+ $parameters = StructuredFields\Parameters::fromAssociative(['a' => 1, 'b' => 2, 'c' => "hello world"]);
219
+ count($parameters); // returns 3
220
+ $parameters->isEmpty(); // returns false
221
+ $parameters->toHttpValue(); // returns ';a=1;b=2;c="hello world"'
222
+ $parameters->clear()->isEmpty(); // returns true
223
+ ```
224
+
225
+ The package exposes ordered maps and lists with different requirements via the following value objects:
196
226
197
227
- ` Dictionary ` ,
198
228
- ` Parameters ` ,
@@ -212,17 +242,29 @@ At any given time it is possible with each of these objects to:
212
242
- For setter methods, Item types are inferred using ` Item::from ` if a ` Item ` object is not submitted.
213
243
- Because all containers can be access by their indexes, some changes may re-index them as to not expose missing indexes.
214
244
245
+ #### Ordered Maps
246
+
215
247
``` php
216
248
use Bakame\Http\StructuredFields;
217
249
218
- $parameters = StructuredFields\Parameters::fromAssociative(['a' => 1, 'b' => 2, 'c' => "hello world"]);
219
- count($parameters); // returns 3
220
- $parameters->isEmpty(); // returns false
221
- $parameters->toHttpValue(); // returns ';a=1;b=2;c="hello world"'
222
- $parameters->clear()->isEmpty(); // returns true
223
- ```
250
+ $dictionary = StructuredFields\Dictionary::fromPairs([
251
+ ['b', true],
252
+ ]);
253
+ $dictionary
254
+ ->append('c', StructuredFields\Item::from(true, ['foo' => StructuredFields\Token::fromString('bar')]))
255
+ ->prepend('a', false)
256
+ ->toHttpValue(); //returns "a=?0, b, c;foo=bar"
224
257
225
- #### Ordered Maps
258
+ $dictionary->has('a'); //return true
259
+ $dictionary->has('foo'); //return false
260
+ $dictionary->pair(1); //return ['b', Item::fromBoolean(true)]
261
+ $dictionary->hasPair(-1); //return true
262
+
263
+ echo $dictionary
264
+ ->append('z', 42.0)
265
+ ->delete('b', 'c')
266
+ ->toHttpValue(); //returns "a=?0, z=42.0"
267
+ ```
226
268
227
269
The ` Parameters ` and the ` Dictionary ` classes allow associating a string
228
270
key to its members
@@ -232,10 +274,13 @@ key to its members
232
274
233
275
Both classes exposes the following:
234
276
277
+ named constructors:
278
+
279
+ - ` fromAssociative ` instantiates the container with an iterable construct of associative value;
280
+ - ` fromPairs ` instantiates the container with a list of key-value pairs;
281
+
235
282
getter methods:
236
283
237
- - ` fromAssociative ` a named constructor to instantiate the container with an iterable construct of associative value;
238
- - ` fromPairs ` a named constructor to instantiate the container with a list of key-value pairs;
239
284
- ` toPairs ` returns an iterator to iterate over the container pairs;
240
285
- ` keys ` to list all existing keys of the ordered maps as an array list;
241
286
- ` has ` tell whether a specific element is associated to a given ` key ` ;
@@ -252,26 +297,6 @@ setter methods:
252
297
- ` mergeAssociative ` merge multiple instances of iterable structure as associative constructs;
253
298
- ` mergePairs ` merge multiple instances of iterable structure as pairs constructs;
254
299
255
- ``` php
256
- use Bakame\Http\StructuredFields;
257
-
258
- $dictionary = StructuredFields\Dictionary::fromPairs([['b', true]]);
259
- $dictionary
260
- ->append('c', StructuredFields\Item::from(true, ['foo' => StructuredFields\Token::fromString('bar')]))
261
- ->prepend('a', false)
262
- ->toHttpValue(); //returns "a=?0, b, c;foo=bar"
263
-
264
- $dictionary->has('a'); //return true
265
- $dictionary->has('foo'); //return false
266
- $dictionary->pair(1); //return ['b', Item::fromBoolean(true)]
267
- $dictionary->hasPair(-1); //return true
268
-
269
- echo $dictionary
270
- ->append('z', 42.0)
271
- ->delete('b', 'c')
272
- ->toHttpValue(); //returns "a=?0, z=42.0"
273
- ```
274
-
275
300
The ` Parameters ` instance exposes the following additional methods:
276
301
277
302
- ` Parameters::values() ` to list all existing Bare Items value as an array list;
@@ -298,6 +323,23 @@ $parameters->value('unknown'); // returns null
298
323
299
324
#### Lists
300
325
326
+ ``` php
327
+ use Bakame\Http\StructuredFields;
328
+
329
+ $innerList = StructuredFields\InnerList::fromList([42, 42.0, "42"], ["a" => true]);
330
+ $innerList->has(2); //return true
331
+ $innerList->has(42); //return false
332
+ $innerList->push(StructuredFields\Token::fromString('forty-two'));
333
+ $innerList->remove(0, 2);
334
+ echo $innerList->toHttpValue(); //returns '(42.0 forty-two);a'
335
+
336
+ $orderedList = StructuredFields\OrderedList::from(
337
+ StructuredFields\Item::from("42", ["foo" => "bar"]),
338
+ $innerList
339
+ );
340
+ echo $orderedList->toHttpValue(); //returns '"42";foo="bar", (42.0 forty-two);a'
341
+ ```
342
+
301
343
The ` OrderedList ` and the ` InnerList ` classes are list of members that act as containers
302
344
303
345
The main distinction between ` OrderedList ` and ` InnerList ` are:
@@ -308,10 +350,13 @@ The main distinction between `OrderedList` and `InnerList` are:
308
350
309
351
Both classes exposes the following:
310
352
353
+ named constructors:
354
+
355
+ - ` fromList ` instantiates the container with a list of members in an iterable construct;
356
+ - ` from ` instantiates the container with a list of members as variadic;
357
+
311
358
getter methods:
312
359
313
- - ` fromList ` a named constructor to instantiate the container with a list of members in an iterable construct;
314
- - ` from ` a named constructor to instantiate the container with a list of members as variadic;
315
360
- ` get ` to access an element at a given index (negative indexes are supported)
316
361
- ` has ` tell whether an element is attached to the container using its ` index ` ;
317
362
@@ -323,23 +368,6 @@ setter methods
323
368
- ` replace ` to replace an element at a given position in the list;
324
369
- ` remove ` to remove elements based on their position;
325
370
326
- ``` php
327
- use Bakame\Http\StructuredFields;
328
-
329
- $innerList = StructuredFields\InnerList::fromList([42, 42.0, "42"], ["a" => true]);
330
- $innerList->has(2); //return true
331
- $innerList->has(42); //return false
332
- $innerList->push(StructuredFields\Token::fromString('forty-two'));
333
- $innerList->remove(0, 2);
334
- echo $innerList->toHttpValue(); //returns '(42.0 forty-two);a'
335
-
336
- $orderedList = StructuredFields\OrderedList::from(
337
- StructuredFields\Item::from("42", ["foo" => "bar"]),
338
- $innerList
339
- );
340
- echo $orderedList->toHttpValue(); //returns '"42";foo="bar", (42.0 forty-two);a'
341
- ```
342
-
343
371
A ` Parameters ` instance can be associated to an ` InnerList ` using the same API as for the ` Item ` value object.
344
372
345
373
``` php
0 commit comments