Skip to content

Commit e8c8724

Browse files
committed
Improve Token API
1 parent 9f39429 commit e8c8724

File tree

4 files changed

+60
-58
lines changed

4 files changed

+60
-58
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1212
- `Item` implements the `ValueAccess` interface;
1313
- `Item::toPair` to complement `Item::fromPair`;
1414
- `Value` internal class to improve Item public API;
15+
- `Token::toString` to return the string representation of the token.
1516
- Adding support for `MapKey` object to access container members.
1617

1718
### Fixed
@@ -27,6 +28,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
2728
- **[BC Break]** `InnerList::fromPairParameters` use `InnerList::fromPairs` instead.
2829
- **[BC Break]** `InnerList::fromAssociativeParameters` use `InnerList::fromAssociative` instead.
2930
- **[BC Break]** `Value` interface use a combination of `ValueAccess` **and** `ParameterAccess` instead.
31+
- **[BC Break]** `Token::value` is no longer public use `Token::toString` instead.
3032

3133
## [0.8.0] - 2023-03-12
3234

README.md

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ ByteSequence::fromDecoded(string|Stringable $value): ByteSequence;
114114
ByteSequence::fromEncoded(string|Stringable $value): ByteSequence;
115115
```
116116

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.
118118
To access their value, they expose the following API:
119119

120120
```php
121121
use Bakame\Http\StructuredFields\Token;
122122
use Bakame\Http\StructuredFields\ByteSequence;
123123

124124
$token = Token::fromString('application/text+xml');
125-
echo $token->value; // returns 'application/text+xml'
125+
echo $token->toString(); // returns 'application/text+xml'
126126

127127
$byte = ByteSequence::fromDecoded('Hello world!');
128128
$byte->decoded(); // returns 'Hello world!'
@@ -150,17 +150,7 @@ $item = Item::from(CarbonImmutable::parse('today'));
150150
$item->type(); // return Type::Date;
151151
$item->value() // return CarbonImmutable::parse('today') (because it extends DateTimeImmutable)
152152
// 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
164154
```
165155

166156
#### Containers
@@ -199,7 +189,9 @@ $container->pair(int $offset): array{0:string, 1:StructuredField};
199189
$container->toPairs(): iterable<array{0:string, 1:StructuredField}>;
200190
```
201191

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
203195
using the following methods:
204196

205197
```php
@@ -219,21 +211,6 @@ The `Item` value object exposes lots of named constructors.
219211
use Bakame\Http\StructuredFields\ByteSequence;
220212
use Bakame\Http\StructuredFields\Item;
221213

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-
237214
$item = Item::from("hello world", [
238215
"a" => Item::fromDecodedByteSequence("Hello World")
239216
]);
@@ -261,10 +238,6 @@ The `Item::from` method expects an associative iterable to represents the parame
261238
```php
262239
use Bakame\Http\StructuredFields\Item;
263240

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;
268241
Item::fromDecodedByteSequence(string $value): self;
269242
Item::fromEncodedByteSequence(string $value): self;
270243
Item::fromToken(string $value): self;
@@ -286,21 +259,6 @@ use Bakame\Http\StructuredFields\Parameters;
286259
Item::withValue(SfItemInput $value): static
287260
```
288261

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-
304262
The `Dictionary` and `Parameters` instances can be build with an associative iterable structure as shown below
305263

306264
```php
@@ -405,28 +363,65 @@ $list->replace($key, $member): static;
405363
$list->remove(...$key): static;
406364
```
407365

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:
410370

411371
```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=:
418382
```
419383

420384
It is also possible to instantiate an `InnerList` instance with included parameters
421385
using one of those two additional named constructors:
422386

423387
```php
424388
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;
425395

426396
InnerList::fromAssociative(iterable<string, SfItemInput> $parameters, ...$members): self;
427397
InnerList::fromPair(array{0:list<Item>, iterable<array{0:string, 1:SfItemInput}>} $pair): self;
428398
```
429399

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+
430425
## Contributing
431426

432427
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.

src/Token.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212
*/
1313
final class Token
1414
{
15-
private function __construct(public readonly string $value)
15+
private function __construct(private readonly string $value)
1616
{
1717
if (1 !== preg_match("/^([a-z*][a-z\d:\/!#\$%&'*+\-.^_`|~]*)$/i", $this->value)) {
1818
throw new SyntaxError('Invalid characters in token.');
1919
}
2020
}
2121

22+
public function toString(): string
23+
{
24+
return $this->value;
25+
}
26+
2227
public static function fromString(Stringable|string $value): self
2328
{
2429
return new self((string) $value);

src/Value.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function serialize(): string
194194
is_int($this->value) => (string) $this->value,
195195
is_float($this->value) => self::serializeDecimal($this->value),
196196
is_bool($this->value) => '?'.($this->value ? '1' : '0'),
197-
$this->value instanceof Token => $this->value->value,
197+
$this->value instanceof Token => $this->value->toString(),
198198
$this->value instanceof DateTimeImmutable => '@'.$this->value->getTimestamp(),
199199
default => ':'.$this->value->encoded().':',
200200
};

0 commit comments

Comments
 (0)