7
7
[ ![ Total Downloads] ( https://img.shields.io/packagist/dt/bakame/http-structured-fields.svg?style=flat-square )] ( https://packagist.org/packages/bakame/http-structured-fields )
8
8
[ ![ Sponsor development of this project] ( https://img.shields.io/badge/sponsor%20this%20package-%E2%9D%A4-ff69b4.svg?style=flat-square )] ( https://github.com/sponsors/nyamsprod )
9
9
10
- The package uses value objects to parse, serialize, build and update [ HTTP Structured Fields] [ 1 ] in PHP.
10
+ ` bakame/http-structured-fields ` is a framework-agnostic PHP library that allows you to parse, serialize and build HTTP Structured Fields in PHP according to the [ RFC ] [ 1 ] .
11
11
12
12
HTTP Structured fields are intended for use for new HTTP fields that wish to use a common syntax that is
13
- more restrictive than traditional HTTP field values or could be used to [ retrofit current fields] [ 2 ] to
13
+ more restrictive than traditional HTTP field values or could be used to retrofit current fields value to
14
14
have them compliant with the new syntax.
15
15
16
16
``` php
@@ -52,45 +52,65 @@ echo $list[-1]->value(); // returns '/member/*/comments'
52
52
53
53
### Parsing and Serializing Structured Fields
54
54
55
- To parse the string representation of an HTTP field you must use the ` fromHttpValue `
56
- named constructor provided to all the immutable value objects :
55
+ Once the library is installed parsing the header value is done via one of the package value object via its ` fromHttpValue `
56
+ named constructor as shown below :
57
57
58
58
``` php
59
- use Bakame\Http\StructuredFields\Dictionary;
60
59
61
- $dictionary = Dictionary::fromHttpValue("a=?0, b, c=?1; foo=bar");
60
+ declare(strict_types=1);
61
+
62
+ namespace MyApp;
63
+
64
+ require 'vendor/autoload.php';
65
+
66
+ use Bakame\Http\StructuredFields\Item;
67
+
68
+ //the HTTP request object is given by your application
69
+ // or any given framework or package.
70
+ //We use a PSR-7 Request object in this example
71
+
72
+ $headerLine = $request->getHeaderLine('foo');
73
+ $field = Item::fromHttpValue($headerLine);
74
+ $field->value(); // returns the found token value
75
+ $field->parameter('baz'); // returns the value of the parameter or null if the parameter is not defined.
62
76
```
63
77
64
- ` fromHttpValue ` returns an instance of the ` Bakame\Http\StructuredFields\StructuredField ` interface.
78
+ The ` fromHttpValue ` method returns an instance of the ` Bakame\Http\StructuredFields\StructuredField ` interface.
65
79
The interface provides a way to serialize the value object into a normalized RFC compliant HTTP field
66
80
string value using the ` toHttpValue ` method.
67
81
68
82
To ease integration with current PHP frameworks and packages working with HTTP headers and trailers,
69
- each value object also exposes the ` Stringable ` interface method ` __toString ` ,
70
- an alias of the ` toHttpValue ` method.
83
+ each value object also exposes the ` Stringable ` interface method ` __toString ` as an alias
84
+ of the ` toHttpValue ` method.
71
85
72
86
```` php
73
- use Bakame\Http\StructuredFields\OuterList ;
87
+ use Bakame\Http\StructuredFields\Item ;
74
88
75
- $list = OuterList::fromHttpValue('("foo"; a=1;b=2);lvl=5, ("bar" "baz");lvl=1');
76
- echo $list->toHttpValue(); // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
77
- echo $list; // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
89
+ $bar = Item::fromToken('bar')->addParameter('baz', Item::from(42));
90
+ echo $bar->toHttpValue(); // return 'bar;baz=42'
91
+
92
+ //the HTTP response object is given by your application
93
+ // or any given framework or package.
94
+ //We use Symfony Response object in this example
95
+ $newResponse = $response->headers->set('foo', $bar->toHttpValue());
96
+ //or
97
+ $newResponse = $response->headers->set('foo', $bar);
78
98
````
79
99
80
- The library provides currently five (5) immutable value objects define inside the ` Bakame\Http\StructuredFields `
81
- namespace
100
+ The library provides all five (5) structured defined in the RFC inside the ` Bakame\Http\StructuredFields `
101
+ namespace.
82
102
83
103
- ` Item ` ;
84
- - ` Dictionary ` and ` Parameters ` as 2 Ordered Map Containers;
85
- - ` OuterList ` and ` InnerList ` as 2 List Containers ;
104
+ - ` Dictionary ` and ` Parameters ` as 2 Ordered map Containers;
105
+ - ` OuterList ` and ` InnerList ` as 2 list Containers ;
86
106
87
- they all implement the ` StructuredField ` interface and expose a ` fromHttpValue ` named constructor:
107
+ They all implement the ` StructuredField ` interface and expose a ` fromHttpValue ` named constructor:
88
108
89
109
### Building and Updating Structured Fields Values
90
110
91
111
Every value object can be used as a builder to create an HTTP field value.
92
112
93
- For instance, ` Dictionary ` and ` Parameters ` can be build with an associative iterable as shown below
113
+ The ` Dictionary ` and ` Parameters ` instances can be build with an associative iterable structure as shown below
94
114
95
115
``` php
96
116
use Bakame\Http\StructuredFields\Dictionary;
@@ -105,7 +125,7 @@ echo $value->toHttpValue(); //"b=?0, a=bar;baz=42, c=@1671800423"
105
125
echo $value; //"b=?0, a=bar;baz=42, c=@1671800423"
106
126
```
107
127
108
- Or with an iterable structure of pairs as described in the RFC:
128
+ Or with an iterable structure of pairs as per defined in the RFC:
109
129
110
130
``` php
111
131
use Bakame\Http\StructuredFields\Parameters;
@@ -120,7 +140,7 @@ echo $value->toHttpValue(); //;b=?0;a=bar;c=@1671800423
120
140
echo $value; //;b=?0;a=bar;c=@1671800423
121
141
```
122
142
123
- If builder methods are preferred , the same result can be achieved with the following steps:
143
+ If the preference is to use the builder pattern , the same result can be achieved with the following steps:
124
144
125
145
``` php
126
146
use Bakame\Http\StructuredFields\Dictionary;
@@ -129,7 +149,7 @@ use Bakame\Http\StructuredFields\Token;
129
149
130
150
$bar = Item::fromToken('bar')
131
151
->addParameter('baz', Item::from(42));
132
- $dictBuilder = Dictionary::create()
152
+ $value = Dictionary::create()
133
153
->add('a', $bar)
134
154
->prepend('b', Item::from(false))
135
155
->append('c', Item::from(new DateTimeImmutable('2022-12-23 13:00:23')))
@@ -200,28 +220,28 @@ Items can have different types that are translated to PHP using:
200
220
201
221
The table below summarizes the item value type.
202
222
203
- | HTTP DataType | Package Data Type | Package Enum Type |
204
- | ---------------| -------------------------------------------------------- | ----------------------|
205
- | Integer | ` int ` | ` Type::Integer ` |
206
- | Decimal | ` float ` | ` Type::Decimal ` |
207
- | String | ` string ` or ` Stringable ` class | ` Tyoe::String ` |
208
- | Boolean | ` bool ` | ` Type::Boolean ` |
209
- | Token | class ` Token ` | ` Type::Token ` |
210
- | Byte Sequence | class ` ByteSequence ` | ` Type::ByteSequence ` |
211
- | Date | class ` DateTimeImmutable ` or ` DateTimeInterface ` class | ` Type::Date ` |
223
+ | HTTP DataType | Package Data Type | Package Enum Type |
224
+ | ---------------| --------------------------------| ----------------------|
225
+ | Integer | ` int ` | ` Type::Integer ` |
226
+ | Decimal | ` float ` | ` Type::Decimal ` |
227
+ | String | ` string ` or ` Stringable ` class | ` Tyoe::String ` |
228
+ | Boolean | ` bool ` | ` Type::Boolean ` |
229
+ | Token | class ` Token ` | ` Type::Token ` |
230
+ | Byte Sequence | class ` ByteSequence ` | ` Type::ByteSequence ` |
231
+ | Date | class ` DateTimeImmutable ` | ` Type::Date ` |
212
232
213
233
``` php
214
234
use Bakame\Http\StructuredFields\ByteSequence;
215
235
use Bakame\Http\StructuredFields\Item;
216
236
217
237
$item = Item::fromPair([
218
238
"hello world", [
219
- ["a", ByteSequence::fromDecoded("Hello World")],
239
+ ["a", Item::from( ByteSequence::fromDecoded("Hello World") )],
220
240
]
221
241
]);
222
242
$item->value(); // returns "hello world"
223
243
$item->type(); // returns Type::String
224
- $item->parameters("a"); // returns StructuredFields\ ByteSequence::fromDecoded('Hello World');
244
+ $item->parameters("a"); // returns ByteSequence::fromDecoded('Hello World');
225
245
echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
226
246
```
227
247
@@ -276,10 +296,10 @@ Item::fromPair(array{0:mixed, 1:iterable<array{0:string, 1:Value}>} $pair): self
276
296
Item::fromDecodedByteSequence(string $value): self;
277
297
Item::fromEncodedByteSequence(string $value): self;
278
298
Item::fromToken(string $value): self;
279
- Item::fromDate(DateTimeInterface $value ): self;
299
+ Item::fromDate(DateTimeInterface $datetime ): self;
280
300
Item::fromTimestamp(int $value): self;
281
- Item::fromDateFormat(string $dateFormat , string $dateString ): self;
282
- Item::fromDateFormat (string $dateString , DateTimeZone|string|null $timezone): self;
301
+ Item::fromDateFormat(string $format , string $datetime ): self;
302
+ Item::fromDateString (string $datetime , DateTimeZone|string|null $timezone): self;
283
303
```
284
304
285
305
### Accessing members of Structured Fields Containers.
0 commit comments