@@ -19,7 +19,7 @@ use Bakame\Http\StructuredFields\Item;
19
19
$field = Item::from("/terms", ['rel' => 'copyright', 'anchor' => '#foo']);
20
20
echo $field->toHttpValue(); // display "/terms";rel="copyright";anchor="#foo"
21
21
echo $field->value(); // display "/terms"
22
- echo $field->parameters()[ 'rel']->value(); // display "copyright"
22
+ echo $field->parameter( 'rel'); // display "copyright"
23
23
```
24
24
25
25
## System Requirements
@@ -86,10 +86,11 @@ namespace
86
86
87
87
they all implement the ` StructuredField ` interface and expose a ` fromHttpValue ` named constructor:
88
88
89
- ### Building and Updating Structured Fields
89
+ ### Building and Updating Structured Fields Values
90
90
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
93
94
94
95
``` php
95
96
use Bakame\Http\StructuredFields\Dictionary;
@@ -104,7 +105,7 @@ echo $value->toHttpValue(); //"b=?0, a=bar;baz=42, c=@1671800423"
104
105
echo $value; //"b=?0, a=bar;baz=42, c=@1671800423"
105
106
```
106
107
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:
108
109
109
110
``` php
110
111
use Bakame\Http\StructuredFields\Parameters;
@@ -141,7 +142,7 @@ echo $value; //"b=?0, a=bar;baz=42, c=@1671800423"
141
142
Because we are using immutable value objects any change to the value object will return a new instance with
142
143
the changes applied and leave the original instance unchanged.
143
144
144
- Ordered Map Containers exhibits the following modifying methods:
145
+ ` Dictionary ` and ` Parameters ` exhibit the following modifying methods:
145
146
146
147
``` php
147
148
$map->add($key, $value): static;
@@ -151,7 +152,7 @@ $map->mergeAssociative(...$others): static;
151
152
$map->mergePairs(...$others): static;
152
153
```
153
154
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:
155
156
156
157
``` php
157
158
use Bakame\Http\StructuredFields\InnerList;
@@ -167,7 +168,7 @@ echo $list->toHttpValue(); //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
167
168
echo $list; //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
168
169
```
169
170
170
- List containers exhibit the following modifying methods:
171
+ ` OuterList ` and ` InnerList ` exhibit the following modifying methods:
171
172
172
173
``` php
173
174
$list->unshift(...$members): static;
@@ -176,8 +177,8 @@ $list->insert($key, ...$members): static;
176
177
$list->replace($key, $member): static;
177
178
```
178
179
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 :
181
182
182
183
``` php
183
184
$field->parameter($key): mixed|null;
@@ -263,6 +264,21 @@ $integer = Item::fromPair([42]);
263
264
$integer->type(); //return Type::Integer
264
265
```
265
266
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
+
266
282
### Accessing members of Structured Fields Containers.
267
283
268
284
` Item ` are accessible using three (3) methods:
@@ -271,9 +287,10 @@ $integer->type(); //return Type::Integer
271
287
use Bakame\Http\StructuredFields\Item;
272
288
273
289
$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
277
294
```
278
295
279
296
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
292
309
use them:
293
310
294
311
``` php
295
- use Bakame\Http\StructuredFields\Item ;
312
+ use Bakame\Http\StructuredFields\Parameters ;
296
313
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
304
319
```
305
320
306
321
Apart from the PHP interfaces with the ` Dictionary ` and the ` Parameters ` classes you can access the members as pairs:
0 commit comments