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
10
` bakame/http-structured-fields ` is a framework-agnostic PHP library that allows you to parse, serialize
11
- and build HTTP Structured Fields in PHP according to the [ RFC8941] ( https://www.rfc-editor.org/rfc/rfc8941.html ) .
11
+ build and update HTTP Structured Fields in PHP according to the [ RFC8941] ( https://www.rfc-editor.org/rfc/rfc8941.html ) .
12
12
13
13
## System Requirements
14
14
@@ -30,7 +30,6 @@ Once the library is installed parsing the header value is done via the normalize
30
30
constructor attached to library's structured fields representation as shown below:
31
31
32
32
``` php
33
-
34
33
declare(strict_types=1);
35
34
36
35
require 'vendor/autoload.php';
@@ -58,8 +57,8 @@ the `toHttpValue` method.
58
57
```` php
59
58
use Bakame\Http\StructuredFields\Item;
60
59
61
- $bar = Item::fromToken ('bar')->addParameter('baz', 42 );
62
- echo $bar->toHttpValue(); // return 'bar;baz=42'
60
+ $bar = Item::fromHttpValue ('bar; baz=42; secure=?1' );
61
+ echo $bar->toHttpValue(); // return 'bar;baz=42;secure' on serialization the field has been normalized
63
62
64
63
// the HTTP response object is build by your application
65
64
// via your framework, a package or a native PHP function.
@@ -103,7 +102,7 @@ The table below summarizes the item value type.
103
102
104
103
As shown in the table, the RFC define two (2) specific data types that can not be represented by
105
104
PHP default type system, for them, we have defined two classes ` Token ` and ` ByteSequence ` to help
106
- with represention .
105
+ with representation .
107
106
108
107
``` php
109
108
use Bakame\Http\StructuredFields\Token;
@@ -147,11 +146,9 @@ are accessible using the following methods:
147
146
use Bakame\Http\StructuredFields\Item;
148
147
use Bakame\Http\StructuredFields\Type;
149
148
150
- //@type SfItemInput ByteSequence|Token|DateTimeImmutable|Stringable|string|int|float|bool
151
- // the Item::value() can return one of those type
152
- $item = Item::fromDate(CarbonImmutable::parse('today'));
149
+ $item = Item::fromHttpValue('@1234567890');
153
150
$item->type(); // return Type::Date;
154
- $item->value() // return a CarbonImmutable instance because it extends DateTimeImmutable
151
+ $item->value() // return the equivalent to DateTimeImmutable('2009-02-13T23:31:30.000+00:00');
155
152
// you can also do
156
153
Type::Date->equals($item); // returns true
157
154
```
@@ -175,7 +172,7 @@ if you try to use them on any container object:
175
172
``` php
176
173
use Bakame\Http\StructuredFields\Parameters;
177
174
178
- $value = Parameters::fromAssociative(['a' => ' foobar']);
175
+ $value = Parameters::fromHttpValue(';a= foobar']);
179
176
$value->has('b'); // return false
180
177
$value['a']->value(); // return 'foobar'
181
178
$value['b']; // triggers a SyntaxError exception, the index does not exist
@@ -212,7 +209,7 @@ Every value object can be used as a builder to create an HTTP field value.
212
209
213
210
#### Items value
214
211
215
- The ` Item ` value object exposes lots of named constructors to construct
212
+ The ` Item ` value object exposes a number of named constructors to construct
216
213
bare items (ie: item without parameters attached to them).
217
214
218
215
``` php
@@ -223,10 +220,10 @@ Item::fromDecodedByteSequence(Stringable|string $value): self;
223
220
Item::fromEncodedByteSequence(Stringable|string $value): self;
224
221
Item::fromToken(Stringable|string $value): self;
225
222
Item::fromString(Stringable|string $value): self;
226
- Item::fromTimestamp(int $value ): self;
223
+ Item::fromDate(DateTimeInterface $datetime ): self;
227
224
Item::fromDateFormat(string $format, string $datetime): self;
228
225
Item::fromDateString(string $datetime, DateTimeZone|string|null $timezone = null): self;
229
- Item::fromDate(DateTimeInterface $datetime ): self;
226
+ Item::fromTimestamp(int $value ): self;
230
227
Item::fromDecimal(int|float $value): self;
231
228
Item::fromInteger(int|float $value): self;
232
229
Item::true(): self;
@@ -238,7 +235,7 @@ To update an `Item` object value, the `Item::withValue` method should be use:
238
235
``` php
239
236
use Bakame\Http\StructuredFields\Item;
240
237
241
- Item::withValue(SfItemInput $value): static
238
+ Item::withValue(mixed $value): static
242
239
```
243
240
244
241
#### Dictionaries
@@ -265,7 +262,7 @@ use Bakame\Http\StructuredFields\Parameters;
265
262
266
263
$value = Parameters::fromPairs([
267
264
['b', false],
268
- ['a', Item::fromPair([Token::fromString( 'bar')] )],
265
+ ['a', Item::fromToken( 'bar')],
269
266
['c', new DateTime('2022-12-23 13:00:23')]
270
267
]);
271
268
@@ -327,14 +324,15 @@ echo $list; //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
327
324
Once again, builder methods exist on both classes to ease container construction.
328
325
329
326
``` php
327
+ use Bakame\Http\StructuredFields\ByteSequence;
330
328
use Bakame\Http\StructuredFields\InnerList;
331
329
use Bakame\Http\StructuredFields\Item;
332
330
333
331
$list = InnerList::new()
334
332
->unshift('42')
335
333
->push(42)
336
334
->insert(1, 42.0)
337
- ->replace(0, Item::fromDecodedByteSequence( 'Hello World'));
335
+ ->replace(0, Item::new(ByteSequence::fromDecoded( 'Hello World') ));
338
336
339
337
echo $list->toHttpValue(); //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
340
338
echo $list; //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
@@ -357,10 +355,12 @@ public API is added. It is also possible to instantiate an `InnerList` or an `It
357
355
instance with included parameters using one of these named constructors:
358
356
359
357
``` php
358
+ use Bakame\Http\StructuredFields\ByteSequence;
360
359
use Bakame\Http\StructuredFields\InnerList;
361
360
use Bakame\Http\StructuredFields\Item;
361
+ use Bakame\Http\StructuredFields\Token;
362
362
363
- //@type SfItemInput ByteSequence|Token|DateTimeInterface|Stringable| string|int|float|bool
363
+ //@type SfItemInput ByteSequence|Token|DateTimeInterface|string|int|float|bool
364
364
365
365
Item::fromAssociative(SfItemInput $value, Parameters|iterable<string , SfItemInput > $parameters): self;
366
366
Item::fromPair(array{0:SfItemInput, 1:Parameters|iterable<array {0:string, 1:SfItemInput} >} $pair): self;
0 commit comments