@@ -19,8 +19,8 @@ use Bakame\Http\StructuredFields\OuterList;
19
19
use Bakame\Http\StructuredFields\Token;
20
20
21
21
//1 - parsing an Accept Header
22
- $acceptHeaderValue = 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8';
23
- $field = OuterList::fromHttpValue($acceptHeaderValue );
22
+ $headerValue = 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8';
23
+ $field = OuterList::fromHttpValue($headerValue );
24
24
$field[2]->value()->toString(); // returns 'application/xml'
25
25
$field[2]->parameter('q'); // returns (float) 0.9
26
26
$field[0]->value()->toString(); // returns 'text/html'
@@ -38,7 +38,7 @@ echo OuterList::new(
38
38
])
39
39
)
40
40
->toHttpValue();
41
- // return ("foo" "bar");expire=@1681504328;path="/";max-age=2500;secure;httponly=?0;samesite=lax
41
+ // returns ("foo" "bar");expire=@1681504328;path="/";max-age=2500;secure;httponly=?0;samesite=lax
42
42
```
43
43
44
44
## System Requirements
@@ -57,15 +57,15 @@ composer require bakame/http-structured-fields
57
57
58
58
### Foreword
59
59
60
- ** While this package parses, builds, updates and serializes the header value,
61
- it does not in any shape or form validate its content. It is still required
62
- to validate the parse data against the RFC rules of the corresponding header.
63
- Content validation is out of scope for this library.**
60
+ ** While this package parses and serializes the header value, it does not validate its content.
61
+ It is still required to validate the parsed data against the constraints of the corresponding
62
+ header. Content validation is out of scope for this library.**
64
63
65
64
### Parsing and Serializing Structured Fields
66
65
67
- Parsing the header value is done via the ` fromHttpValue ` named constructor
68
- attached to each library's structured fields representation as shown below:
66
+ Parsing the header value is done via the ` fromHttpValue ` named constructor.
67
+ The method is attached to each library's structured fields representation
68
+ as shown below:
69
69
70
70
``` php
71
71
declare(strict_types=1);
@@ -75,35 +75,32 @@ require 'vendor/autoload.php';
75
75
use Bakame\Http\StructuredFields\Item;
76
76
77
77
// the raw HTTP field value is given by your application
78
- // via any given framework or package or super global.
79
- // We are using a PSR-7 Request object in this example
78
+ // via any given framework, package or super global.
80
79
81
- $headerLine = $request->getHeaderLine('foo'); // 'foo: bar;baz=42' the raw header line is a structured field item
80
+ $headerLine = ' bar;baz=42'; // the raw header line is a structured field item
82
81
$field = Item::fromHttpValue($headerLine);
83
82
$field->value(); // returns Token::fromString('bar); the found token value
84
83
$field->parameter('baz'); // returns 42; the value of the parameter or null if the parameter is not defined.
85
84
```
86
85
87
- The ` fromHttpValue ` method returns an instance which implements
88
- the ` StructuredField ` interface. The interface provides the
89
- ` toHttpValue ` method that serializes it into a normalized
90
- RFC compliant HTTP field string value. To ease integration,
91
- the ` __toString ` method is implemented as an alias to the
92
- ` toHttpValue ` method.
86
+ The ` fromHttpValue ` method returns an instance which implements the ` StructuredField ` interface.
87
+ The interface provides the ` toHttpValue ` method that serializes it into a normalized RFC
88
+ compliant HTTP field string value. To ease integration, the ` __toString ` method is
89
+ implemented as an alias to the ` toHttpValue ` method.
93
90
94
91
```` php
95
92
use Bakame\Http\StructuredFields\Item;
96
93
97
- $bar = Item::fromHttpValue('bar; baz=42; secure=?1');
98
- echo $bar->toHttpValue(); // return 'bar;baz=42;secure' on serialization the field has been normalized
94
+ $field = Item::fromHttpValue('bar; baz=42; secure=?1');
95
+ echo $field->toHttpValue(); // return 'bar;baz=42;secure'
96
+ // on serialization the field has been normalized
99
97
100
- // the HTTP response object is build by your application
101
- // via your framework, a package or a native PHP function.
102
- // We are using Symfony Response object in this example
98
+ // the HTTP response is build by your application
99
+ // via any given framework, package or PHP native function.
103
100
104
- $newResponse = $response->headers->set ('foo', $bar ->toHttpValue());
101
+ header ('foo: '. $field ->toHttpValue());
105
102
//or
106
- $newResponse = $response->headers->set ('foo', $bar );
103
+ header ('foo: '. $field );
107
104
````
108
105
109
106
All five (5) structured data type as defined in the RFC are provided inside the
@@ -176,16 +173,16 @@ from a string or a string like object**
176
173
177
174
#### Item
178
175
179
- The defined types are all attached to the ` Item ` object where their values and
180
- types are accessible using the following methods:
176
+ The defined types are all attached to an ` Item ` object where their value and
177
+ type are accessible using the following methods:
181
178
182
179
``` php
183
180
use Bakame\Http\StructuredFields\Item;
184
181
use Bakame\Http\StructuredFields\Type;
185
182
186
183
$item = Item::fromHttpValue('@1234567890');
187
184
$item->type(); // return Type::Date;
188
- $item->value() // return the equivalent to DateTimeImmutable('2009-02-13T23:31:30.000+00:00 ');
185
+ $item->value() // return the equivalent to DateTimeImmutable('@1234567890 ');
189
186
// you can also do
190
187
Type::Date->equals($item); // returns true
191
188
```
@@ -196,13 +193,15 @@ All containers objects implement PHP `IteratorAggregate`, `Countable` and `Array
196
193
interfaces. Their members can be accessed using the following shared methods
197
194
198
195
``` php
199
- $container->keys(): array<string >;
196
+ $container->keys(): array<string |int >;
200
197
$container->has(string|int ...$offsets): bool;
201
198
$container->get(string|int $offset): StrucuredField;
202
199
$container->hasMembers(): bool;
203
200
$container->hasNoMembers(): bool;
204
201
```
205
202
203
+ ** The ` get ` method will throw an ` InvalidOffset ` exception if no member exists for the given ` $offset ` .**
204
+
206
205
To avoid invalid states, ` ArrayAccess ` modifying methods throw a ` ForbiddenOperation `
207
206
if you try to use them on any container object:
208
207
@@ -225,6 +224,8 @@ $container->pair(int $offset): array{0:string, 1:StructuredField};
225
224
$container->toPairs(): iterable<array {0:string, 1:StructuredField} >;
226
225
```
227
226
227
+ ** The ` pair ` method will throw an ` InvalidOffset ` exception if no member exists for the given ` $offset ` .**
228
+
228
229
#### Accessing the parameters values
229
230
230
231
Accessing the associated ` Parameters ` instance attached to an ` InnerList ` or a ` Item ` instances
@@ -241,7 +242,7 @@ InnerList::toPair(): array{0:list<Item>, 1:Parameters}>};
241
242
Item::toPair(): array{0:ByteSequence|Token|DateTimeImmutable|Stringable|string|int|float|bool, 1:Parameters}>};
242
243
```
243
244
244
- ** Of note: the ` parameter ` method will return ` null ` if no value is found for the given index.**
245
+ ** The ` parameter ` method will return ` null ` if no value is found for the given index.**
245
246
246
247
### Building and Updating Structured Fields Values
247
248
@@ -315,10 +316,12 @@ echo $value->toHttpValue(); //;b=?0;a=bar;c=@1671800423
315
316
echo $value; //;b=?0;a=bar;c=@1671800423
316
317
```
317
318
318
- If the preference is to use the builder pattern, the same result can be achieved with the following steps.
319
- First create a ` Parameters ` or a ` Dictionary ` instance using the ` new ` named constructor which
320
- returns a new instance with no members. And then, use any of the following modifying methods
321
- to populate it.
319
+ If the preference is to use the builder pattern, the same result can be achieved with the
320
+ following steps:
321
+
322
+ - First create a ` Parameters ` or a ` Dictionary ` instance using the ` new ` named constructor which
323
+ returns a new instance with no members.
324
+ - And then, use any of the following modifying methods to populate it.
322
325
323
326
``` php
324
327
$map->add(string $key, $value): static;
@@ -350,14 +353,18 @@ echo $value->toHttpValue(); //b=?0, a=(bar "42" 42 42.0), c=@1671800423
350
353
echo $value; //b=?0, a=(bar "42" 42 42.0), c=@1671800423
351
354
```
352
355
353
- #### Automatic conversion.
356
+ #### Automatic conversion
354
357
355
358
For all containers, to ease instantiation the following automatic conversion are applied on
356
- the member argument of each modifying methods, if the submitted type is:
359
+ the member argument of each modifying methods.
360
+
361
+ If the submitted type is:
357
362
358
363
- a ` StructuredField ` implementing object, it will be passed as is
359
364
- an iterable structure, it will be converted to an ` InnerList ` instance using ` InnerList::new `
360
- - otherwise, the method will try to convert it into an ` Item ` using ` Item::new ` following the conversion rule in the table above.
365
+ - otherwise, it is converted into an ` Item ` using ` Item::new ` following the conversion rules explained in the table above.
366
+
367
+ If no conversion is possible an ` InvalidArgument ` exception will be thrown.
361
368
362
369
This means that the previous example can be rewritten like this:
363
370
@@ -376,10 +383,12 @@ echo $value->toHttpValue(); //b=?0, a=(bar "42" 42 42.0), c=@1671800423
376
383
echo $value; //b=?0, a=(bar "42" 42 42.0), c=@1671800423
377
384
```
378
385
386
+ Of course, it is possible to mix both notation as shown in the example.
387
+
379
388
#### Lists
380
389
381
- To Create ` OuterList ` and ` InnerList ` instances you can use the ` new ` named constructor
382
- which takes fron 0 to n members:
390
+ To create ` OuterList ` and ` InnerList ` instances you can use the ` new ` named constructor
391
+ which takes a single variadic parameter ` $ members` :
383
392
384
393
``` php
385
394
use Bakame\Http\StructuredFields\InnerList;
@@ -395,9 +404,8 @@ echo $list->toHttpValue(); //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
395
404
echo $list; //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
396
405
```
397
406
398
- Once again, the builder pattern can be achieved with a combinason of
399
- using the ` new ` named constructor and the using any of the following
400
- modifying methods.
407
+ Once again, the builder pattern can be used via a combination of the ` new `
408
+ named constructor and the use any of the following modifying methods.
401
409
402
410
``` php
403
411
$list->unshift(...$members): static;
0 commit comments