Skip to content

Commit 59ac094

Browse files
committed
Update documentation
1 parent ea25a65 commit 59ac094

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

README.md

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use Bakame\Http\StructuredFields\Item;
1919
$field = Item::from("/terms", ['rel' => 'copyright', 'anchor' => '#foo']);
2020
echo $field->toHttpValue(); // display "/terms";rel="copyright";anchor="#foo"
2121
echo $field->value(); // display "/terms"
22-
echo $field->parameters()['rel']->value(); // display "copyright"
22+
echo $field->parameter('rel'); // display "copyright"
2323
```
2424

2525
## System Requirements
@@ -86,10 +86,11 @@ namespace
8686

8787
they all implement the `StructuredField` interface and expose a `fromHttpValue` named constructor:
8888

89-
### Building and Updating Structured Fields
89+
### Building and Updating Structured Fields Values
9090

91-
Every value object can create or update the HTTP field value it represents.
92-
For instance Ordered Map containers can be build with an associative iterable as shown below
91+
Every value object can be used as a builder to create an HTTP field value.
92+
93+
For instance, `Dictionary` and `Parameters` can be build with an associative iterable as shown below
9394

9495
```php
9596
use Bakame\Http\StructuredFields\Dictionary;
@@ -104,7 +105,7 @@ echo $value->toHttpValue(); //"b=?0, a=bar;baz=42, c=@1671800423"
104105
echo $value; //"b=?0, a=bar;baz=42, c=@1671800423"
105106
```
106107

107-
Or with an iterable of pairs as described in the RFC:
108+
Or with an iterable structure of pairs as described in the RFC:
108109

109110
```php
110111
use Bakame\Http\StructuredFields\Parameters;
@@ -141,7 +142,7 @@ echo $value; //"b=?0, a=bar;baz=42, c=@1671800423"
141142
Because we are using immutable value objects any change to the value object will return a new instance with
142143
the changes applied and leave the original instance unchanged.
143144

144-
Ordered Map Containers exhibits the following modifying methods:
145+
`Dictionary` and `Parameters` exhibit the following modifying methods:
145146

146147
```php
147148
$map->add($key, $value): static;
@@ -151,7 +152,7 @@ $map->mergeAssociative(...$others): static;
151152
$map->mergePairs(...$others): static;
152153
```
153154

154-
Conversely, changes can be applied to List containers but with adapted methods around list handling:
155+
Conversely, changes can be applied to `OuterList` and `InnerList` with adapted methods around list handling:
155156

156157
```php
157158
use Bakame\Http\StructuredFields\InnerList;
@@ -167,7 +168,7 @@ echo $list->toHttpValue(); //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
167168
echo $list; //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
168169
```
169170

170-
List containers exhibit the following modifying methods:
171+
`OuterList` and `InnerList` exhibit the following modifying methods:
171172

172173
```php
173174
$list->unshift(...$members): static;
@@ -176,8 +177,8 @@ $list->insert($key, ...$members): static;
176177
$list->replace($key, $member): static;
177178
```
178179

179-
Last but not least you can attach and update to `Item` and `InnerList` instances a `Parameters` instance using the
180-
following methods:
180+
Last but not least you can attach, read and update a `Parameters` instance using the
181+
following methods on `Item` and `InnerList` instances:
181182

182183
```php
183184
$field->parameter($key): mixed|null;
@@ -263,6 +264,21 @@ $integer = Item::fromPair([42]);
263264
$integer->type(); //return Type::Integer
264265
```
265266

267+
Here's the complete list of named constructors attached to the `Item` object. They all call the `Item::from` method,
268+
so they all expect an associative iterable to represents the parameters.
269+
270+
```php
271+
use Bakame\Http\StructuredFields\Item;
272+
use Bakame\Http\StructuredFields\Value;
273+
274+
Item::from($value, iterable<string, Value> $associativeParameters = []): self;
275+
Item::fromDecodedByteSequence(string $value, iterable<string, Value> $associativeParameters = []): self;
276+
Item::fromEncodedByteSequence(string $value, iterable<string, Value> $associativeParameters = []): self;
277+
Item::fromTimestamp(int $value, iterable<string, Value> $associativeParameters = []): self;
278+
Item::fromDateFormat(string $dateFormat, string $dateString, iterable<string, Value> $associativeParameters = []): self;
279+
Item::fromPair(array{0:mixed, 1:iterable<array{0:string, 1:StructuredField}>} $pair): self;
280+
```
281+
266282
### Accessing members of Structured Fields Containers.
267283

268284
`Item` are accessible using three (3) methods:
@@ -271,9 +287,10 @@ $integer->type(); //return Type::Integer
271287
use Bakame\Http\StructuredFields\Item;
272288

273289
$item = Item::from(CarbonImmutable::parse('today'));
274-
$item->type(); // return Type::Date;
275-
$item->value() // return CarbonImmutable::parse('today') (because it extends DateTimeImmutable)
276-
$item->parameters(); // returns a Parameters container
290+
$item->type(); // return Type::Date;
291+
$item->value() // return CarbonImmutable::parse('today') (because it extends DateTimeImmutable)
292+
$item->parameters(); // returns a Parameters container
293+
$item->parameter('z'); // returns the Bare Item value or null if the key does not exists
277294
```
278295

279296
All containers implement PHP `IteratorAggregate`, `Countable` and `ArrayAccess` interfaces for easy usage in your codebase.
@@ -292,15 +309,13 @@ To avoid invalid states, the modifying methods from PHP `ArrayAccess` will throw
292309
use them:
293310

294311
```php
295-
use Bakame\Http\StructuredFields\Item;
312+
use Bakame\Http\StructuredFields\Parameters;
296313

297-
$value = Item::from("Hello world", [
298-
'a' => 'foobar',
299-
]);
300-
$value->parameters('b'); // return null
301-
$value->parameters('a'); // return 'foobar'
302-
$value->parameters()['a'] = 23 // triggers a ForbiddenOperation exception
303-
unset($value->parameters()['a']); // triggers a ForbiddenOperation exception
314+
$value = Parameters::fromAssociative(['a' => 'foobar']);
315+
$value->has('b'); // return false
316+
$value['a']->value(); // return 'foobar'
317+
$value['a'] = 23 // triggers a ForbiddenOperation exception
318+
unset($value['a']); // triggers a ForbiddenOperation exception
304319
```
305320

306321
Apart from the PHP interfaces with the `Dictionary` and the `Parameters` classes you can access the members as pairs:

0 commit comments

Comments
 (0)