Skip to content

Commit 19f0835

Browse files
committed
Bugfix Parameters::toHttpValue
1 parent bc42d42 commit 19f0835

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/Parameters.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function keys(): array
9999
return array_keys($this->elements);
100100
}
101101

102-
public function getByKey(string $key): Item|InnerList|null
102+
public function getByKey(string $key): Item
103103
{
104104
if (!array_key_exists($key, $this->elements)) {
105105
throw InvalidOffset::dueToKeyNotFound($key);
@@ -141,16 +141,22 @@ public function hasIndex(int $index): bool
141141

142142
public function toHttpValue(): string
143143
{
144-
$returnValue = '';
144+
$returnValue = [];
145145

146146
foreach ($this->elements as $key => $val) {
147-
$returnValue .= ';'.$key;
147+
if (!$val->parameters()->isEmpty()) {
148+
throw new SyntaxError('the Item cannot be parameterized.');
149+
}
150+
151+
$value = ';'.$key;
148152
if ($val->value() !== true) {
149-
$returnValue .= '='.$val->toHttpValue();
153+
$value .= '='.$val->toHttpValue();
150154
}
155+
156+
$returnValue[] = $value;
151157
}
152158

153-
return $returnValue;
159+
return implode('', $returnValue);
154160
}
155161

156162
public function set(string $key, Item|ByteSequence|Token|bool|int|float|string $element): void

src/ParametersTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,16 @@ public function it_can_merge_without_argument_and_not_throw(): void
178178
$instance->merge();
179179
self::assertCount(1, $instance);
180180
}
181+
182+
/**
183+
* @test
184+
*/
185+
public function it_fails_if_internal_parameters_are_changed_illegally(): void
186+
{
187+
$this->expectException(SyntaxError::class);
188+
189+
$fields = Item::from('/terms', ['rel' => 'copyright', 'anchor' => '#foo']);
190+
$fields->parameters()->getByKey('anchor')->parameters()->set('yolo', 42);
191+
$fields->toHttpValue();
192+
}
181193
}

0 commit comments

Comments
 (0)