@@ -18,34 +18,27 @@ use Bakame\Http\StructuredFields\Item;
18
18
use Bakame\Http\StructuredFields\OuterList;
19
19
use Bakame\Http\StructuredFields\Token;
20
20
21
- echo OuterList::new()
22
- ->push(
23
- InnerList::new(Item::fromString('foo'), Item::fromString('bar'))
24
- ->addParameter('expire', Item::fromDateString('+30 minutes'))
25
- ->addParameter('path', '/')
26
- ->addParameter('max-age', 2500)
27
- ->addParameter('secure', true)
28
- ->addParameter('httponly', false)
29
- ->addParameter('samesite', Token::fromString('lax'))
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);
24
+ $field[2]->value()->toString(); // returns 'application/xml'
25
+ $field[2]->parameter('q'); // returns (float) 0.9
26
+ $field[0]->value()->toString(); // returns 'text/html'
27
+ $field[0]->parameter('q'); // returns null
28
+
29
+ //2 - building a Retrofit Cookie Header
30
+ echo OuterList::new(
31
+ InnerList::fromAssociative(['foo', 'bar'], [
32
+ 'expire' => $expire,
33
+ 'path' => '/',
34
+ 'max-age' => 2500,
35
+ 'secure' => true,
36
+ 'httponly' => true,
37
+ 'samesite' => BakameToken::fromString('lax'),
38
+ ])
30
39
)
31
40
->toHttpValue();
32
-
33
- // or
34
-
35
- echo OuterList::new(
36
- InnerList::fromAssociative(['foo', 'bar'], [
37
- 'expire' => new DateTimeImmutable('+30 minutes'),
38
- 'path' => '/',
39
- 'max-age' => 2500,
40
- 'secure' => true,
41
- 'httponly' => true,
42
- 'samesite' => Token::fromString('lax'),
43
- ])
44
- );
45
-
46
- // Both code snippet return
47
- // ("foo" "bar");expire=@1681504328;path="/";max-age=2500;secure;httponly=?0;samesite=lax
48
- // the retrofit representation of the cookie header
41
+ // return ("foo" "bar");expire=@1681504328;path="/";max-age=2500;secure;httponly=?0;samesite=lax
49
42
```
50
43
51
44
## System Requirements
@@ -62,6 +55,13 @@ composer require bakame/http-structured-fields
62
55
63
56
## Documentation
64
57
58
+ ### Foreword
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.**
64
+
65
65
### Parsing and Serializing Structured Fields
66
66
67
67
Parsing the header value is done via the ` fromHttpValue ` named constructor
@@ -84,11 +84,12 @@ $field->value(); // returns Token::fromString('bar); the found token va
84
84
$field->parameter('baz'); // returns 42; the value of the parameter or null if the parameter is not defined.
85
85
```
86
86
87
- The ` fromHttpValue ` method returns an instance which implements the` StructuredField ` interface.
88
- The interface provides the ` toHttpValue ` method that serializes it into a normalized
89
- RFC compliant HTTP field string value.
90
-
91
- To ease integration, the ` __toString ` method is implemented as an alias to the ` toHttpValue ` method.
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.
92
93
93
94
```` php
94
95
use Bakame\Http\StructuredFields\Item;
@@ -149,8 +150,8 @@ ByteSequence::fromDecoded(string|Stringable $value): ByteSequence;
149
150
ByteSequence::fromEncoded(string|Stringable $value): ByteSequence;
150
151
```
151
152
152
- Both classes are final and immutable; their value can not be modified once instantiated.
153
- To access their value, they expose the following API:
153
+ Both classes are final and immutable; their value can not be modified once
154
+ instantiated. To access their value, they expose the following API:
154
155
155
156
``` php
156
157
use Bakame\Http\StructuredFields\Token;
@@ -175,8 +176,8 @@ from a string or a string like object**
175
176
176
177
#### Item
177
178
178
- The defined types are all attached to the ` Item ` object where their values and types
179
- are accessible using the following methods:
179
+ The defined types are all attached to the ` Item ` object where their values and
180
+ types are accessible using the following methods:
180
181
181
182
``` php
182
183
use Bakame\Http\StructuredFields\Item;
@@ -335,23 +336,28 @@ use Bakame\Http\StructuredFields\Item;
335
336
use Bakame\Http\StructuredFields\Token;
336
337
337
338
$value = Dictionary::new()
338
- ->add('a', InnerList::new(Item::fromToken('bar'), tem::fromString('bar'))
339
+ ->add('a', InnerList::new(
340
+ Item::fromToken('bar'),
341
+ Item::fromString('42'),
342
+ Item::fromInteger(42),
343
+ Item::fromDecimal(42)
344
+ ))
339
345
->prepend('b', Item::false())
340
346
->append('c', Item::fromDateString('2022-12-23 13:00:23'))
341
347
;
342
348
343
- echo $value->toHttpValue(); //b=?0, a=(bar "bar" ), c=@1671800423
344
- echo $value; //b=?0, a=(bar "bar" ), c=@1671800423
349
+ echo $value->toHttpValue(); //b=?0, a=(bar "42" 42 42.0 ), c=@1671800423
350
+ echo $value; //b=?0, a=(bar "42" 42 42.0 ), c=@1671800423
345
351
```
346
352
347
353
#### Automatic conversion.
348
354
349
- For all containers to ease instantiaiton the following automatic conversion are applied on
355
+ For all containers, to ease instantiation the following automatic conversion are applied on
350
356
the member argument of each modifying methods, if the submitted type is:
351
357
352
358
- a ` StructuredField ` implementing object, it will be passed as is
353
- - an iterable structure it will be converted to an ` InnerList ` instance using ` InnerList::new `
354
- - otherwise the value will be converted to an ` Item ` using ` Item::new ` .
359
+ - 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 .
355
361
356
362
This means that the previous example can be rewritten like this:
357
363
@@ -361,13 +367,13 @@ use Bakame\Http\StructuredFields\Item;
361
367
use Bakame\Http\StructuredFields\Token;
362
368
363
369
$value = Dictionary::new()
364
- ->add('a', [Token::fromString('bar'), 'bar' ])
370
+ ->add('a', [Token::fromString('bar'), '42', 42, 42.0 ])
365
371
->prepend('b', false)
366
372
->append('c', new DateTimeImmutable('2022-12-23 13:00:23'))
367
373
;
368
374
369
- echo $value->toHttpValue(); //" b=?0, a=bar, c=@1671800423"
370
- echo $value; //" b=?0, a=bar, c=@1671800423"
375
+ echo $value->toHttpValue(); //b=?0, a=( bar "42" 42 42.0) , c=@1671800423
376
+ echo $value; //b=?0, a=( bar "42" 42 42.0) , c=@1671800423
371
377
```
372
378
373
379
#### Lists
0 commit comments