Skip to content

Commit afda658

Browse files
committed
S Simplify Interfaces and add convenient methods
1 parent ea8f0f1 commit afda658

11 files changed

+276
-469
lines changed

src/Dictionary.php

+176-62
Large diffs are not rendered by default.

src/Ietf.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function isObsolete(): bool
3333
{
3434
return match ($this) {
3535
self::Rfc9651 => false,
36-
self::Rfc8941 => true,
36+
default => true,
3737
};
3838
}
3939

src/InnerList.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
use ArrayAccess;
88
use Closure;
9+
use Countable;
910
use DateTimeImmutable;
1011
use DateTimeInterface;
1112
use Iterator;
13+
use IteratorAggregate;
1214
use Stringable;
1315

1416
use function array_filter;
@@ -30,9 +32,10 @@
3032
* @phpstan-import-type SfItemInput from StructuredField
3133
* @phpstan-import-type SfInnerListPair from StructuredField
3234
*
33-
* @implements MemberList<int, Item>
35+
* @implements ArrayAccess<int, Item>
36+
* @implements IteratorAggregate<int, Item>
3437
*/
35-
final class InnerList implements MemberList, ParameterAccess
38+
final class InnerList implements ArrayAccess, Countable, IteratorAggregate, ParameterAccess, StructuredField
3639
{
3740
/** @var list<Item> */
3841
private readonly array $members;
@@ -80,7 +83,7 @@ public static function fromHttpValue(Stringable|string $httpValue, ?Ietf $rfc =
8083
* Returns a new instance with an iter.
8184
*
8285
* @param iterable<SfItemInput> $value
83-
* @param MemberOrderedMap<string, Item>|iterable<string, SfItemInput> $parameters
86+
* @param Parameters|iterable<string, SfItemInput> $parameters
8487
*/
8588
public static function fromAssociative(iterable $value, iterable $parameters): static
8689
{
@@ -94,7 +97,7 @@ public static function fromAssociative(iterable $value, iterable $parameters): s
9497
/**
9598
* @param array{
9699
* 0:iterable<SfItemInput>,
97-
* 1:MemberOrderedMap<string, Item>|iterable<array{0:string, 1:SfItemInput}>
100+
* 1:Parameters|iterable<array{0:string, 1:SfItemInput}>
98101
* } $pair
99102
*/
100103
public static function fromPair(array $pair): static
@@ -149,7 +152,7 @@ public function __toString(): string
149152
}
150153

151154
/**
152-
* @return array{0:list<Item>, 1:MemberOrderedMap<string, Item>}
155+
* @return array{0:list<Item>, 1:Parameters}
153156
*/
154157
public function toPair(): array
155158
{
@@ -259,6 +262,11 @@ public function last(): ?StructuredField
259262
return $this->members[$this->filterIndex(-1)] ?? null;
260263
}
261264

265+
public function sortParameters(Closure $callback): static
266+
{
267+
return $this->withParameters($this->parameters()->sort($callback));
268+
}
269+
262270
/**
263271
* Inserts members at the beginning of the list.
264272
*/
@@ -512,13 +520,8 @@ public function withoutAnyParameter(): static
512520
return $this->withParameters(Parameters::new());
513521
}
514522

515-
/**
516-
* @deprecated since version 1.1
517-
* @see ParameterAccess::withoutParameterByKeys()
518-
* @codeCoverageIgnore
519-
*/
520-
public function withoutParameter(string ...$keys): static
523+
public function filterParameters(Closure $callback): static
521524
{
522-
return $this->withoutParameterByKeys(...$keys);
525+
return $this->withParameters($this->parameters()->filter($callback));
523526
}
524527
}

src/Item.php

+12-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Bakame\Http\StructuredFields;
66

7+
use Closure;
78
use DateTimeImmutable;
89
use DateTimeInterface;
910
use DateTimeZone;
@@ -15,10 +16,9 @@
1516
/**
1617
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.3
1718
*
18-
* @phpstan-import-type SfType from StructuredField
1919
* @phpstan-import-type SfItemInput from StructuredField
20-
* @phpstan-import-type SfTypeInput from StructuredField
2120
* @phpstan-import-type SfItemPair from StructuredField
21+
* @phpstan-import-type SfTypeInput from StructuredField
2222
*/
2323
final class Item implements ParameterAccess, StructuredField
2424
{
@@ -68,10 +68,7 @@ public static function fromAssociative(ByteSequence|Token|DisplayString|DateTime
6868
}
6969

7070
/**
71-
* @param array{
72-
* 0: SfType,
73-
* 1: MemberOrderedMap<string, Item>|iterable<array{0:string, 1:SfItemInput}>
74-
* } $pair
71+
* @param array{0: SfTypeInput, 1: Parameters|iterable<array{0:string, 1:SfItemInput}>} $pair
7572
*
7673
* @throws SyntaxError If the pair or its content is not valid.
7774
*/
@@ -87,7 +84,7 @@ public static function fromPair(array $pair): self
8784
/**
8885
* Returns a new bare instance from value.
8986
*
90-
* @param SfTypeInput|array{0:SfType, 1:MemberOrderedMap<string, Item>|iterable<array{0:string, 1:SfItemInput}>} $value
87+
* @param SfTypeInput|array{0:SfTypeInput, 1:Parameters|iterable<array{0:string, 1:SfItemInput}>} $value
9188
*
9289
* @throws SyntaxError If the value is not valid.
9390
*/
@@ -314,7 +311,7 @@ public function __toString(): string
314311
}
315312

316313
/**
317-
* @return array{0:SfItemInput, 1:MemberOrderedMap<string, Item>}
314+
* @return array{0:SfItemInput, 1:Parameters}
318315
*/
319316
public function toPair(): array
320317
{
@@ -340,6 +337,11 @@ public function withValue(
340337
return new self($value, $this->parameters);
341338
}
342339

340+
public function sortParameters(Closure $callback): static
341+
{
342+
return $this->withParameters($this->parameters()->sort($callback));
343+
}
344+
343345
public function withParameters(Parameters $parameters): static
344346
{
345347
return $this->parameters->toHttpValue() === $parameters->toHttpValue() ? $this : new self($this->value, $parameters);
@@ -413,13 +415,8 @@ public function withoutAnyParameter(): static
413415
return $this->withParameters(Parameters::new());
414416
}
415417

416-
/**
417-
* @deprecated since version 1.1
418-
* @see ParameterAccess::withoutParameterByKeys()
419-
* @codeCoverageIgnore
420-
*/
421-
public function withoutParameter(string ...$keys): static
418+
public function filterParameters(Closure $callback): static
422419
{
423-
return $this->withoutParameterByKeys(...$keys);
420+
return $this->withParameters($this->parameters()->filter($callback));
424421
}
425422
}

src/MemberContainer.php

-85
This file was deleted.

src/MemberList.php

-72
This file was deleted.

0 commit comments

Comments
 (0)