Skip to content

Commit e9c2db3

Browse files
committed
Introduce ParameterAccess::parameterByIndex
1 parent 7f66362 commit e9c2db3

File tree

8 files changed

+59
-20
lines changed

8 files changed

+59
-20
lines changed

CHANGELOG.md

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

77
### Added
88

9-
- None
9+
- `Item::parameterByOffset` and `InnerList::parameterByOffset` tu returns the parameter as a tuple.
1010

1111
### Fixed
1212

13-
- `Item::parameter` and `InnerList::parameter` now supports accessing parameters value per index.
13+
- `Parser` class is no longer internal
1414

1515
### Deprecated
1616

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,13 @@ use Bakame\Http\StructuredFields\Parameters;
263263

264264
$field->parameter(string $key): ByteSequence|Token|DateTimeImmutable|Stringable|string|int|float|bool|null;
265265
$field->parameters(): Parameters;
266+
$field->parameterByIndex(int $index): array{0:string, 1:ByteSequence|Token|DateTimeImmutable|Stringable|string|int|float|boo}|array{}
266267
InnerList::toPair(): array{0:list<Item>, 1:Parameters}>};
267268
Item::toPair(): array{0:ByteSequence|Token|DateTimeImmutable|Stringable|string|int|float|bool, 1:Parameters}>};
268269
```
269270

270-
**The `parameter` method will return `null` if no value is found for the given index.**
271+
**The `parameter` method will return `null` if no value is found for the given key.**
272+
**The `parameterByIndex` method is added in `version 1.1.0` and returns an empty array if no parameter is found for the given index.**
271273

272274
### Building and Updating Structured Fields Values
273275

@@ -525,6 +527,22 @@ echo InnerList::new('foo', 'bar')
525527
// ("foo" "bar");expire=@1681538756;path="/";max-age=2500
526528
```
527529

530+
### Advanced usages
531+
532+
Starting with version `1.1.0` the parser is made public with the following static methods:
533+
534+
- `Parser::parseList(Stringable|String $value): array`
535+
- `Parser::parseInnerList(Stringable|String $value): array`
536+
- `Parser::parseDictionary(Stringable|String $value): array`
537+
- `Parser::parseItem(Stringable|String $value): array`
538+
- `Parser::parseParameters(Stringable|String $value): array`
539+
540+
All these static methods parse the HTTP Header string value and return a `array` structure
541+
representing the parsed string. It is possible to use this representation if you want
542+
to build your own structure field objects. Those methods are the ones used by all the
543+
`fromHttpValue` named constructors to generate `StructuredField` instances from their
544+
returned `array`.
545+
528546
## Contributing
529547

530548
Contributions are welcome and will be fully credited. Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CODE OF CONDUCT](.github/CODE_OF_CONDUCT.md) for details.

src/InnerList.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,29 @@ public function parameters(): Parameters
135135
return $this->parameters;
136136
}
137137

138-
public function parameter(string|int $key): Token|ByteSequence|DateTimeImmutable|int|float|string|bool|null
138+
public function parameter(string $key): Token|ByteSequence|DateTimeImmutable|int|float|string|bool|null
139139
{
140140
try {
141-
return match (true) {
142-
is_int($key) => $this->parameters->pair($key)[1]->value(),
143-
default => $this->parameters->get($key)->value(),
144-
};
141+
return $this->parameters->get($key)->value();
145142
} catch (StructuredFieldError) {
146143
return null;
147144
}
148145
}
149146

147+
/**
148+
* @return array{0:string, 1:Token|ByteSequence|DateTimeImmutable|int|float|string|bool}|array{}
149+
*/
150+
public function parameterByIndex(int $index): array
151+
{
152+
try {
153+
$tuple = $this->parameters->pair($index);
154+
155+
return [$tuple[0], $tuple[1]->value()];
156+
} catch (StructuredFieldError) {
157+
return [];
158+
}
159+
}
160+
150161
public function count(): int
151162
{
152163
return count($this->members);

src/InnerListTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ public function it_can_access_its_parameter_values(): void
132132

133133
self::assertSame('bar', $instance->parameters()->get('foo')->value());
134134
self::assertSame('bar', $instance->parameter('foo'));
135-
self::assertSame('bar', $instance->parameter(0));
135+
self::assertSame(['foo', 'bar'], $instance->parameterByIndex(0));
136136
self::assertNull($instance->parameter('non-existing-key'));
137-
self::assertNull($instance->parameter(-42));
137+
self::assertSame([], $instance->parameterByIndex(42));
138138
}
139139

140140
#[Test]

src/Item.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,29 @@ public function parameters(): Parameters
219219
return $this->parameters;
220220
}
221221

222-
public function parameter(string|int $key): Token|ByteSequence|DateTimeImmutable|int|float|string|bool|null
222+
public function parameter(string $key): Token|ByteSequence|DateTimeImmutable|int|float|string|bool|null
223223
{
224224
try {
225-
return match (true) {
226-
is_int($key) => $this->parameters->pair($key)[1]->value(),
227-
default => $this->parameters->get($key)->value(),
228-
};
225+
return $this->parameters->get($key)->value();
229226
} catch (StructuredFieldError) {
230227
return null;
231228
}
232229
}
233230

231+
/**
232+
* @return array{0:string, 1:Token|ByteSequence|DateTimeImmutable|int|float|string|bool}|array{}
233+
*/
234+
public function parameterByIndex(int $index): array
235+
{
236+
try {
237+
$tuple = $this->parameters->pair($index);
238+
239+
return [$tuple[0], $tuple[1]->value()];
240+
} catch (StructuredFieldError) {
241+
return [];
242+
}
243+
}
244+
234245
/**
235246
* Serialize the Item value according to RFC8941.
236247
*

src/ItemTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,11 @@ public function it_can_create_via_parameters_access_methods_a_new_object(): void
429429
self::assertEquals($instance1->value(), $instance3->value());
430430
self::assertSame($instance1, $instance4);
431431
self::assertTrue($instance1->parameter('a'));
432-
self::assertTrue($instance1->parameter(0));
432+
self::assertSame(['a', true], $instance1->parameterByIndex(0));
433433
self::assertNull($instance5->parameter('a'));
434434
self::assertTrue($instance5->parameters()->hasNoMembers());
435435
self::assertTrue($instance6->parameters()->hasNoMembers());
436436
self::assertNull($instance1->parameter('non-existing-key'));
437-
self::assertNull($instance1->parameter(-42));
437+
self::assertSame([], $instance1->parameterByIndex(42));
438438
}
439439
}

src/ParameterAccess.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/**
1010
* @phpstan-import-type SfItem from StructuredField
11+
*
12+
* @method array{0:string, 1:Token|ByteSequence|DateTimeImmutable|int|float|string|bool}|array{} parameterByIndex(int $index) returns the tuple representation of the parameter
1113
*/
1214
interface ParameterAccess
1315
{

src/Parser.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
*
2323
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2
2424
*
25-
* @internal Use Dictionary::fromHttpValue(), Parameters::fromHttpValue(),
26-
* OuterList::fromHttpValue(), InnerList::fromHttpValue() or Item::fromHttpValue() instead
27-
*
2825
* @phpstan-import-type SfType from StructuredField
2926
*/
3027
final class Parser

0 commit comments

Comments
 (0)