14
14
use Stringable ;
15
15
use Throwable ;
16
16
17
+ use function array_is_list ;
17
18
use function array_key_exists ;
18
19
use function array_keys ;
20
+ use function array_map ;
19
21
use function count ;
20
22
use function implode ;
21
23
use function is_array ;
24
+ use function is_bool ;
22
25
use function is_int ;
23
26
use function is_iterable ;
24
27
use function is_string ;
28
+ use function uasort ;
25
29
26
30
/**
27
31
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.2
@@ -56,14 +60,16 @@ private static function filterMember(mixed $member): InnerList|Item
56
60
{
57
61
if ($ member instanceof StructuredFieldProvider) {
58
62
$ member = $ member ->toStructuredField ();
63
+ if ($ member instanceof Item || $ member instanceof InnerList) {
64
+ return $ member ;
65
+ }
66
+
67
+ throw new InvalidArgument ('The ' .StructuredFieldProvider::class.' must provide a ' .Item::class.' or an ' .InnerList::class.'; ' .$ member ::class.' given. ' );
59
68
}
60
69
61
70
return match (true ) {
62
71
$ member instanceof InnerList,
63
72
$ member instanceof Item => $ member ,
64
- $ member instanceof OuterList,
65
- $ member instanceof Dictionary,
66
- $ member instanceof Parameters => throw new InvalidArgument ('An instance of " ' .$ member ::class.'" can not be a member of " ' .self ::class.'". ' ),
67
73
is_iterable ($ member ) => InnerList::new (...$ member ),
68
74
default => Item::new ($ member ),
69
75
};
@@ -122,6 +128,11 @@ public static function fromPairs(StructuredFieldProvider|Dictionary|Parameters|i
122
128
$ converter = function (mixed $ pair ): InnerList |Item {
123
129
if ($ pair instanceof StructuredFieldProvider) {
124
130
$ pair = $ pair ->toStructuredField ();
131
+ if ($ pair instanceof Item || $ pair instanceof InnerList) {
132
+ return $ pair ;
133
+ }
134
+
135
+ throw new InvalidArgument ('The ' .StructuredFieldProvider::class.' must provide a ' .Item::class.' or an ' .InnerList::class.'; ' .$ pair ::class.' given. ' );
125
136
}
126
137
127
138
if ($ pair instanceof InnerList || $ pair instanceof Item) {
@@ -140,13 +151,11 @@ public static function fromPairs(StructuredFieldProvider|Dictionary|Parameters|i
140
151
return InnerList::new ();
141
152
}
142
153
143
- [$ member , $ parameters ] = match (count ($ pair )) {
144
- 2 => $ pair ,
145
- 1 => [$ pair [0 ], []],
146
- default => throw new SyntaxError ('The pair first member is the item value; its second member is the item parameters. ' ),
147
- };
154
+ if (!in_array (count ($ pair ), [1 , 2 ], true )) {
155
+ throw new SyntaxError ('The pair first member is the item value; its second member is the item parameters. ' );
156
+ }
148
157
149
- return is_iterable ($ member ) ? InnerList::fromPair ([ $ member , $ parameters ] ) : Item::fromPair ([ $ member , $ parameters ] );
158
+ return is_iterable ($ pair [ 0 ] ) ? InnerList::fromPair ($ pair ) : Item::fromPair ($ pair );
150
159
};
151
160
152
161
return match (true ) {
@@ -191,9 +200,9 @@ public static function fromRfc8941(Stringable|string $httpValue): self
191
200
*
192
201
* @throws StructuredFieldError|Throwable If the string is not a valid
193
202
*/
194
- public static function fromHttpValue (Stringable |string $ httpValue , ? Ietf $ rfc = Ietf::Rfc9651): self
203
+ public static function fromHttpValue (Stringable |string $ httpValue , Ietf $ rfc = Ietf::Rfc9651): self
195
204
{
196
- return self ::fromPairs (Parser:: new ($ rfc )->parseDictionary ($ httpValue )); /* @phpstan-ignore-line */
205
+ return self ::fromPairs (( new Parser ($ rfc) )->parseDictionary ($ httpValue )); /* @phpstan-ignore-line */
197
206
}
198
207
199
208
public function toRfc9651 (): string
@@ -206,18 +215,14 @@ public function toRfc8941(): string
206
215
return $ this ->toHttpValue (Ietf::Rfc8941);
207
216
}
208
217
209
- public function toHttpValue (? Ietf $ rfc = Ietf::Rfc9651): string
218
+ public function toHttpValue (Ietf $ rfc = Ietf::Rfc9651): string
210
219
{
211
- $ rfc ??= Ietf::Rfc9651;
212
- $ members = [];
213
- foreach ($ this ->members as $ key => $ member ) {
214
- $ members [] = match (true ) {
215
- $ member instanceof Item && true === $ member ->value () => $ key .$ member ->parameters ()->toHttpValue ($ rfc ),
216
- default => $ key .'= ' .$ member ->toHttpValue ($ rfc ),
217
- };
218
- }
220
+ $ formatter = static fn (Item |InnerList $ member , string $ offset ): string => match (true ) {
221
+ $ member instanceof Item && true === $ member ->value () => $ offset .$ member ->parameters ()->toHttpValue ($ rfc ),
222
+ default => $ offset .'= ' .$ member ->toHttpValue ($ rfc ),
223
+ };
219
224
220
- return implode (', ' , $ members );
225
+ return implode (', ' , array_map ( $ formatter , $ this -> members , array_keys ( $ this -> members )) );
221
226
}
222
227
223
228
public function __toString (): string
@@ -506,7 +511,7 @@ public function last(): array
506
511
*/
507
512
public function add (
508
513
string $ key ,
509
- iterable |StructuredFieldProvider |OuterList | Dictionary | InnerList |Parameters |Item |Token |Bytes |DisplayString |DateTimeInterface |string |int |float |bool |null $ member
514
+ iterable |StructuredFieldProvider |Dictionary |Parameters |Item |Token |Bytes |DisplayString |DateTimeInterface |string |int |float |bool |null $ member
510
515
): self {
511
516
if (null === $ member ) {
512
517
return $ this ;
@@ -596,7 +601,7 @@ public function removeByKeys(string ...$keys): self
596
601
*/
597
602
public function append (
598
603
string $ key ,
599
- iterable |StructuredFieldProvider |OuterList | Dictionary | InnerList |Parameters |Item |Token |Bytes |DisplayString |DateTimeInterface |string |int |float |bool $ member
604
+ iterable |StructuredFieldProvider |Dictionary |Parameters |Item |Token |Bytes |DisplayString |DateTimeInterface |string |int |float |bool $ member
600
605
): self {
601
606
$ members = $ this ->members ;
602
607
unset($ members [$ key ]);
@@ -616,7 +621,7 @@ public function append(
616
621
*/
617
622
public function prepend (
618
623
string $ key ,
619
- iterable |StructuredFieldProvider |OuterList | Dictionary | InnerList |Parameters |Item |Token |Bytes |DisplayString |DateTimeInterface |string |int |float |bool $ member
624
+ iterable |StructuredFieldProvider |Dictionary |Parameters |Item |Token |Bytes |DisplayString |DateTimeInterface |string |int |float |bool $ member
620
625
): self {
621
626
$ members = $ this ->members ;
622
627
unset($ members [$ key ]);
@@ -840,7 +845,7 @@ public function filter(callable $callback): self
840
845
public function sort (callable $ callback ): self
841
846
{
842
847
$ members = iterator_to_array ($ this );
843
- usort ($ members , $ callback );
848
+ uasort ($ members , $ callback );
844
849
845
850
return self ::fromPairs ($ members );
846
851
}
0 commit comments