@@ -13,32 +13,31 @@ build and update HTTP Structured Fields in PHP according to the [RFC8941](https:
13
13
Once installed you will be able to do the following:
14
14
15
15
``` php
16
- use Bakame\Http\StructuredFields\InnerList;
17
- use Bakame\Http\StructuredFields\OuterList;
16
+ use Bakame\Http\StructuredFields\DataType;
18
17
use Bakame\Http\StructuredFields\Token;
19
18
20
19
//1 - parsing an Accept Header
21
20
$headerValue = 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8';
22
- $field = http_parse_structured_field('list', $headerValue);
21
+ $field = DataType::List->parse( $headerValue);
23
22
$field[2]->value()->toString(); // returns 'application/xml'
24
23
$field[2]->parameter('q'); // returns (float) 0.9
25
24
$field[0]->value()->toString(); // returns 'text/html'
26
25
$field[0]->parameter('q'); // returns null
27
26
28
27
//2 - building a Retrofit Cookie Header
29
- echo http_build_structured_field('list', [
28
+ echo DataType::List->build [
30
29
[
31
30
['foo', 'bar'],
32
31
[
33
- 'expire' => $expire,
34
- 'path' => '/',
35
- 'max-age' => 2500,
36
- 'secure' => true,
37
- 'httponly' => true,
38
- 'samesite' => Token::fromString('lax'),
32
+ [ 'expire', $expire] ,
33
+ [ 'path', '/'] ,
34
+ [ 'max-age', 2500] ,
35
+ [ 'secure', true] ,
36
+ [ 'httponly', true] ,
37
+ [ 'samesite', Token::fromString('lax')] ,
39
38
]
40
- ]
41
- ]);
39
+ ],
40
+ ]),
42
41
// returns ("foo" "bar");expire=@1681504328;path="/";max-age=2500;secure;httponly=?0;samesite=lax
43
42
```
44
43
@@ -65,22 +64,66 @@ header. Content validation is out of scope for this library.
65
64
66
65
### Parsing and Serializing Structured Fields
67
66
68
- #### Basic usage
67
+ #### Basic Usage
68
+
69
+ > [ !NOTE]
70
+ > New in version 1.2.0
71
+
72
+ The ` DataType ` enum serves as a factory class to quickly parse and build a structured field.
73
+ The enum list all available data type according to the RFC. To parse a header you need to
74
+ give the ` parse ` method a string or a stringable object. On success, it will return a
75
+ ` Bakame\Http\StruncturedFields\StruncturedField ` implementing object otherwise an
76
+ exception will be thrown.
77
+
78
+ ``` php
79
+ $headerLine = 'bar;baz=42'; //the raw header line is a structured field item
80
+ $field = DataType::Item->parse($headerLine);
81
+ $field->value(); // returns Token::fromString('bar); the found token value
82
+ $field->parameter('baz'); // returns 42; the value of the parameter or null if the parameter is not defined.
83
+ ```
84
+
85
+ On the other hand, ` build ` method expects an iterable structure composed
86
+ of pair values that matches each data type and returns the structured field text representation
87
+ of the header.
88
+
89
+ ``` php
90
+ use Bakame\Http\StructuredFields\Item;
91
+ use Bakame\Http\StructuredFields\DataType;
92
+
93
+ echo DataType::List->build([
94
+ [
95
+ 'dumela lefatshe',
96
+ [['a', false]]
97
+ ],
98
+ [
99
+ ['a', 'b', Item::fromDateString('+30 minutes')],
100
+ [['a', true]]
101
+ ],
102
+ ]);
103
+ // display "dumela lefatshe";a=?0, ("a" "b" @1703319068);a
104
+ ```
105
+
106
+ The data type can be given as a string or using the ` DataType ` enum.
69
107
70
- Parsing the header value is done via the ` fromHttpValue ` named constructor.
71
- The method is attached to each library's structured fields representation
72
- as shown below:
108
+ #### Using specific named constructor
109
+
110
+ The package provides specific classes for each data type. if you do not wish to
111
+ use the ` DataType ` factoring, parsing the header value is done via the
112
+ ` fromHttpValue ` named constructor. The method is attached to each library's
113
+ structured fields representation as shown below:
73
114
74
115
``` php
75
116
declare(strict_types=1);
76
117
118
+ use Bakame\Http\StructuredFields\DataType;
119
+
77
120
require 'vendor/autoload.php';
78
121
79
122
// the raw HTTP field value is given by your application
80
123
// via any given framework, package or super global.
81
124
82
125
$headerLine = 'bar;baz=42'; //the raw header line is a structured field item
83
- $field = parse ($headerLine, 'item' );
126
+ $field = Item::fromHttpValue ($headerLine);
84
127
$field->value(); // returns Token::fromString('bar); the found token value
85
128
$field->parameter('baz'); // returns 42; the value of the parameter or null if the parameter is not defined.
86
129
```
@@ -91,10 +134,7 @@ compliant HTTP field string value. To ease integration, the `__toString` method
91
134
implemented as an alias to the ` toHttpValue ` method.
92
135
93
136
```` php
94
- use function Bakame\Http\StructuredFields\http_sf_parse;
95
- use function Bakame\Http\StructuredFields\http_sf_build;
96
-
97
- $field = http_sf_parse('bar; baz=42; secure=?1', 'item');
137
+ $field = Item::fromHttpValue('bar; baz=42; secure=?1');
98
138
echo $field->toHttpValue(); // return 'bar;baz=42;secure'
99
139
// on serialization the field has been normalized
100
140
@@ -104,8 +144,6 @@ echo $field->toHttpValue(); // return 'bar;baz=42;secure'
104
144
header('foo: '. $field->toHttpValue());
105
145
//or
106
146
header('foo: '. $field);
107
- //or
108
- header('foo: '. http_sf_build($field));
109
147
````
110
148
111
149
All five (5) structured data type as defined in the RFC are provided inside the
@@ -211,9 +249,10 @@ To ease validation a `Type::equals` method is exposed to check if the `Item` has
211
249
the expected type. It can also be used to compare types.
212
250
213
251
``` php
252
+ use Bakame\Http\StructuredFields\DataType;
214
253
use Bakame\Http\StructuredFields\Type;
215
254
216
- $field = Item::fromHttpValue ('"foo"');
255
+ $field = DataType::Item->parse ('"foo"');
217
256
Type::Date->equals($field); // returns false
218
257
Type::String->equals($field); // returns true;
219
258
Type::Boolean->equals(Type::String); // returns false
@@ -302,7 +341,7 @@ if you try to use them on any container object:
302
341
``` php
303
342
use Bakame\Http\StructuredFields\Parameters;
304
343
305
- $value = Parameters::fromHttpValue(';a=foobar'] );
344
+ $value = Parameters::fromHttpValue(';a=foobar');
306
345
$value->has('b'); // return false
307
346
$value['a']->value(); // return 'foobar'
308
347
$value['b']; // triggers a InvalidOffset exception, the index does not exist
@@ -613,6 +652,37 @@ echo $list->toHttpValue(); //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
613
652
echo $list; //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
614
653
```
615
654
655
+ > [ !NOTE]
656
+ > New in version 1.2.0
657
+
658
+ It is also possible to create an ` OuterList ` based on an iterable structure
659
+ of pairs.
660
+
661
+ ``` php
662
+ use Bakame\Http\StructuredFields\OuterList;
663
+
664
+ $list = OuterList::fromPairs([
665
+ [
666
+ ['foo', 'bar'],
667
+ [
668
+ ['expire', $expire],
669
+ ['path', '/'],
670
+ [ 'max-age', 2500],
671
+ ['secure', true],
672
+ ['httponly', true],
673
+ ['samesite', Token::fromString('lax')],
674
+ ]
675
+ ],
676
+ [
677
+ 'coucoulesamis',
678
+ [['a', false]],
679
+ ]
680
+ ]);
681
+ ```
682
+
683
+ The pairs definitions are the same as for creating either a ` InnerList ` or an ` Item ` using
684
+ their respective ` fromPair ` method.
685
+
616
686
#### Adding and updating parameters
617
687
618
688
To ease working with instances that have a ` Parameters ` object attached to, the following
0 commit comments