Skip to content

Commit ee2ef43

Browse files
committed
Update Parameters and Dictionary toHttpValue implementation
1 parent c8ec5c8 commit ee2ef43

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
"require-dev": {
3131
"friendsofphp/php-cs-fixer": "^v3.8.0",
3232
"httpwg/structured-field-tests": "*@dev",
33-
"phpunit/phpunit": "^9.5.20",
3433
"phpstan/phpstan": "^1.5.4",
3534
"phpstan/phpstan-strict-rules": "^1.1",
3635
"phpstan/phpstan-phpunit": "^1.0",
37-
"phpstan/phpstan-deprecation-rules": "^1.0"
36+
"phpstan/phpstan-deprecation-rules": "^1.0",
37+
"phpunit/phpunit": "^9.5.20"
3838
},
3939
"autoload": {
4040
"psr-4": {
@@ -79,7 +79,7 @@
7979
],
8080
"extra": {
8181
"branch-alias": {
82-
"dev-develop": "1.0.x-dev"
82+
"dev-develop": "1.x-dev"
8383
}
8484
}
8585
}

src/Dictionary.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,12 @@ public static function fromHttpValue(string $httpValue): self
8989

9090
public function toHttpValue(): string
9191
{
92-
$returnValue = [];
93-
foreach ($this->members as $key => $member) {
94-
$returnValue[] = match (true) {
95-
$member instanceof Item && true === $member->value => $key.$member->parameters->toHttpValue(),
96-
default => $key.'='.$member->toHttpValue(),
97-
};
98-
}
92+
$formatter = fn (Item|InnerList $member, string $key): string => match (true) {
93+
$member instanceof Item && true === $member->value => $key.$member->parameters->toHttpValue(),
94+
default => $key.'='.$member->toHttpValue(),
95+
};
9996

100-
return implode(', ', $returnValue);
97+
return implode(', ', array_map($formatter, $this->members, array_keys($this->members)));
10198
}
10299

103100
public function count(): int

src/Parameters.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,13 @@ public static function fromHttpValue(string $httpValue): self
136136
*/
137137
public function toHttpValue(): string
138138
{
139-
$returnValue = [];
140-
141-
foreach ($this->members as $key => $member) {
142-
$value = ';'.$key;
143-
$member = self::filterMember($member);
144-
145-
if ($member->value !== true) {
146-
$value .= '='.$member->toHttpValue();
147-
}
148-
149-
$returnValue[] = $value;
150-
}
139+
$formatter = fn (Item $member, string $key): string => match (true) {
140+
!$member->parameters->isEmpty() => throw new ForbiddenStateError('Parameters instances can not contain parameterized Items.'),
141+
true === $member->value => ';'.$key,
142+
default => ';'.$key.'='.$member->toHttpValue(),
143+
};
151144

152-
return implode('', $returnValue);
145+
return implode('', array_map($formatter, $this->members, array_keys($this->members)));
153146
}
154147

155148
public function count(): int

0 commit comments

Comments
 (0)