Skip to content

Commit 77afe49

Browse files
committed
Adding ParameterAccess::parameter method
1 parent 3be5979 commit 77afe49

File tree

7 files changed

+45
-13
lines changed

7 files changed

+45
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
66

77
### Added
88

9+
- `ParameterAccess::parameter` to ease parameter members value access.
910
- **[BC Break]** `OrderedList` is renamed `OuterList`.
1011
- **[BC Break]** `MemberContainer::remove` methods get added to the interface.
1112
- **[BC Break]** `MemberContainer::keys` method added to the interface.

README.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,13 @@ Last but not least you can attach and update to `Item` and `InnerList` instances
180180
following methods:
181181

182182
```php
183-
$field->withParameters(Parameters $parameters): static;
183+
$field->parameter($key): mixed|null;
184184
$field->addParameter($key, $value): static;
185185
$field->appendParameter($key, $value): static;
186186
$field->prependParameter($key, $value): static;
187187
$field->withoutParameters(...$keys): static;
188188
$field->withoutAllParameters($): static;
189+
$field->withParameters(Parameters $parameters): static;
189190
```
190191

191192
### Item and RFC Data Types
@@ -217,11 +218,10 @@ $item = Item::fromPair([
217218
["a", ByteSequence::fromDecoded("Hello World")],
218219
]
219220
]);
220-
$item->value(); // returns "hello world"
221-
$item->type(); // returns Type::String
222-
$item->parameters()["a"]->type(); // returns Type::ByteSequence
223-
$item->parameters()["a"]->value(); // returns StructuredFields\ByteSequence::fromDecoded('Hello World');
224-
echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
221+
$item->value(); // returns "hello world"
222+
$item->type(); // returns Type::String
223+
$item->parameters("a"); // returns StructuredFields\ByteSequence::fromDecoded('Hello World');
224+
echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
225225
```
226226

227227
Once again it is possible to simplify this code using the following technique:
@@ -233,11 +233,10 @@ use Bakame\Http\StructuredFields\Item;
233233
$item = Item::from("hello world", [
234234
"a" => Item::fromDecodedByteSequence("Hello World")
235235
]);
236-
$item->value(); // returns "hello world"
237-
$item->type(); // returns Type::String
238-
$item->parameters()["a"]->value(); // returns StructuredFields\ByteSequence::fromDecoded('Hello World');
239-
$item->parameters()["a"]->type(); // returns Type::ByteSequence
240-
echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
236+
$item->value(); // returns "hello world"
237+
$item->type(); // returns Type::String
238+
$item->parameters("a"); // returns StructuredFields\ByteSequence::fromDecoded('Hello World');
239+
echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
241240
```
242241

243242
The RFC define two (2) specific data types that can not be represented by PHP default type system, for them, we define
@@ -298,8 +297,8 @@ use Bakame\Http\StructuredFields\Item;
298297
$value = Item::from("Hello world", [
299298
'a' => 'foobar',
300299
]);
301-
$value->parameters()->has('b'); // return false
302-
$value->parameters()['a']; // return Item::from('foobar')
300+
$value->parameters('b'); // return null
301+
$value->parameters('a'); // return 'foobar'
303302
$value->parameters()['a'] = 23 // triggers a ForbiddenOperation exception
304303
unset($value->parameters()['a']); // triggers a ForbiddenOperation exception
305304
```

src/InnerList.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ public function parameters(): Parameters
7575
return $this->parameters;
7676
}
7777

78+
public function parameter(string $key): mixed
79+
{
80+
if ($this->parameters->has($key)) {
81+
return $this->parameters->get($key)->value();
82+
}
83+
84+
return null;
85+
}
86+
7887
public function addParameter(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
7988
{
8089
return $this->withParameters($this->parameters()->add($key, $member));

src/InnerListTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ public function it_can_create_via_parameters_access_methods_a_new_object(): void
215215
self::assertSame($instance1, $instance4);
216216
self::assertFalse($instance5->parameters()->hasMembers());
217217
self::assertTrue($instance6->parameters()->hasNoMembers());
218+
self::assertTrue($instance1->parameter('a'));
219+
self::assertNull($instance5->parameter('a'));
218220
}
219221

220222
#[Test]

src/Item.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ public function parameters(): Parameters
215215
return $this->parameters;
216216
}
217217

218+
public function parameter(string $key): mixed
219+
{
220+
if ($this->parameters->has($key)) {
221+
return $this->parameters->get($key)->value();
222+
}
223+
224+
return null;
225+
}
226+
218227
public function addParameter(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
219228
{
220229
return $this->withParameters($this->parameters()->add($key, $member));

src/ItemTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ public function it_can_create_via_parameters_access_methods_a_new_object(): void
358358
self::assertNotSame($instance1, $instance3);
359359
self::assertEquals($instance1->value(), $instance3->value());
360360
self::assertSame($instance1, $instance4);
361+
self::assertTrue($instance1->parameter('a'));
362+
self::assertNull($instance5->parameter('a'));
361363
self::assertTrue($instance5->parameters()->hasNoMembers());
362364
self::assertTrue($instance6->parameters()->hasNoMembers());
363365
}

src/ParameterAccess.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44

55
namespace Bakame\Http\StructuredFields;
66

7+
/**
8+
* @phpstan-import-type DataType from Value
9+
*/
710
interface ParameterAccess
811
{
912
/**
1013
* Returns a copy of the associated parameter instance.
1114
*/
1215
public function parameters(): Parameters;
1316

17+
/**
18+
* Returns the member value or null if no members value exists.
19+
*
20+
* @return DataType|null
21+
*/
22+
public function parameter(string $key): mixed;
23+
1424
/**
1525
* Adds a member if its key is not present at the of the associated parameter instance or update the instance at the given key.
1626
*

0 commit comments

Comments
 (0)