Skip to content

Commit 9f8c52a

Browse files
committed
Improve documentation
1 parent 3959179 commit 9f8c52a

File tree

1 file changed

+107
-79
lines changed

1 file changed

+107
-79
lines changed

README.md

Lines changed: 107 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ echo $list->get(-1)->value; //returns '/member/*/comments'
6060
Parsing and Serializing Structured Fields
6161
------------
6262

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+
6376
an HTTP field value can be:
6477

6578
- a Dictionary,
@@ -72,19 +85,6 @@ representation of the field and to serialize the value object back to the textua
7285
- Parsing is done via a common named constructor `fromHttpValue` which expects the Header or Trailer string value.
7386
- Serializing is done via a common `toHttpValue` public method. The method returns the normalized string representation suited for HTTP textual representation.
7487

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-
8888
Building Structured Fields
8989
------------
9090

@@ -115,53 +115,67 @@ keys are strings and the value are bare items. Their public API is covered in su
115115

116116
**An item without any parameter associated to it is said to be a bare item.**
117117

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+
```
119128

120129
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.
121130

122131
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
124135

136+
```php
137+
use Bakame\Http\StructuredFields;
125138

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+
```
127147

128148
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,
129149
the `Bakame\Http\StructuredFields\ByteSequence` class is used.
130150

131151
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
133153
the `ByteSequence::toHttpValue` to enable its textual representation.
134154

135155
#### Usages
136156

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-
142157
```php
143158
use Bakame\Http\StructuredFields;
144159

145160
$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
149164
$item->parameters->value("a"); //returns true
150165
```
151166

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.
154168

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;
157171

158172
```php
159173
use Bakame\Http\StructuredFields;
160174

161175
$item = StructuredFields\Item::fromPair([
162176
"hello world",
163177
[
164-
["a", StructuredFields\ByteSequence::fromDecoded("Hello World")]
178+
["a", StructuredFields\ByteSequence::fromDecoded("Hello World")],
165179
]
166180
]);
167181
$item->value; //returns "hello world"
@@ -171,6 +185,12 @@ $item->parameters->value("a")->decoded(); //returns 'Hello World'
171185
echo $item->toHttpValue(); //returns "hello world";a=:SGVsbG8gV29ybGQ=:
172186
```
173187

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+
174194
Once instantiated, accessing `Item` properties is done via two (2) readonly properties:
175195

176196
- `Item::value` which returns the instance underlying value
@@ -192,7 +212,17 @@ $item->isInteger(); //return true
192212

193213
### Containers
194214

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:
196226

197227
- `Dictionary`,
198228
- `Parameters`,
@@ -212,17 +242,29 @@ At any given time it is possible with each of these objects to:
212242
- For setter methods, Item types are inferred using `Item::from` if a `Item` object is not submitted.
213243
- Because all containers can be access by their indexes, some changes may re-index them as to not expose missing indexes.
214244

245+
#### Ordered Maps
246+
215247
```php
216248
use Bakame\Http\StructuredFields;
217249

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"
224257

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+
```
226268

227269
The `Parameters` and the `Dictionary` classes allow associating a string
228270
key to its members
@@ -232,10 +274,13 @@ key to its members
232274

233275
Both classes exposes the following:
234276

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+
235282
getter methods:
236283

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;
239284
- `toPairs` returns an iterator to iterate over the container pairs;
240285
- `keys` to list all existing keys of the ordered maps as an array list;
241286
- `has` tell whether a specific element is associated to a given `key`;
@@ -252,26 +297,6 @@ setter methods:
252297
- `mergeAssociative` merge multiple instances of iterable structure as associative constructs;
253298
- `mergePairs` merge multiple instances of iterable structure as pairs constructs;
254299

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-
275300
The `Parameters` instance exposes the following additional methods:
276301

277302
- `Parameters::values()` to list all existing Bare Items value as an array list;
@@ -298,6 +323,23 @@ $parameters->value('unknown'); // returns null
298323

299324
#### Lists
300325

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+
301343
The `OrderedList` and the `InnerList` classes are list of members that act as containers
302344

303345
The main distinction between `OrderedList` and `InnerList` are:
@@ -308,10 +350,13 @@ The main distinction between `OrderedList` and `InnerList` are:
308350

309351
Both classes exposes the following:
310352

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+
311358
getter methods:
312359

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;
315360
- `get` to access an element at a given index (negative indexes are supported)
316361
- `has` tell whether an element is attached to the container using its `index`;
317362

@@ -323,23 +368,6 @@ setter methods
323368
- `replace` to replace an element at a given position in the list;
324369
- `remove` to remove elements based on their position;
325370

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-
343371
A `Parameters` instance can be associated to an `InnerList` using the same API as for the `Item` value object.
344372

345373
```php

0 commit comments

Comments
 (0)