@@ -27,7 +27,7 @@ composer require bakame/http-structured-fields
27
27
### Parsing and Serializing Structured Fields
28
28
29
29
Once the library is installed parsing the header value is done via the normalized ` fromHttpValue ` named
30
- constructor attached to all library's value objects as shown below:
30
+ constructor attached to library's structured fields representation as shown below:
31
31
32
32
``` php
33
33
@@ -41,7 +41,7 @@ use Bakame\Http\StructuredFields\Item;
41
41
// via any given framework or package or super global.
42
42
// We are using a PSR-7 Request object in this example
43
43
44
- $headerLine = $request->getHeaderLine('foo'); // 'foo: bar;baz=42' the raw header line
44
+ $headerLine = $request->getHeaderLine('foo'); // 'foo: bar;baz=42' the raw header line is a structured field item
45
45
$field = Item::fromHttpValue($headerLine);
46
46
$field->value(); // returns Token::fromString('bar); the found token value
47
47
$field->parameter('baz'); // returns 42; the value of the parameter or null if the parameter is not defined.
@@ -102,8 +102,8 @@ The table below summarizes the item value type.
102
102
| Date | class ` DateTimeImmutable ` | ` Type::Date ` |
103
103
104
104
As shown in the table, the RFC define two (2) specific data types that can not be represented by
105
- PHP default type system, for them, we define two classes ` Token ` and ` ByteSequence ` to help
106
- representing them in our code .
105
+ PHP default type system, for them, we have defined two classes ` Token ` and ` ByteSequence ` to help
106
+ with represention .
107
107
108
108
``` php
109
109
use Bakame\Http\StructuredFields\Token;
@@ -130,14 +130,17 @@ $byte->encoded(); // returns 'SGVsbG8gd29ybGQh'
130
130
131
131
$token->equals($byte); // will return false;
132
132
$byte->equals(ByteSequence::fromEncoded('SGVsbG8gd29ybGQh')); // will return true
133
+
134
+ $token->type(); // returns Type::Token enum
135
+ $byte->type(); // returns Type::ByteSequence
133
136
```
134
137
135
138
** Both classes DO NOT expose the ` Stringable ` interface to distinguish them
136
139
from a string or a string like object**
137
140
138
141
#### Item
139
142
140
- The defined types are all attached to the ` Item ` object where there values and type
143
+ The defined types are all attached to the ` Item ` object where there values and types
141
144
are accessible using the following methods:
142
145
143
146
``` php
@@ -148,7 +151,7 @@ use Bakame\Http\StructuredFields\Type;
148
151
// the Item::value() can return one of those type
149
152
$item = Item::from(CarbonImmutable::parse('today'));
150
153
$item->type(); // return Type::Date;
151
- $item->value() // return CarbonImmutable::parse('today') ( because it extends DateTimeImmutable)
154
+ $item->value() // return a CarbonImmutable instance because it extends DateTimeImmutable
152
155
// you can also do
153
156
Type::Date->equals($item); // returns true
154
157
```
@@ -189,7 +192,7 @@ $container->pair(int $offset): array{0:string, 1:StructuredField};
189
192
$container->toPairs(): iterable<array {0:string, 1:StructuredField} >;
190
193
```
191
194
192
- #### Accessing the paramters values
195
+ #### Accessing the parameters values
193
196
194
197
You can also read the associated ` Parameters ` instance attached to an ` InnerList ` or a ` Item ` instances
195
198
using the following methods:
@@ -205,35 +208,10 @@ $innerList->parameters(): Parameters;
205
208
206
209
Every value object can be used as a builder to create an HTTP field value.
207
210
208
- The ` Item ` value object exposes lots of named constructors.
211
+ #### Items value
209
212
210
- ``` php
211
- use Bakame\Http\StructuredFields\ByteSequence;
212
- use Bakame\Http\StructuredFields\Item;
213
-
214
- $item = Item::from("hello world", [
215
- "a" => Item::fromDecodedByteSequence("Hello World")
216
- ]);
217
- $item->value(); // returns "hello world"
218
- $item->type(); // returns Type::String
219
- $item->parameter("a"); // returns ByteSequence::fromDecoded('Hello World');
220
- echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
221
- ```
222
-
223
- ** Of note: to instantiate a decimal number type a float MUST be used as the first argument of ` Item::from ` .**
224
-
225
- ``` php
226
- use Bakame\Http\StructuredFields\Item;
227
-
228
- $decimal = Item::from(42.0);
229
- $decimal->type(); //Type::Decimal
230
-
231
- $integer = Item::fromPair([42]);
232
- $integer->type(); //return Type::Integer
233
- ```
234
-
235
- Here's the complete list of named constructors attached to the ` Item ` object.
236
- The ` Item::from ` method expects an associative iterable to represents the parameters.
213
+ The ` Item ` value object exposes lots of named constructors to construct
214
+ bare items (ie: item without parameters attached to them).
237
215
238
216
``` php
239
217
use Bakame\Http\StructuredFields\Item;
@@ -243,22 +221,24 @@ Item::fromEncodedByteSequence(string $value): self;
243
221
Item::fromToken(string $value): self;
244
222
Item::fromTimestamp(int $value): self;
245
223
Item::fromDateFormat(string $format, string $datetime): self;
224
+ Item::fromDateString(string $datetime, DateTimeZone|string|null $timezone = null): self;
225
+ Item::fromDate(DateTimeInterface $datetime): self;
246
226
Item::fromDecimal(int|float $value): self;
247
227
Item::fromInteger(int|float $value): self;
248
228
Item::true(): self;
249
229
Item::false(): self;
250
230
```
251
231
252
- It is possible to update a ` Item ` object using the following modifying method:
232
+ To update an ` Item ` object value, the ` Item::withValue ` method should be use :
253
233
254
234
``` php
255
235
use Bakame\Http\StructuredFields\Item;
256
- use Bakame\Http\StructuredFields\ValueAccess;
257
- use Bakame\Http\StructuredFields\Parameters;
258
236
259
237
Item::withValue(SfItemInput $value): static
260
238
```
261
239
240
+ ### Dictionaries
241
+
262
242
The ` Dictionary ` and ` Parameters ` instances can be build with an associative iterable structure as shown below
263
243
264
244
``` php
@@ -337,6 +317,7 @@ $list = InnerList::from(
337
317
echo $list->toHttpValue(); //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
338
318
echo $list; //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
339
319
```
320
+
340
321
Once again, builder methods exist on both classes to ease container construction.
341
322
342
323
``` php
@@ -363,26 +344,10 @@ $list->replace($key, $member): static;
363
344
$list->remove(...$key): static;
364
345
```
365
346
366
- # Adding and updating parameters
367
-
368
- Apart from the ` Item::from ` named constructor, you can initialize a new Item instance using pairs
369
- as defined in the RFC:
370
-
371
- ``` php
372
- use Bakame\Http\StructuredFields\ByteSequence;
373
- use Bakame\Http\StructuredFields\Item;
374
-
375
- $item = Item::fromPair(["hello world", [
376
- ["a", Item::from(ByteSequence::fromDecoded("Hello World"))],
377
- ]]);
378
- $item->value(); // returns "hello world"
379
- $item->type(); // returns Type::String
380
- $item->parameter("a"); // returns ByteSequence::fromDecoded('Hello World');
381
- echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
382
- ```
347
+ #### Adding and updating parameters
383
348
384
- It is also possible to instantiate an ` InnerList ` instance with included parameters
385
- using one of those two additional named constructors:
349
+ It is also possible to instantiate an ` InnerList ` or an ` Item ` instance with included parameters
350
+ using one of these named constructors:
386
351
387
352
``` php
388
353
use Bakame\Http\StructuredFields\InnerList;
@@ -408,7 +373,7 @@ InnerList::toPair(): array{0:list<Item>, 1:Parameters}>};
408
373
Item::toPair(): array{0:mixed, 1:Parameters}>};
409
374
```
410
375
411
- The ` InnerList ` and ` Item ` object provide additional modifying methods to help deal with parameters.
376
+ The ` InnerList ` and ` Item ` object provide additional modifying methods to help deal with parameters.
412
377
You can attach and update the associated ` Parameters ` instance using the following methods:
413
378
414
379
``` php
0 commit comments