Skip to content

Commit 26a3b07

Browse files
committed
Improve public API
1 parent 0b6d795 commit 26a3b07

28 files changed

+145
-138
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
66

77
### Added
88

9+
- `Byte` class replace the `ByteSequence` class.
910
- `Ietf` enum.
1011
- methods `fromRFC9651`, `fromRfc8941`, `toRFC9651`, `toRfc8941`, to ease parsing/serializing Structured Fields.
1112
- method `equals` to allow comparison of Structured Fields values.
@@ -44,6 +45,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
4445
- `InnerList:remove` replaced by `InnerList:removeByIndices`
4546
- `::hasNoMember` and `::hasMembers` methods have been replaced by `isEmpty` and `isNotEmpty` on containers
4647
- `Dictionary::toPairs` and `Parameters::toPairs`
48+
- `ByteSequence` class replaced by `Byte` class
4749

4850
## [1.3.0](https://github.com/bakame-php/http-structured-fields/compare/1.2.2...1.3.0) - 2024-01-05
4951

docs/03-value-types.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ PHP default type system, for them, we have defined three classes `Token`,
7373
`ByteSequence` and `DisplayString` to help with their representation.
7474

7575
```php
76-
use Bakame\Http\StructuredFields\ByteSequence;
76+
use Bakame\Http\StructuredFields\Byte;
7777
use Bakame\Http\StructuredFields\DisplayString;
7878
use Bakame\Http\StructuredFields\Token;
7979

8080
Token::fromString(string|Stringable $value): Token
81-
ByteSequence::fromDecoded(string|Stringable $value): ByteSequence;
82-
ByteSequence::fromEncoded(string|Stringable $value): ByteSequence;
81+
Byte::fromDecoded(string|Stringable $value): ByteSequence;
82+
Byte::fromEncoded(string|Stringable $value): ByteSequence;
8383
DisplayString::fromDecoded(string|Stringable $value): DisplayString;
8484
DisplayString::fromEncoded(string|Stringable $value): DisplayString;
8585
```
@@ -89,7 +89,7 @@ instantiated. To access their value, they expose the following API:
8989

9090
```php
9191
use Bakame\Http\StructuredFields\Token;
92-
use Bakame\Http\StructuredFields\ByteSequence;
92+
use Bakame\Http\StructuredFields\Byte;
9393
use Bakame\Http\StructuredFields\DisplayString;
9494

9595
$token = Token::fromString('application/text+xml');
@@ -99,13 +99,13 @@ $displayString = DisplayString::fromDecoded('füü');
9999
$displayString->decoded(); // returns 'füü'
100100
$displayString->encoded(); // returns 'f%c3%bc%c3%bc'
101101

102-
$byte = ByteSequence::fromDecoded('Hello world!');
102+
$byte = Byte::fromDecoded('Hello world!');
103103
$byte->decoded(); // returns 'Hello world!'
104104
$byte->encoded(); // returns 'SGVsbG8gd29ybGQh'
105105

106106
$token->equals($byte); // will return false;
107107
$displayString->equals($byte); // will return false;
108-
$byte->equals(ByteSequence::fromEncoded('SGVsbG8gd29ybGQh')); // will return true
108+
$byte->equals(Byte::fromEncoded('SGVsbG8gd29ybGQh')); // will return true
109109
$displayString->equals(DisplayString::fromEncoded('f%c3%bc%c3%bc')); // will return true
110110

111111
$token->type(); // returns Type::Token

docs/04-item.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The `Item` value object exposes the following named constructors to instantiate
2424
bare items (ie: item without parameters attached to them).
2525

2626
```php
27-
use Bakame\Http\StructuredFields\ByteSequence;
27+
use Bakame\Http\StructuredFields\Byte;
2828
use Bakame\Http\StructuredFields\Item;
2929
use Bakame\Http\StructuredFields\Token;
3030

docs/05-containers.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ order: 6
55

66
# Containers
77

8-
While building or updating a Bare Item is straightforward, doing the same with the structured field
8+
While building or updating a Bare Item is straightforward, doing the same with the structured field containers
99
requires a bit more logic. In the following sections we will explore how we can access, build and update
1010
containers.
1111

@@ -274,10 +274,10 @@ which takes a single variadic parameter `$members`:
274274

275275
```php
276276
use Bakame\Http\StructuredFields\InnerList;
277-
use Bakame\Http\StructuredFields\ByteSequence;
277+
use Bakame\Http\StructuredFields\Byte;
278278

279279
$list = InnerList::new(
280-
ByteSequence::fromDecoded('Hello World'),
280+
Byte::fromDecoded('Hello World'),
281281
42.0,
282282
42
283283
);
@@ -300,14 +300,14 @@ $list->removeByIndices(int ...$index): static;
300300
as shown below
301301

302302
```php
303-
use Bakame\Http\StructuredFields\ByteSequence;
303+
use Bakame\Http\StructuredFields\Byte;
304304
use Bakame\Http\StructuredFields\InnerList;
305305

306306
$list = InnerList::new()
307307
->unshift('42')
308308
->push(42)
309309
->insert(1, 42.0)
310-
->replace(0, ByteSequence::fromDecoded('Hello World'));
310+
->replace(0, Byte::fromDecoded('Hello World'));
311311

312312
echo $list->toHttpValue(); //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
313313
echo $list; //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
@@ -347,7 +347,7 @@ To ease working with instances that have a `Parameters` object attached to, the
347347
methods are added:
348348

349349
```php
350-
use Bakame\Http\StructuredFields\ByteSequence;
350+
use Bakame\Http\StructuredFields\Byte;
351351
use Bakame\Http\StructuredFields\InnerList;
352352
use Bakame\Http\StructuredFields\Item;
353353
use Bakame\Http\StructuredFields\Token;

src/ByteSequence.php renamed to src/Byte.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.3.5
1616
*/
17-
final class ByteSequence
17+
final class Byte
1818
{
1919
private function __construct(
2020
private readonly string $value

src/Dictionary.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Iterator;
1313
use IteratorAggregate;
1414
use Stringable;
15+
use Throwable;
1516

1617
use function array_key_exists;
1718
use function array_keys;
@@ -80,7 +81,7 @@ public static function new(): self
8081
/**
8182
* Returns a new instance from an associative iterable construct.
8283
*
83-
* its keys represent the dictionary entry key
84+
* its keys represent the dictionary entry name
8485
* its values represent the dictionary entry value
8586
*
8687
* @param StructuredFieldProvider|iterable<string, InnerList|Item|SfMemberInput> $members
@@ -102,12 +103,12 @@ public static function fromAssociative(StructuredFieldProvider|iterable $members
102103
* Returns a new instance from a pair iterable construct.
103104
*
104105
* Each member is composed of an array with two elements
105-
* the first member represents the instance entry key
106+
* the first member represents the instance entry name
106107
* the second member represents the instance entry value
107108
*
108109
* @param StructuredFieldProvider|Dictionary|Parameters|iterable<array{0:string, 1?:InnerList|Item|SfMemberInput}> $pairs
109110
*/
110-
public static function fromPairs(StructuredFieldProvider|iterable $pairs): self
111+
public static function fromPairs(StructuredFieldProvider|Dictionary|Parameters|iterable $pairs): self
111112
{
112113
if ($pairs instanceof StructuredFieldProvider) {
113114
$pairs = $pairs->toStructuredField();
@@ -163,7 +164,7 @@ public static function fromPairs(StructuredFieldProvider|iterable $pairs): self
163164
*
164165
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.2
165166
*
166-
* @throws SyntaxError If the string is not a valid
167+
* @throws StructuredFieldError|Throwable
167168
*/
168169
public static function fromRfc9651(Stringable|string $httpValue): self
169170
{
@@ -175,7 +176,7 @@ public static function fromRfc9651(Stringable|string $httpValue): self
175176
*
176177
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.2
177178
*
178-
* @throws SyntaxError If the string is not a valid
179+
* @throws StructuredFieldError|Throwable
179180
*/
180181
public static function fromRfc8941(Stringable|string $httpValue): self
181182
{
@@ -187,7 +188,7 @@ public static function fromRfc8941(Stringable|string $httpValue): self
187188
*
188189
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.2
189190
*
190-
* @throws SyntaxError If the string is not a valid
191+
* @throws StructuredFieldError|Throwable If the string is not a valid
191192
*/
192193
public static function fromHttpValue(Stringable|string $httpValue, ?Ietf $rfc = null): self
193194
{
@@ -380,7 +381,7 @@ private function filterIndex(int $index, int|null $max = null): int|null
380381
}
381382

382383
/**
383-
* Returns the item or the inner-list and its key as attached to the given
384+
* Returns the item or the inner-list and its name as attached to the given
384385
* collection according to their index position otherwise throw.
385386
*
386387
* @param ?callable(Item|InnerList, string): (bool|string) $validate
@@ -418,7 +419,7 @@ public function getByIndex(int $index, ?callable $validate = null): array
418419
}
419420

420421
/**
421-
* Returns the key associated with the given index or null otherwise.
422+
* Returns the name associated with the given index or null otherwise.
422423
*/
423424
public function indexByName(string $name): ?int
424425
{
@@ -451,7 +452,7 @@ public function nameByIndex(int $index): ?string
451452
}
452453

453454
/**
454-
* Returns the first member whether it is an item or an inner-list and its key as attached to the given
455+
* Returns the first member whether it is an item or an inner-list and its name as attached to the given
455456
* collection according to their index position otherwise returns an empty array.
456457
*
457458
* @return array{0:string, 1:InnerList|Item}|array{}
@@ -460,13 +461,13 @@ public function first(): array
460461
{
461462
try {
462463
return $this->getByIndex(0);
463-
} catch (InvalidOffset) {
464+
} catch (StructuredFieldError) {
464465
return [];
465466
}
466467
}
467468

468469
/**
469-
* Returns the first member whether it is an item or an inner-list and its key as attached to the given
470+
* Returns the first member whether it is an item or an inner-list and its name as attached to the given
470471
* collection according to their index position otherwise returns an empty array.
471472
*
472473
* @return array{0:string, 1:InnerList|Item}|array{}
@@ -475,24 +476,24 @@ public function last(): array
475476
{
476477
try {
477478
return $this->getByIndex(-1);
478-
} catch (InvalidOffset) {
479+
} catch (StructuredFieldError) {
479480
return [];
480481
}
481482
}
482483

483484
/**
484-
* Adds a member at the end of the instance otherwise updates the value associated with the key if already present.
485+
* Adds a member at the end of the instance otherwise updates the value associated with the name if already present.
485486
*
486487
* This method MUST retain the state of the current instance, and return
487488
* an instance that contains the specified changes.
488489
*
489490
* @param InnerList|Item|SfMemberInput|null $member
490491
*
491-
* @throws SyntaxError If the string key is not a valid
492+
* @throws SyntaxError If the string name is not a valid
492493
*/
493494
public function add(
494495
string $name,
495-
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
496+
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Byte|DisplayString|DateTimeInterface|string|int|float|bool|null $member
496497
): self {
497498
if (null === $member) {
498499
return $this;
@@ -561,7 +562,7 @@ public function removeByIndices(int ...$indices): self
561562
}
562563

563564
/**
564-
* Deletes members associated with the list using the member key.
565+
* Deletes members associated with the list using the member name.
565566
*
566567
* This method MUST retain the state of the current instance, and return
567568
* an instance that contains the specified changes.
@@ -572,17 +573,17 @@ public function removeByNames(string ...$names): self
572573
}
573574

574575
/**
575-
* Adds a member at the end of the instance and deletes any previous reference to the key if present.
576+
* Adds a member at the end of the instance and deletes any previous reference to the name if present.
576577
*
577578
* This method MUST retain the state of the current instance, and return
578579
* an instance that contains the specified changes.
579580
*
580581
* @param InnerList|Item|SfMemberInput|null $member
581-
* @throws SyntaxError If the string key is not a valid
582+
* @throws SyntaxError If the string name is not a valid
582583
*/
583584
public function append(
584585
string $name,
585-
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
586+
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Byte|DisplayString|DateTimeInterface|string|int|float|bool|null $member
586587
): self {
587588
if (null === $member) {
588589
return $this;
@@ -594,18 +595,18 @@ public function append(
594595
}
595596

596597
/**
597-
* Adds a member at the beginning of the instance and deletes any previous reference to the key if present.
598+
* Adds a member at the beginning of the instance and deletes any previous reference to the name if present.
598599
*
599600
* This method MUST retain the state of the current instance, and return
600601
* an instance that contains the specified changes.
601602
*
602603
* @param InnerList|Item|SfMemberInput|null $member
603604
*
604-
* @throws SyntaxError If the string key is not a valid
605+
* @throws SyntaxError If the string name is not a valid
605606
*/
606607
public function prepend(
607608
string $name,
608-
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
609+
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Byte|DisplayString|DateTimeInterface|string|int|float|bool|null $member
609610
): self {
610611
if (null === $member) {
611612
return $this;
@@ -763,6 +764,8 @@ public function offsetExists(mixed $offset): bool
763764

764765
/**
765766
* @param string $offset
767+
*
768+
* @throws StructuredFieldError
766769
*/
767770
public function offsetGet(mixed $offset): InnerList|Item
768771
{
@@ -790,7 +793,7 @@ public function offsetSet(mixed $offset, mixed $value): void
790793
*/
791794
public function map(callable $callback): Iterator
792795
{
793-
foreach ($this->getIterator() as $offset => $member) {
796+
foreach ($this as $offset => $member) {
794797
yield ($callback)($member, $offset);
795798
}
796799
}
@@ -805,7 +808,7 @@ public function map(callable $callback): Iterator
805808
*/
806809
public function reduce(callable $callback, mixed $initial = null): mixed
807810
{
808-
foreach ($this->getIterator() as $offset => $pair) {
811+
foreach ($this as $offset => $pair) {
809812
$initial = $callback($initial, $pair, $offset);
810813
}
811814

@@ -819,7 +822,7 @@ public function reduce(callable $callback, mixed $initial = null): mixed
819822
*/
820823
public function filter(callable $callback): self
821824
{
822-
return self::fromPairs(new CallbackFilterIterator($this->getIterator(), $callback));
825+
return self::fromPairs(new CallbackFilterIterator($this, $callback));
823826
}
824827

825828
/**
@@ -829,7 +832,7 @@ public function filter(callable $callback): self
829832
*/
830833
public function sort(callable $callback): self
831834
{
832-
$members = iterator_to_array($this->getIterator());
835+
$members = iterator_to_array($this);
833836
usort($members, $callback);
834837

835838
return self::fromPairs($members);

src/Ietf.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ public function uri(): string
2222

2323
public function publishedAt(): DateTimeImmutable
2424
{
25-
return match ($this) {
26-
self::Rfc9651 => new DateTimeImmutable('2024-09-01', new DateTimeZone('UTC')),
27-
self::Rfc8941 => new DateTimeImmutable('2021-02-01', new DateTimeZone('UTC')),
28-
};
25+
return new DateTimeImmutable(match ($this) {
26+
self::Rfc9651 => '2024-09-01',
27+
self::Rfc8941 => '2021-02-01',
28+
}, new DateTimeZone('UTC'));
29+
}
30+
31+
public function isActive(): bool
32+
{
33+
return self::Rfc9651 === $this;
2934
}
3035

3136
public function isObsolete(): bool
3237
{
33-
return match ($this) {
34-
self::Rfc9651 => false,
35-
default => true,
36-
};
38+
return !$this->isActive();
3739
}
3840

3941
public function supports(mixed $value): bool

0 commit comments

Comments
 (0)