@@ -114,15 +114,15 @@ ByteSequence::fromDecoded(string|Stringable $value): ByteSequence;
114
114
ByteSequence::fromEncoded(string|Stringable $value): ByteSequence;
115
115
```
116
116
117
- Both class are final and immutable and their value can not be modified once instantiated.
117
+ Both classes are final and immutable; their value can not be modified once instantiated.
118
118
To access their value, they expose the following API:
119
119
120
120
``` php
121
121
use Bakame\Http\StructuredFields\Token;
122
122
use Bakame\Http\StructuredFields\ByteSequence;
123
123
124
124
$token = Token::fromString('application/text+xml');
125
- echo $token->value ; // returns 'application/text+xml'
125
+ echo $token->toString() ; // returns 'application/text+xml'
126
126
127
127
$byte = ByteSequence::fromDecoded('Hello world!');
128
128
$byte->decoded(); // returns 'Hello world!'
@@ -150,17 +150,7 @@ $item = Item::from(CarbonImmutable::parse('today'));
150
150
$item->type(); // return Type::Date;
151
151
$item->value() // return CarbonImmutable::parse('today') (because it extends DateTimeImmutable)
152
152
// you can also do
153
- Type::Date->equals($item->type()); // returns true
154
- ```
155
-
156
- You can also read the associated ` Parameters ` instance attached to an ` Item ` instance
157
- using the following methods:
158
-
159
- ``` php
160
- use Bakame\Http\StructuredFields\Parameters;
161
-
162
- $item->parameter($key): ByteSequence|Token|DateTimeImmutable|Stringable|string|int|float|bool|null;
163
- $item->parameters(): Parameters;
153
+ Type::Date->equals($item); // returns true
164
154
```
165
155
166
156
#### Containers
@@ -199,7 +189,9 @@ $container->pair(int $offset): array{0:string, 1:StructuredField};
199
189
$container->toPairs(): iterable<array {0:string, 1:StructuredField} >;
200
190
```
201
191
202
- You can also read the associated ` Parameters ` instance attached to an ` InnerList ` instance
192
+ #### Accessing the paramters values
193
+
194
+ You can also read the associated ` Parameters ` instance attached to an ` InnerList ` or a ` Item ` instances
203
195
using the following methods:
204
196
205
197
``` php
@@ -219,21 +211,6 @@ The `Item` value object exposes lots of named constructors.
219
211
use Bakame\Http\StructuredFields\ByteSequence;
220
212
use Bakame\Http\StructuredFields\Item;
221
213
222
- $item = Item::fromPair(["hello world", [
223
- ["a", Item::from(ByteSequence::fromDecoded("Hello World"))],
224
- ]]);
225
- $item->value(); // returns "hello world"
226
- $item->type(); // returns Type::String
227
- $item->parameter("a"); // returns ByteSequence::fromDecoded('Hello World');
228
- echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
229
- ```
230
-
231
- Once again it is possible to simplify this code using the following technique:
232
-
233
- ``` php
234
- use Bakame\Http\StructuredFields\ByteSequence;
235
- use Bakame\Http\StructuredFields\Item;
236
-
237
214
$item = Item::from("hello world", [
238
215
"a" => Item::fromDecodedByteSequence("Hello World")
239
216
]);
@@ -261,10 +238,6 @@ The `Item::from` method expects an associative iterable to represents the parame
261
238
``` php
262
239
use Bakame\Http\StructuredFields\Item;
263
240
264
- //@type SfItemInput ByteSequence|Token|DateTimeInterface|Stringable|string|int|float|bool
265
-
266
- Item::from(SfItemInput $value, iterable<string , SfItemInput > $associativeParameters = []): self;
267
- Item::fromPair(array{0:SfItemInput, 1:iterable<array {0:string, 1:SfItemInput} >} $pair): self;
268
241
Item::fromDecodedByteSequence(string $value): self;
269
242
Item::fromEncodedByteSequence(string $value): self;
270
243
Item::fromToken(string $value): self;
@@ -286,21 +259,6 @@ use Bakame\Http\StructuredFields\Parameters;
286
259
Item::withValue(SfItemInput $value): static
287
260
```
288
261
289
- And just like with the ` InnerList ` instance the ` Item ` object provides additional modifying methods
290
- to help deal with parameters. You can attach and update the associated ` Parameters ` instance using the
291
- following methods:
292
-
293
- ``` php
294
- use Bakame\Http\StructuredFields\Parameters;
295
-
296
- $item->addParameter($key, $value): static;
297
- $item->appendParameter($key, $value): static;
298
- $item->prependParameter($key, $value): static;
299
- $item->withoutParameters(...$keys): static;
300
- $item->withoutAnyParameter(): static;
301
- $item->withParameters(Parameters $parameters): static;
302
- ```
303
-
304
262
The ` Dictionary ` and ` Parameters ` instances can be build with an associative iterable structure as shown below
305
263
306
264
``` php
@@ -405,28 +363,65 @@ $list->replace($key, $member): static;
405
363
$list->remove(...$key): static;
406
364
```
407
365
408
- On ` InnerList ` instances it is possible to attach and update a ` Parameters ` instance using the
409
- following methods:
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:
410
370
411
371
``` php
412
- $list->addParameter($key, $value): static;
413
- $list->appendParameter($key, $value): static;
414
- $list->prependParameter($key, $value): static;
415
- $list->withoutParameters(...$keys): static;
416
- $list->withoutAnyParameter(): static;
417
- $list->withParameters(Parameters $parameters): static;
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=:
418
382
```
419
383
420
384
It is also possible to instantiate an ` InnerList ` instance with included parameters
421
385
using one of those two additional named constructors:
422
386
423
387
``` php
424
388
use Bakame\Http\StructuredFields\InnerList;
389
+ use Bakame\Http\StructuredFields\Item;
390
+
391
+ //@type SfItemInput ByteSequence|Token|DateTimeInterface|Stringable|string|int|float|bool
392
+
393
+ Item::from(SfItemInput $value, iterable<string , SfItemInput > $parameters = []): self;
394
+ Item::fromPair(array{0:SfItemInput, 1:iterable<array {0:string, 1:SfItemInput} >} $pair): self;
425
395
426
396
InnerList::fromAssociative(iterable<string , SfItemInput > $parameters, ...$members): self;
427
397
InnerList::fromPair(array{0:list<Item >, iterable<array {0:string, 1:SfItemInput} >} $pair): self;
428
398
```
429
399
400
+ Both classes allow return their respective pair representation via the ` toPair ` method.
401
+
402
+ ``` php
403
+ use Bakame\Http\StructuredFields\InnerList;
404
+ use Bakame\Http\StructuredFields\Item;
405
+ use Bakame\Http\StructuredFields\Parameters;
406
+
407
+ InnerList::toPair(): array{0:list<Item >, 1:Parameters}>};
408
+ Item::toPair(): array{0:mixed, 1:Parameters}>};
409
+ ```
410
+
411
+ The ` InnerList ` and ` Item ` object provide additional modifying methods to help deal with parameters.
412
+ You can attach and update the associated ` Parameters ` instance using the following methods:
413
+
414
+ ``` php
415
+ use Bakame\Http\StructuredFields\Parameters;
416
+
417
+ $field->addParameter(string $key, mixed $value): static;
418
+ $field->appendParameter(string $key, mixed $value): static;
419
+ $field->prependParameter(string $key, mixed $value): static;
420
+ $field->withoutParameters(string ...$keys): static;
421
+ $field->withoutAnyParameter(): static;
422
+ $field->withParameters(Parameters $parameters): static;
423
+ ```
424
+
430
425
## Contributing
431
426
432
427
Contributions are welcome and will be fully credited. Please see [ CONTRIBUTING] ( .github/CONTRIBUTING.md ) and [ CODE OF CONDUCT] ( .github/CODE_OF_CONDUCT.md ) for details.
0 commit comments