Skip to content

Commit c5b436c

Browse files
committed
Normalizes parameters usage
1 parent 16831d6 commit c5b436c

File tree

7 files changed

+32
-44
lines changed

7 files changed

+32
-44
lines changed

src/Dictionary.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,17 @@ public function pair(int $index): array
219219
*
220220
* @throws SyntaxError If the string key is not a valid
221221
*/
222-
public function set(string $key, StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): self
222+
public function set(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
223223
{
224224
$this->members[MapKey::fromString($key)->value] = self::filterMember($member);
225225

226226
return $this;
227227
}
228228

229-
private static function filterMember(StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): InnerList|Item
229+
private static function filterMember(InnerList|Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): InnerList|Item
230230
{
231231
return match (true) {
232232
$member instanceof InnerList, $member instanceof Item => $member,
233-
$member instanceof StructuredField => throw new InvalidArgument('Expecting a "'.Item::class.'" or a "'.InnerList::class.'" instance; received a "'.$member::class.'" instead.'),
234233
default => Item::from($member),
235234
};
236235
}
@@ -259,7 +258,7 @@ public function clear(): self
259258
*
260259
* @throws SyntaxError If the string key is not a valid
261260
*/
262-
public function append(string $key, StructuredField|ByteSequence|DateTimeInterface|Token|bool|int|float|string $member): self
261+
public function append(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
263262
{
264263
unset($this->members[$key]);
265264

@@ -273,7 +272,7 @@ public function append(string $key, StructuredField|ByteSequence|DateTimeInterfa
273272
*
274273
* @throws SyntaxError If the string key is not a valid
275274
*/
276-
public function prepend(string $key, StructuredField|ByteSequence|DateTimeInterface|Token|bool|int|float|string $member): self
275+
public function prepend(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
277276
{
278277
unset($this->members[$key]);
279278

@@ -285,7 +284,7 @@ public function prepend(string $key, StructuredField|ByteSequence|DateTimeInterf
285284
/**
286285
* Merges multiple instances using iterable associative structures.
287286
*
288-
* @param iterable<string, InnerList<int, Item>|Item|ByteSequence|DateTimeInterface|Token|bool|int|float|string> ...$others
287+
* @param iterable<string, InnerList<int, Item>|Item|DataType> ...$others
289288
*/
290289
public function mergeAssociative(iterable ...$others): self
291290
{

src/InnerList.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private function __construct(private readonly Parameters $parameters)
2828
/**
2929
* Returns a new instance.
3030
*/
31-
public static function from(Item|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string ...$members): self
31+
public static function from(Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
3232
{
3333
return self::fromList($members);
3434
}
@@ -47,7 +47,7 @@ public static function fromList(iterable $members, iterable $parameters = []): s
4747
return $instance;
4848
}
4949

50-
private static function filterMember(Item|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): Item
50+
private static function filterMember(Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): Item
5151
{
5252
return match (true) {
5353
$member instanceof Item => $member,
@@ -65,12 +65,12 @@ public function parameters(): Parameters
6565
return clone $this->parameters;
6666
}
6767

68-
public function prependParameter(string $key, Item|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): static
68+
public function prependParameter(string $key, Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
6969
{
7070
return $this->withParameters($this->parameters()->prepend($key, $member));
7171
}
7272

73-
public function appendParameter(string $key, Item|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): static
73+
public function appendParameter(string $key, Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
7474
{
7575
return $this->withParameters($this->parameters()->append($key, $member));
7676
}
@@ -151,7 +151,7 @@ public function get(string|int $offset): Item
151151
/**
152152
* Insert members at the beginning of the list.
153153
*/
154-
public function unshift(StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string ...$members): self
154+
public function unshift(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
155155
{
156156
$this->members = [...array_map(self::filterMember(...), array_values($members)), ...$this->members];
157157

@@ -161,7 +161,7 @@ public function unshift(StructuredField|ByteSequence|Token|DateTimeInterface|Str
161161
/**
162162
* Insert members at the end of the list.
163163
*/
164-
public function push(StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string ...$members): self
164+
public function push(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
165165
{
166166
$this->members = [...$this->members, ...array_map(self::filterMember(...), array_values($members))];
167167

@@ -173,7 +173,7 @@ public function push(StructuredField|ByteSequence|Token|DateTimeInterface|String
173173
*
174174
* @throws InvalidOffset If the index does not exist
175175
*/
176-
public function insert(int $index, StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string ...$members): self
176+
public function insert(int $index, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
177177
{
178178
$offset = $this->filterIndex($index);
179179
match (true) {
@@ -186,7 +186,7 @@ public function insert(int $index, StructuredField|ByteSequence|Token|DateTimeIn
186186
return $this;
187187
}
188188

189-
public function replace(int $index, StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): self
189+
public function replace(int $index, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
190190
{
191191
if (null === ($offset = $this->filterIndex($index))) {
192192
throw InvalidOffset::dueToIndexNotFound($index);

src/InvalidArgument.php

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

src/ItemTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,12 @@ public function it_can_create_via_with_value_method_a_new_object(): void
315315
{
316316
$instance1 = Item::from(Token::fromString('babayaga'), ['a' => true]);
317317
$instance2 = $instance1->withValue(Token::fromString('babayaga'));
318-
$instance3 = $instance1->withValue('babayaga');
318+
$instance3 = $instance1->withValue(new class() implements Stringable {
319+
public function __toString(): string
320+
{
321+
return 'babayaga';
322+
}
323+
});
319324

320325
self::assertSame($instance1, $instance2);
321326
self::assertNotSame($instance1, $instance3);

src/OrderedList.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private function __construct()
3232
*
3333
* @return static
3434
*/
35-
public static function from(InnerList|Item|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string ...$members): self
35+
public static function from(InnerList|Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
3636
{
3737
return self::fromList($members);
3838
}
@@ -50,7 +50,7 @@ public static function fromList(iterable $members): self
5050
return $instance;
5151
}
5252

53-
private static function filterMember(Item|InnerList|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): InnerList|Item
53+
private static function filterMember(InnerList|Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): InnerList|Item
5454
{
5555
return match (true) {
5656
$member instanceof InnerList, $member instanceof Item => $member,
@@ -135,7 +135,7 @@ public function get(string|int $offset): Item|InnerList
135135
*
136136
* @param InnerList<int, Item>|Item|DataType ...$members
137137
*/
138-
public function unshift(StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string ...$members): self
138+
public function unshift(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
139139
{
140140
$this->members = [...array_map(self::filterMember(...), array_values($members)), ...$this->members];
141141

@@ -147,7 +147,7 @@ public function unshift(StructuredField|ByteSequence|Token|DateTimeInterface|Str
147147
*
148148
* @param InnerList<int, Item>|Item|DataType ...$members
149149
*/
150-
public function push(StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string ...$members): self
150+
public function push(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
151151
{
152152
$this->members = [...$this->members, ...array_map(self::filterMember(...), array_values($members))];
153153

@@ -161,7 +161,7 @@ public function push(StructuredField|ByteSequence|Token|DateTimeInterface|String
161161
*
162162
* @throws InvalidOffset If the index does not exist
163163
*/
164-
public function insert(int $index, StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string ...$members): self
164+
public function insert(int $index, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
165165
{
166166
$offset = $this->filterIndex($index);
167167
match (true) {
@@ -181,7 +181,7 @@ public function insert(int $index, StructuredField|ByteSequence|Token|DateTimeIn
181181
*
182182
* @throws InvalidOffset If the index does not exist
183183
*/
184-
public function replace(int $index, StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): self
184+
public function replace(int $index, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
185185
{
186186
if (null === ($offset = $this->filterIndex($index))) {
187187
throw InvalidOffset::dueToIndexNotFound($index);

src/Parameters.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function create(): self
4343
* its keys represent the dictionary entry key
4444
* its values represent the dictionary entry value
4545
*
46-
* @param iterable<array-key, Item|Token|ByteSequence|DateTimeInterface|Stringable|float|int|bool|string> $members
46+
* @param iterable<array-key, Item|DataType> $members
4747
*
4848
* @throws SyntaxError If the string is not a valid
4949
* @throws ForbiddenStateError If the bare item contains parameters
@@ -65,7 +65,7 @@ public static function fromAssociative(iterable $members): self
6565
* the first member represents the instance entry key
6666
* the second member represents the instance entry value
6767
*
68-
* @param MemberOrderedMap<string, Item>|iterable<array{0:string, 1:Item|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string}> $pairs
68+
* @param MemberOrderedMap<string, Item>|iterable<array{0:string, 1:Item|DataType}> $pairs
6969
*
7070
* @throws ForbiddenStateError If the bare item contains parameters
7171
*/
@@ -85,7 +85,6 @@ public static function fromPairs(MemberOrderedMap|iterable $pairs): self
8585

8686
/**
8787
* @throws ForbiddenStateError If the bare item contains parameters
88-
* @throws InvalidArgument If the structured field is not supported
8988
*/
9089
private static function filterMember(Item $item): Item
9190
{
@@ -96,7 +95,7 @@ private static function filterMember(Item $item): Item
9695
throw new ForbiddenStateError('Parameters instances can only contain bare items.');
9796
}
9897

99-
private static function formatMember(Item|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): Item
98+
private static function formatMember(Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): Item
10099
{
101100
return match (true) {
102101
$member instanceof Item => self::filterMember($member),
@@ -109,8 +108,7 @@ private static function formatMember(Item|ByteSequence|Token|DateTimeInterface|S
109108
*
110109
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.1.2
111110
*
112-
* @throws SyntaxError If the string is not a valid
113-
* @throws ForbiddenStateError If the bare item contains parameters
111+
* @throws SyntaxError If the string is not a valid
114112
*/
115113
public static function fromHttpValue(Stringable|string $httpValue): self
116114
{
@@ -141,7 +139,6 @@ public static function fromHttpValue(Stringable|string $httpValue): self
141139
public function toHttpValue(): string
142140
{
143141
$formatter = fn (Item $member, string $offset): string => match (true) {
144-
$member->parameters()->hasMembers() => throw new ForbiddenStateError('Parameter member "'.$offset.'" is in invalid state; Parameters instances can only contain bare items.'),
145142
true === $member->value() => ';'.$offset,
146143
default => ';'.$offset.'='.$member->toHttpValue(),
147144
};
@@ -275,7 +272,7 @@ public function pair(int $index): array
275272
* @throws SyntaxError If the string key is not a valid
276273
* @throws ForbiddenStateError if the found item is in invalid state
277274
*/
278-
public function set(string $key, StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): self
275+
public function set(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
279276
{
280277
$this->members[MapKey::fromString($key)->value] = self::formatMember($member);
281278

@@ -307,7 +304,7 @@ public function clear(): self
307304
* @throws SyntaxError If the string key is not a valid
308305
* @throws ForbiddenStateError if the found item is in invalid state
309306
*/
310-
public function append(string $key, StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): self
307+
public function append(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
311308
{
312309
unset($this->members[$key]);
313310

@@ -322,7 +319,7 @@ public function append(string $key, StructuredField|ByteSequence|Token|DateTimeI
322319
* @throws SyntaxError If the string key is not a valid
323320
* @throws ForbiddenStateError if the found item is in invalid state
324321
*/
325-
public function prepend(string $key, StructuredField|ByteSequence|Token|DateTimeInterface|Stringable|bool|int|float|string $member): self
322+
public function prepend(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
326323
{
327324
unset($this->members[$key]);
328325

src/StructuredField.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ interface StructuredField
88
{
99
/**
1010
* Returns the serialize-representation of the Structured Field as a textual HTTP field value.
11-
*
12-
* @throws ForbiddenStateError If a component of the object is in invalid state
1311
*/
1412
public function toHttpValue(): string;
1513
}

0 commit comments

Comments
 (0)