@@ -18,34 +18,27 @@ use Bakame\Http\StructuredFields\Item;
1818use Bakame\Http\StructuredFields\OuterList;
1919use Bakame\Http\StructuredFields\Token;
2020
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+ ])
3039 )
3140 ->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
4942```
5043
5144## System Requirements
@@ -62,6 +55,13 @@ composer require bakame/http-structured-fields
6255
6356## Documentation
6457
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+
6565### Parsing and Serializing Structured Fields
6666
6767Parsing the header value is done via the ` fromHttpValue ` named constructor
@@ -84,11 +84,12 @@ $field->value(); // returns Token::fromString('bar); the found token va
8484$field->parameter('baz'); // returns 42; the value of the parameter or null if the parameter is not defined.
8585```
8686
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.
9293
9394```` php
9495use Bakame\Http\StructuredFields\Item;
@@ -149,8 +150,8 @@ ByteSequence::fromDecoded(string|Stringable $value): ByteSequence;
149150ByteSequence::fromEncoded(string|Stringable $value): ByteSequence;
150151```
151152
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:
154155
155156``` php
156157use Bakame\Http\StructuredFields\Token;
@@ -175,8 +176,8 @@ from a string or a string like object**
175176
176177#### Item
177178
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:
180181
181182``` php
182183use Bakame\Http\StructuredFields\Item;
@@ -335,23 +336,28 @@ use Bakame\Http\StructuredFields\Item;
335336use Bakame\Http\StructuredFields\Token;
336337
337338$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+ ))
339345 ->prepend('b', Item::false())
340346 ->append('c', Item::fromDateString('2022-12-23 13:00:23'))
341347;
342348
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
345351```
346352
347353#### Automatic conversion.
348354
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
350356the member argument of each modifying methods, if the submitted type is:
351357
352358- 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 .
355361
356362This means that the previous example can be rewritten like this:
357363
@@ -361,13 +367,13 @@ use Bakame\Http\StructuredFields\Item;
361367use Bakame\Http\StructuredFields\Token;
362368
363369$value = Dictionary::new()
364- ->add('a', [Token::fromString('bar'), 'bar' ])
370+ ->add('a', [Token::fromString('bar'), '42', 42, 42.0 ])
365371 ->prepend('b', false)
366372 ->append('c', new DateTimeImmutable('2022-12-23 13:00:23'))
367373;
368374
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
371377```
372378
373379#### Lists
0 commit comments