Skip to content

Commit d260c63

Browse files
committed
Improve StructuredFieldProvider usage
1 parent 877f798 commit d260c63

File tree

4 files changed

+92
-28
lines changed

4 files changed

+92
-28
lines changed

src/Dictionary.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ public static function new(): self
8989
public static function fromAssociative(StructuredFieldProvider|iterable $members): self
9090
{
9191
if ($members instanceof StructuredFieldProvider) {
92-
$members = $members->toStructuredField();
93-
}
92+
$structuredField = $members->toStructuredField();
9493

95-
if (!is_iterable($members)) {
96-
throw new InvalidArgument('The "'.$members::class.'" instance can not be used for creating a .'.self::class.' structured field.');
94+
return match (true) {
95+
$structuredField instanceof Dictionary,
96+
$structuredField instanceof Parameters => new self($structuredField->toAssociative()),
97+
default => throw new InvalidArgument('The '.StructuredFieldProvider::class.' must provide a structured field container; '.$structuredField::class.' given.'),
98+
};
9799
}
98100

99101
return new self($members);

src/InnerList.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,17 @@ public static function fromHttpValue(Stringable|string $httpValue, ?Ietf $rfc =
8686
* @param iterable<SfItemInput> $value
8787
* @param Parameters|iterable<string, SfItemInput> $parameters
8888
*/
89-
public static function fromAssociative(iterable $value, iterable $parameters): self
90-
{
89+
public static function fromAssociative(
90+
iterable $value,
91+
StructuredFieldProvider|Parameters|iterable $parameters
92+
): self {
93+
if ($parameters instanceof StructuredFieldProvider) {
94+
$parameters = $parameters->toStructuredField();
95+
if (!$parameters instanceof Parameters) {
96+
throw new InvalidArgument('The '.StructuredFieldProvider::class.' must provide a '.Parameters::class.'; '.$parameters::class.' given.');
97+
}
98+
}
99+
91100
if (!$parameters instanceof Parameters) {
92101
$parameters = Parameters::fromAssociative($parameters);
93102
}
@@ -100,13 +109,30 @@ public static function fromAssociative(iterable $value, iterable $parameters): s
100109
*/
101110
public static function fromPair(array $pair): self
102111
{
103-
return match (true) {
104-
[] === $pair => self::new(),
105-
!array_is_list($pair) => throw new SyntaxError('The pair must be represented by an array as a list.'),
106-
2 === count($pair) => new self($pair[0], !$pair[1] instanceof Parameters ? Parameters::fromPairs($pair[1]) : $pair[1]),
107-
1 === count($pair) => new self($pair[0], Parameters::new()),
108-
default => throw new SyntaxError('The pair first member must be the member list and the second member the inner list parameters.'),
109-
};
112+
if ([] === $pair) {
113+
return self::new();
114+
}
115+
116+
if (!array_is_list($pair) || 2 < count($pair)) {
117+
throw new SyntaxError('The pair must be represented by an non-empty array as a list containing at most 2 members.');
118+
}
119+
120+
if (1 === count($pair)) {
121+
return new self($pair[0], Parameters::new());
122+
}
123+
124+
if ($pair[1] instanceof StructuredFieldProvider) {
125+
$pair[1] = $pair[1]->toStructuredField();
126+
if (!$pair[1] instanceof Parameters) {
127+
throw new InvalidArgument('The '.StructuredFieldProvider::class.' must provide a '.Parameters::class.'; '.$pair[1]::class.' given.');
128+
}
129+
}
130+
131+
if (!$pair[1] instanceof Parameters) {
132+
$pair[1] = Parameters::fromPairs($pair[1]);
133+
}
134+
135+
return new self($pair[0], $pair[1]);
110136
}
111137

112138
/**
@@ -249,8 +275,15 @@ public function last(): ?Item
249275
return $this->members[$this->filterIndex(-1)] ?? null;
250276
}
251277

252-
public function withParameters(Parameters $parameters): static
278+
public function withParameters(StructuredFieldProvider|Parameters $parameters): static
253279
{
280+
if ($parameters instanceof StructuredFieldProvider) {
281+
$parameters = $parameters->toStructuredField();
282+
if (!$parameters instanceof Parameters) {
283+
throw new InvalidArgument('The '.StructuredFieldProvider::class.' must provide a '.Parameters::class.'; '.$parameters::class.' given.');
284+
}
285+
}
286+
254287
return $this->parameters->equals($parameters) ? $this : new self($this->members, $parameters);
255288
}
256289

src/Item.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,21 @@ public static function fromHttpValue(Stringable|string $httpValue, ?Ietf $rfc =
5757
/**
5858
* Returns a new instance from a value type and an iterable of key-value parameters.
5959
*
60-
* @param Parameters|iterable<string, SfItemInput> $parameters
60+
* @param StructuredFieldProvider|Parameters|iterable<string, SfItemInput> $parameters
6161
*
6262
* @throws SyntaxError If the value or the parameters are not valid
6363
*/
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+
6675
if (!$parameters instanceof Parameters) {
6776
$parameters = Parameters::fromAssociative($parameters);
6877
}
@@ -77,12 +86,23 @@ public static function fromAssociative(Bytes|Token|DisplayString|DateTimeInterfa
7786
*/
7887
public static function fromPair(array $pair): self
7988
{
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]);
86106
}
87107

88108
/**
@@ -353,8 +373,15 @@ public function withValue(
353373
return new self($value, $this->parameters);
354374
}
355375

356-
public function withParameters(Parameters $parameters): static
376+
public function withParameters(StructuredFieldProvider|Parameters $parameters): static
357377
{
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+
358385
return $this->parameters->equals($parameters) ? $this : new self($this->value, $parameters);
359386
}
360387
}

src/Parameters.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ public static function new(): self
8585
public static function fromAssociative(StructuredFieldProvider|iterable $members): self
8686
{
8787
if ($members instanceof StructuredFieldProvider) {
88-
$members = $members->toStructuredField();
89-
}
88+
$structuredField = $members->toStructuredField();
9089

91-
if (!is_iterable($members)) {
92-
throw new InvalidArgument('The "'.$members::class.'" instance can not be used for creating a .'.self::class.' structured field.');
90+
return match (true) {
91+
$structuredField instanceof Dictionary,
92+
$structuredField instanceof Parameters => new self($structuredField->toAssociative()),
93+
default => throw new InvalidArgument('The '.StructuredFieldProvider::class.' must provide a structured field container; '.$structuredField::class.' given.'),
94+
};
9395
}
9496

9597
return new self($members);

0 commit comments

Comments
 (0)