@@ -25,7 +25,7 @@ $field[0]->value()->toString(); // returns 'text/html'
25
25
$field[0]->parameter('q'); // returns null
26
26
27
27
//2 - building a Retrofit Cookie Header
28
- echo DataType::List->build[
28
+ echo DataType::List->build( [
29
29
[
30
30
['foo', 'bar'],
31
31
[
@@ -69,11 +69,10 @@ header. Content validation is out of scope for this library.
69
69
> [ !NOTE]
70
70
> New in version 1.2.0
71
71
72
- The ` DataType ` enum list all five (5) available data type according to the RFC. It is also a
73
- Factory to enable parsing and building such data types. To parse a field you need to give
74
- the ` parse ` method a string or a stringable object representing the field text
75
- representation. On success, it will return a ` StruncturedField ` implementing
76
- object otherwise an exception will be thrown.
72
+ To quickly parse or build one of the five (5) available data type according to the RFC, you can use the ` DataType ` enum.
73
+ Apart from listing the data types (` List ` , ` InnerList ` , ` Parameters ` , ` Dictionary ` and ` Item ` ) you can give to
74
+ its ` parse ` method a string or a stringable object representing a field text representation. On success,
75
+ it will return an object representing the structured field otherwise an exception will be thrown.
77
76
78
77
``` php
79
78
$headerLine = 'bar;baz=42'; //the raw header line is a structured field item
@@ -102,12 +101,15 @@ echo DataType::List->build([
102
101
// display "dumela lefatshe";a=?0, ("a" "b" @1703319068);a
103
102
```
104
103
104
+ > [ !TIP]
105
+ > While the format can be overwhelming at first, you will come to understand it while reading
106
+ > the rest of the documentation. Under the hood, the ` DataType ` enum uses the mechanism discussed hereafter.
107
+
105
108
#### Using specific named constructor
106
109
107
- To complement the factory and to allow for more fine-grained manipulations, the package
108
- also provides specific classes for each data type. Parsing the header value is done
109
- via the ` fromHttpValue ` named constructor. The method is attached to each
110
- library's structured fields representation as shown below:
110
+ The package provides specific classes for each data type. Parsing the structured field value is done
111
+ via the ` fromHttpValue ` named constructor. The method is attached to each library's structured
112
+ field representation as shown below:
111
113
112
114
``` php
113
115
declare(strict_types=1);
@@ -157,7 +159,7 @@ All five (5) structured data type as defined in the RFC are provided inside the
157
159
158
160
Starting with version ` 1.1 ` the internal parser has been made public in order to allow:
159
161
160
- - clearer decoupling betwen parsing and objet building
162
+ - clearer decoupling between parsing and objet building
161
163
- different parsers implementations
162
164
- improve the package usage in testing.
163
165
@@ -216,37 +218,38 @@ Per the RFC, items can have different types that are translated to PHP using:
216
218
217
219
The table below summarizes the item value type.
218
220
219
- | RFC Type | PHP Type | Package Enum Type |
220
- | -------------------| ---------------------------| -----------------------|
221
- | Integer | ` int ` | ` Type::Integer ` |
222
- | Decimal | ` float ` | ` Type::Decimal ` |
223
- | String | ` string ` | ` Type::String ` |
224
- | Boolean | ` bool ` | ` Type::Boolean ` |
225
- | Token | class ` Token ` | ` Type::Token ` |
226
- | Byte Sequence | class ` ByteSequence ` | ` Type::ByteSequence ` |
227
- | Date (* ) | class ` DateTimeImmutable ` | ` Type::Date ` |
228
- | DisplayString (* ) | class ` DisplayString ` | ` Type::DisplayString ` |
221
+ | RFC Type | PHP Type | Package Enum Name | Package Enum Value |
222
+ | -------------------| ---------------------------| -----------------------| -------------------- |
223
+ | Integer | ` int ` | ` Type::Integer ` | ` ìnteger ` |
224
+ | Decimal | ` float ` | ` Type::Decimal ` | ` decimal ` |
225
+ | String | ` string ` | ` Type::String ` | ` string ` |
226
+ | Boolean | ` bool ` | ` Type::Boolean ` | ` boolean ` |
227
+ | Token | class ` Token ` | ` Type::Token ` | ` token ` |
228
+ | Byte Sequence | class ` ByteSequence ` | ` Type::ByteSequence ` | ` bytesequence ` |
229
+ | Date (* ) | class ` DateTimeImmutable ` | ` Type::Date ` | ` date ` |
230
+ | DisplayString (* ) | class ` DisplayString ` | ` Type::DisplayString ` | ` displaystring ` |
229
231
230
232
> [ !NOTE]
231
233
> The ` Date ` and ` DisplayString ` type are not yet part of any accepted
232
234
> RFC. But they are already added as new types in the superseeding
233
235
> RFC proposal.
234
236
>
235
237
> See https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-sfbis
236
- > for more information,
238
+ > for more information.
237
239
238
240
The Enum ` Type ` which list all available types can be used to determine the RFC type
239
241
corresponding to a PHP structure using the ` Type::fromVariable ` static method.
240
- The method will throw if the structure is not recognized Alternatively it is possible
241
- to use the ` Type::tryFromValue ` which will instead return ` null ` on unindentified type.
242
- On success both methods returns the corresponding enum ` Type ` .
242
+ The method will throw if the structure is not recognized. Alternatively
243
+ it is possible to use the ` Type::tryFromVariable ` which will instead
244
+ return ` null ` on unidentified type. On success both methods
245
+ return the corresponding enum ` Type ` .
243
246
244
247
``` php
245
248
use Bakame\Http\StructuredFields\Type;
246
249
247
250
echo Type::fromVariable(42)->value; // returns 'integer'
248
251
echo Type::fromVariable(42.0)->name; // returns 'Decimal'
249
- echo Type::fromVariable(new SplTempFileObject()); // throws InvalidArgument
252
+ echo Type::fromVariable(new SplTempFileObject()); // throws InvalidArgument
250
253
echo Type::tryFromValue(new SplTempFileObject()); // returns null
251
254
```
252
255
@@ -268,8 +271,9 @@ PHP default type system, for them, we have defined three classes `Token`,
268
271
` ByteSequence ` and ` DisplayString ` to help with their representation.
269
272
270
273
``` php
271
- use Bakame\Http\StructuredFields\Token;
272
274
use Bakame\Http\StructuredFields\ByteSequence;
275
+ use Bakame\Http\StructuredFields\DisplayString;
276
+ use Bakame\Http\StructuredFields\Token;
273
277
274
278
Token::fromString(string|Stringable $value): Token
275
279
ByteSequence::fromDecoded(string|Stringable $value): ByteSequence;
@@ -308,7 +312,7 @@ $displayString->type(); // returns Type::DisplayString
308
312
309
313
> [ !WARNING]
310
314
> The classes DO NOT expose the ` Stringable ` interface to distinguish them
311
- from a string or a string like object
315
+ > from a regular string or a string like object
312
316
313
317
#### Item
314
318
@@ -337,7 +341,7 @@ $container->hasMembers(): bool;
337
341
$container->hasNoMembers(): bool;
338
342
```
339
343
340
- > [ !NOTE ]
344
+ > [ !IMPORTANT ]
341
345
> The ` get ` method will throw an ` InvalidOffset ` exception if no member exists for the given ` $offset ` .
342
346
343
347
To avoid invalid states, ` ArrayAccess ` modifying methods throw a ` ForbiddenOperation `
@@ -362,7 +366,7 @@ $container->pair(int $offset): array{0:string, 1:StructuredField};
362
366
$container->toPairs(): iterable<array {0:string, 1:StructuredField} >;
363
367
```
364
368
365
- > [ !NOTE ]
369
+ > [ !IMPORTANT ]
366
370
> The ` pair ` method will throw an ` InvalidOffset ` exception if no member exists for the given ` $offset ` .
367
371
368
372
#### Accessing the parameters values
@@ -377,7 +381,7 @@ use Bakame\Http\StructuredFields\Parameters;
377
381
378
382
$field->parameter(string $key): ByteSequence|Token|DisplayString|DateTimeImmutable|Stringable|string|int|float|bool|null;
379
383
$field->parameters(): Parameters;
380
- $field->parameterByIndex(int $index): array{0:string, 1:ByteSequence|Token|DisplayString|DateTimeImmutable|Stringable|string|int|float|boo}|array{}
384
+ $field->parameterByIndex(int $index): array{0:string, 1:ByteSequence|Token|DisplayString|DateTimeImmutable|Stringable|string|int|float|boo}
381
385
InnerList::toPair(): array{0:list<Item >, 1:Parameters}>};
382
386
Item::toPair(): array{0:ByteSequence|Token|DisplayString|DateTimeImmutable|Stringable|string|int|float|bool, 1:Parameters}>};
383
387
```
0 commit comments