Skip to content

Commit 1a55920

Browse files
committed
Update Parameters implementation and add ParameterAccess::clearParameters
1 parent 46812ca commit 1a55920

13 files changed

+66
-57
lines changed

CHANGELOG.md

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

77
### Added
88

9-
- Support for `Stringable` instance added to `Item::from`, the instances will be converted to the string data type.
9+
- Support for `Stringable` instances added to `Item::from`, the instances will be converted to the string data type.
1010
- Support for the upcoming `Date` data type in `Item`. (see https://httpwg.org/http-extensions/draft-ietf-httpbis-sfbis.html)
11-
- Represented as a `DateTimeImmutable` object.
11+
- date type is represented as a `DateTimeImmutable` object.
1212
- `Item::isDate` tells whether the instance represents a `Date` DataType.
13-
- `ParameterAccess` interface updated with 3 new methods to ease parameter members modification.
13+
- `ParameterAccess` interface updated with 4 new methods to ease parameter members modification.
1414
- `Parameter::create` named constructor to create a new instance without any parameter.
1515
- `Dictionnary::create` named constructor to create a new instance without any parameter.
1616

1717
### Fixed
1818

1919
- `Item::fromHttpValue` now internally uses the `Parser` previously it was using its own parsing rules.
20+
- `Parameters::fromHttpValue` now internally uses the `Parser` previously it was using its own parsing rules.
2021
- **[BC Break]** `::fromAssociative`, `::fromList`, `::fromPairs` methods require iterable arguments without default value.
2122
- **[BC Break]** `Item::value` method returns the Item (returns value can be `float|int|string|bool|ByteSequence|DateTimeImmutable|Token`).
2223
- **[BC Break]** `InnerList::parameters` is no longer accessible as a public readonly property.
@@ -27,7 +28,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
2728

2829
### Removed
2930

30-
- None
31+
- **[BC Break]** `ForbiddenStateError` exception is removed, the `InvalidArgument` exception is used instead.
3132

3233
## [0.6.0] - 2022-11-12
3334

src/Dictionary.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public static function fromPairs(MemberOrderedMap|iterable $pairs): self
6868
}
6969

7070
$instance = new self();
71-
foreach ($pairs as [$key, $member]) {
72-
$instance->set($key, $member);
71+
foreach ($pairs as $pair) {
72+
$instance->set(...$pair);
7373
}
7474

7575
return $instance;
@@ -231,10 +231,11 @@ public function set(string $key, StructuredField|Token|ByteSequence|DateTimeInte
231231
return $this;
232232
}
233233

234-
private static function filterMember(InnerList|Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): InnerList|Item
234+
private static function filterMember(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): InnerList|Item
235235
{
236236
return match (true) {
237237
$member instanceof InnerList, $member instanceof Item => $member,
238+
$member instanceof StructuredField => throw new InvalidArgument('Expecting a "'.Item::class.'" or a "'.InnerList::class.'" instance; received a "'.$member::class.'" instead.'),
238239
default => Item::from($member),
239240
};
240241
}
@@ -267,9 +268,7 @@ public function append(string $key, StructuredField|Token|ByteSequence|DateTimeI
267268
{
268269
unset($this->members[$key]);
269270

270-
$this->members[MapKey::fromString($key)->value] = self::filterMember($member);
271-
272-
return $this;
271+
return $this->set($key, $member);
273272
}
274273

275274
/**

src/ForbiddenStateError.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/InnerList.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private function __construct(private readonly Parameters $parameters)
3030
*/
3131
public static function from(Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
3232
{
33-
return self::fromList($members);
33+
return (new self(Parameters::create()))->push(...array_map(self::filterMember(...), $members));
3434
}
3535

3636
/**
@@ -47,10 +47,11 @@ public static function fromList(iterable $members, iterable $parameters = []): s
4747
return $instance;
4848
}
4949

50-
private static function filterMember(Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): Item
50+
private static function filterMember(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): Item
5151
{
5252
return match (true) {
5353
$member instanceof Item => $member,
54+
$member instanceof StructuredField => throw new InvalidArgument('Expecting a "'.Item::class.'" instance; received a "'.$member::class.'" instead.'),
5455
default => Item::from($member),
5556
};
5657
}
@@ -80,6 +81,11 @@ public function withoutParameter(string ...$keys): static
8081
return $this->withParameters($this->parameters()->delete(...$keys));
8182
}
8283

84+
public function clearParameters(): static
85+
{
86+
return $this->withParameters($this->parameters()->clear());
87+
}
88+
8389
public function withParameters(Parameters $parameters): static
8490
{
8591
if ($this->parameters->toHttpValue() === $parameters->toHttpValue()) {

src/InnerListTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,13 @@ public function it_can_create_via_parameters_access_methods_a_new_object(): void
218218
$instance3 = $instance1->prependParameter('a', false);
219219
$instance4 = $instance1->withoutParameter('b');
220220
$instance5 = $instance1->withoutParameter('a');
221+
$instance6 = $instance1->clearParameters();
221222

222223
self::assertSame($instance1, $instance2);
223224
self::assertNotSame($instance1->parameters(), $instance3->parameters());
224225
self::assertEquals(iterator_to_array($instance1), iterator_to_array($instance3));
225226
self::assertSame($instance1, $instance4);
226227
self::assertFalse($instance5->parameters()->hasMembers());
228+
self::assertTrue($instance6->parameters()->hasNoMembers());
227229
}
228230
}

src/InvalidArgument.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bakame\Http\StructuredFields;
6+
7+
use InvalidArgumentException;
8+
9+
final class InvalidArgument extends InvalidArgumentException implements StructuredFieldError
10+
{
11+
}

src/Item.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ public function appendParameter(string $key, Item|Token|ByteSequence|DateTimeInt
214214
return $this->withParameters($this->parameters()->append($key, $member));
215215
}
216216

217+
public function clearParameters(): static
218+
{
219+
return $this->withParameters($this->parameters()->clear());
220+
}
221+
217222
public function withoutParameter(string ...$keys): static
218223
{
219224
return $this->withParameters($this->parameters()->delete(...$keys));

src/ItemTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,13 @@ public function it_can_create_via_parameters_access_methods_a_new_object(): void
348348
$instance3 = $instance1->prependParameter('a', false);
349349
$instance4 = $instance1->withoutParameter('b');
350350
$instance5 = $instance1->withoutParameter('a');
351+
$instance6 = $instance1->clearParameters();
351352

352353
self::assertSame($instance1, $instance2);
353354
self::assertNotSame($instance1, $instance3);
354355
self::assertEquals($instance1->value(), $instance3->value());
355356
self::assertSame($instance1, $instance4);
356-
self::assertFalse($instance5->parameters()->hasMembers());
357+
self::assertTrue($instance5->parameters()->hasNoMembers());
358+
self::assertTrue($instance6->parameters()->hasNoMembers());
357359
}
358360
}

src/OrderedList.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121
final class OrderedList implements MemberList
2222
{
2323
/** @var array<int, Item|InnerList<int, Item>> */
24-
private array $members = [];
24+
private array $members;
2525

26-
private function __construct()
26+
private function __construct(InnerList|Item ...$members)
2727
{
28+
$this->members = array_values($members);
2829
}
2930

3031
public static function from(InnerList|Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
3132
{
32-
return self::fromList($members);
33+
return new self(...array_map(self::filterMember(...), $members));
3334
}
3435

3536
/**

src/ParameterAccess.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public function appendParameter(string $key, Item $member): static;
3939
*/
4040
public function withoutParameter(string ...$keys): static;
4141

42+
/**
43+
* Removes all parameters members associated with the list of submitted keys in the associated parameter intance.
44+
*
45+
* This method MUST retain the state of the current instance, and return
46+
* an instance that contains the specified parameter change.
47+
*/
48+
public function clearParameters(): static;
49+
4250
/**
4351
* Returns a new instance with the newly associated parameter instance.
4452
*

0 commit comments

Comments
 (0)