Skip to content

Commit

Permalink
Parameters::value returns null on failure instead of throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 20, 2022
1 parent b1b803b commit 16c79b9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ echo $dictionary->toHttpValue(); //returns "a=?0, z=42.0"
The `Parameters` instance exposes the following methods:

- `Parameters::values` to list all existing Bare Items value as an array list;
- `Parameters::value($key)` to return the value of the Bare Item associated to the `$key` or throw if the key is unknown or invalid;
- `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;
- `Parameters::merge` also accepts iterable as associative key-value as part of the variadic signature.

```php
Expand All @@ -238,7 +238,7 @@ $parameters->merge(
['b' => 'false']
);
$parameters->toHttpValue(); // returns ;b="false";foo="foo"

$parameters->value('unknown'); // returns null
```

#### Lists
Expand Down
5 changes: 2 additions & 3 deletions src/InnerListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ public function it_can_access_its_parameter_values(): void
*/
public function it_fails_to_access_unknown_parameter_values(): void
{
$this->expectException(InvalidOffset::class);

$instance = InnerList::fromList([false], ['foo' => 'bar']);
$instance->parameters->value('bar');

self::assertNull($instance->parameters->value('bar'));
}
}
5 changes: 2 additions & 3 deletions src/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,9 @@ public function it_can_access_its_parameter_values(): void
*/
public function it_fails_to_access_unknown_parameter_values(): void
{
$this->expectException(InvalidOffset::class);

$instance = Item::fromHttpValue('1; a; b=?0');
$instance->parameters->value('bar');

self::assertNull($instance->parameters->value('bar'));
}

/**
Expand Down
10 changes: 3 additions & 7 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,10 @@ public function values(): array
return array_map(fn (Item $item): Token|ByteSequence|float|int|bool|string => $item->value, $this->members);
}

public function value(string $key): Token|ByteSequence|float|int|bool|string
public function value(string $key): Token|ByteSequence|float|int|bool|string|null
{
self::validateKey($key);

if (!array_key_exists($key, $this->members)) {
throw InvalidOffset::dueToKeyNotFound($key);
return null;
}

return $this->members[$key]->value;
Expand Down Expand Up @@ -314,9 +312,7 @@ public function prepend(string $key, Item|ByteSequence|Token|bool|int|float|stri
public function merge(iterable ...$others): void
{
foreach ($others as $other) {
foreach ($other as $key => $value) {
$this->set($key, $value);
}
$this->members = [...$this->members, ...self::fromAssociative($other)->members];
}
}
}

0 comments on commit 16c79b9

Please sign in to comment.