Skip to content

Commit 16c79b9

Browse files
committed
Parameters::value returns null on failure instead of throwing
1 parent b1b803b commit 16c79b9

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ echo $dictionary->toHttpValue(); //returns "a=?0, z=42.0"
218218
The `Parameters` instance exposes the following methods:
219219

220220
- `Parameters::values` to list all existing Bare Items value as an array list;
221-
- `Parameters::value($key)` to return the value of the Bare Item associated to the `$key` or throw if the key is unknown or invalid;
221+
- `Parameters::value(string $key)` to return the value of the Bare Item associated to the `$key` or `null` if the key is unknown or invalid;
222222
- `Parameters::merge` also accepts iterable as associative key-value as part of the variadic signature.
223223

224224
```php
@@ -238,7 +238,7 @@ $parameters->merge(
238238
['b' => 'false']
239239
);
240240
$parameters->toHttpValue(); // returns ;b="false";foo="foo"
241-
241+
$parameters->value('unknown'); // returns null
242242
```
243243

244244
#### Lists

src/InnerListTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ public function it_can_access_its_parameter_values(): void
142142
*/
143143
public function it_fails_to_access_unknown_parameter_values(): void
144144
{
145-
$this->expectException(InvalidOffset::class);
146-
147145
$instance = InnerList::fromList([false], ['foo' => 'bar']);
148-
$instance->parameters->value('bar');
146+
147+
self::assertNull($instance->parameters->value('bar'));
149148
}
150149
}

src/ItemTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,9 @@ public function it_can_access_its_parameter_values(): void
236236
*/
237237
public function it_fails_to_access_unknown_parameter_values(): void
238238
{
239-
$this->expectException(InvalidOffset::class);
240-
241239
$instance = Item::fromHttpValue('1; a; b=?0');
242-
$instance->parameters->value('bar');
240+
241+
self::assertNull($instance->parameters->value('bar'));
243242
}
244243

245244
/**

src/Parameters.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,10 @@ public function values(): array
168168
return array_map(fn (Item $item): Token|ByteSequence|float|int|bool|string => $item->value, $this->members);
169169
}
170170

171-
public function value(string $key): Token|ByteSequence|float|int|bool|string
171+
public function value(string $key): Token|ByteSequence|float|int|bool|string|null
172172
{
173-
self::validateKey($key);
174-
175173
if (!array_key_exists($key, $this->members)) {
176-
throw InvalidOffset::dueToKeyNotFound($key);
174+
return null;
177175
}
178176

179177
return $this->members[$key]->value;
@@ -314,9 +312,7 @@ public function prepend(string $key, Item|ByteSequence|Token|bool|int|float|stri
314312
public function merge(iterable ...$others): void
315313
{
316314
foreach ($others as $other) {
317-
foreach ($other as $key => $value) {
318-
$this->set($key, $value);
319-
}
315+
$this->members = [...$this->members, ...self::fromAssociative($other)->members];
320316
}
321317
}
322318
}

0 commit comments

Comments
 (0)