@@ -57,12 +57,21 @@ public static function fromHttpValue(Stringable|string $httpValue, ?Ietf $rfc =
57
57
/**
58
58
* Returns a new instance from a value type and an iterable of key-value parameters.
59
59
*
60
- * @param Parameters|iterable<string, SfItemInput> $parameters
60
+ * @param StructuredFieldProvider| Parameters|iterable<string, SfItemInput> $parameters
61
61
*
62
62
* @throws SyntaxError If the value or the parameters are not valid
63
63
*/
64
- public static function fromAssociative (Bytes |Token |DisplayString |DateTimeInterface |string |int |float |bool $ value , iterable $ parameters ): self
65
- {
64
+ public static function fromAssociative (
65
+ Bytes |Token |DisplayString |DateTimeInterface |string |int |float |bool $ value ,
66
+ StructuredFieldProvider |Parameters |iterable $ parameters
67
+ ): self {
68
+ if ($ parameters instanceof StructuredFieldProvider) {
69
+ $ parameters = $ parameters ->toStructuredField ();
70
+ if (!$ parameters instanceof Parameters) {
71
+ throw new InvalidArgument ('The ' .StructuredFieldProvider::class.' must provide a ' .Parameters::class.'; ' .$ parameters ::class.' given. ' );
72
+ }
73
+ }
74
+
66
75
if (!$ parameters instanceof Parameters) {
67
76
$ parameters = Parameters::fromAssociative ($ parameters );
68
77
}
@@ -77,12 +86,23 @@ public static function fromAssociative(Bytes|Token|DisplayString|DateTimeInterfa
77
86
*/
78
87
public static function fromPair (array $ pair ): self
79
88
{
80
- return match (true ) {
81
- [] === $ pair , !array_is_list ($ pair ) => throw new SyntaxError ('The pair must be represented by an non-empty array as a list. ' ),
82
- 2 == count ($ pair ) => new self (new Value ($ pair [0 ]), $ pair [1 ] instanceof Parameters ? $ pair [1 ] : Parameters::fromPairs ($ pair [1 ])),
83
- 1 === count ($ pair ) => new self (new Value ($ pair [0 ]), Parameters::new ()),
84
- default => throw new SyntaxError ('The pair first member is the item value; its second member is the item parameters. ' ),
85
- };
89
+ if ([] === $ pair || !array_is_list ($ pair ) || 2 < count ($ pair )) {
90
+ throw new SyntaxError ('The pair must be represented by an non-empty array as a list containing at most 2 members. ' );
91
+ }
92
+
93
+ if (1 === count ($ pair )) {
94
+ return new self (new Value ($ pair [0 ]), Parameters::new ());
95
+ }
96
+
97
+ if ($ pair [1 ] instanceof StructuredFieldProvider) {
98
+ $ pair [1 ] = $ pair [1 ]->toStructuredField ();
99
+ }
100
+
101
+ if (!$ pair [1 ] instanceof Parameters) {
102
+ $ pair [1 ] = Parameters::fromPairs ($ pair [1 ]);
103
+ }
104
+
105
+ return new self (new Value ($ pair [0 ]), $ pair [1 ]);
86
106
}
87
107
88
108
/**
@@ -353,8 +373,15 @@ public function withValue(
353
373
return new self ($ value , $ this ->parameters );
354
374
}
355
375
356
- public function withParameters (Parameters $ parameters ): static
376
+ public function withParameters (StructuredFieldProvider | Parameters $ parameters ): static
357
377
{
378
+ if ($ parameters instanceof StructuredFieldProvider) {
379
+ $ parameters = $ parameters ->toStructuredField ();
380
+ if (!$ parameters instanceof Parameters) {
381
+ throw new InvalidArgument ('The ' .StructuredFieldProvider::class.' must provide a ' .Parameters::class.'; ' .$ parameters ::class.' given. ' );
382
+ }
383
+ }
384
+
358
385
return $ this ->parameters ->equals ($ parameters ) ? $ this : new self ($ this ->value , $ parameters );
359
386
}
360
387
}
0 commit comments