Skip to content

Commit 7f66362

Browse files
committed
Improve decoupling from Parser
1 parent 39f896b commit 7f66362

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
All Notable changes to `bakame/http-strucured-fields` will be documented in this file.
44

5+
## [Next] - TBD
6+
7+
### Added
8+
9+
- None
10+
11+
### Fixed
12+
13+
- `Item::parameter` and `InnerList::parameter` now supports accessing parameters value per index.
14+
15+
### Deprecated
16+
17+
- None
18+
19+
### Removed
20+
21+
- None
22+
523
## [1.0.1] - 2023-04-20
624

725
### Added

src/InnerList.php

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

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

src/InnerListTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +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));
136+
self::assertNull($instance->parameter('non-existing-key'));
137+
self::assertNull($instance->parameter(-42));
135138
}
136139

137140
#[Test]

src/Item.php

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

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

src/ItemTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +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));
432433
self::assertNull($instance5->parameter('a'));
433434
self::assertTrue($instance5->parameters()->hasNoMembers());
434435
self::assertTrue($instance6->parameters()->hasNoMembers());
436+
self::assertNull($instance1->parameter('non-existing-key'));
437+
self::assertNull($instance1->parameter(-42));
435438
}
436439
}

src/Parser.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function parseItem(Stringable|string $httpValue): array
5151
throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item contains invalid characters.');
5252
}
5353

54-
[$value, $offset] = Parser::parseBareItem($itemString);
54+
[$value, $offset] = Parser::parseValue($itemString);
5555
$remainder = substr($itemString, $offset);
5656
if ('' !== $remainder && !str_contains($remainder, ';')) {
5757
throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item contains invalid characters.');
@@ -72,7 +72,7 @@ public static function parseItem(Stringable|string $httpValue): array
7272
public static function parseParameters(Stringable|string $httpValue): array
7373
{
7474
$httpValue = trim((string) $httpValue);
75-
[$parameters, $offset] = Parser::parseContainedParameters($httpValue);
75+
[$parameters, $offset] = Parser::parseParametersValues($httpValue);
7676
if (strlen($httpValue) !== $offset) {
7777
throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for Parameters contains invalid characters.');
7878
}
@@ -194,7 +194,7 @@ private static function parseItemOrInnerList(string $httpValue): array
194194
return self::parseInnerListValue($httpValue);
195195
}
196196

197-
[$item, $remainder] = self::parseContainedItem($httpValue);
197+
[$item, $remainder] = self::parseItemValue($httpValue);
198198

199199
return [$item, strlen($httpValue) - strlen($remainder)];
200200
}
@@ -215,13 +215,13 @@ private static function parseInnerListValue(string $httpValue): array
215215

216216
if (')' === $remainder[0]) {
217217
$remainder = substr($remainder, 1);
218-
[$parameters, $offset] = self::parseContainedParameters($remainder);
218+
[$parameters, $offset] = self::parseParametersValues($remainder);
219219
$remainder = substr($remainder, $offset);
220220

221221
return [[$list, $parameters], strlen($httpValue) - strlen($remainder)];
222222
}
223223

224-
[$list[], $remainder] = self::parseContainedItem($remainder);
224+
[$list[], $remainder] = self::parseItemValue($remainder);
225225

226226
if ('' !== $remainder && !in_array($remainder[0], [' ', ')'], true)) {
227227
throw new SyntaxError("The HTTP textual representation \"$remainder\" for a inner list is using invalid characters.");
@@ -236,11 +236,11 @@ private static function parseInnerListValue(string $httpValue): array
236236
*
237237
* @return array{0:array{0:SfType, 1:array<string, SfType>}, 1:string}
238238
*/
239-
private static function parseContainedItem(string $remainder): array
239+
private static function parseItemValue(string $remainder): array
240240
{
241-
[$value, $offset] = self::parseBareItem($remainder);
241+
[$value, $offset] = self::parseValue($remainder);
242242
$remainder = substr($remainder, $offset);
243-
[$parameters, $offset] = self::parseContainedParameters($remainder);
243+
[$parameters, $offset] = self::parseParametersValues($remainder);
244244

245245
return [[$value, $parameters], substr($remainder, $offset)];
246246
}
@@ -252,7 +252,7 @@ private static function parseContainedItem(string $remainder): array
252252
*
253253
* @return array{0:SfType, 1:int}
254254
*/
255-
private static function parseBareItem(string $httpValue): array
255+
private static function parseValue(string $httpValue): array
256256
{
257257
return match (true) {
258258
'"' === $httpValue[0] => self::parseString($httpValue),
@@ -272,7 +272,7 @@ private static function parseBareItem(string $httpValue): array
272272
*
273273
* @return array{0:array<string, SfType>, 1:int}
274274
*/
275-
private static function parseContainedParameters(Stringable|string $httpValue): array
275+
private static function parseParametersValues(Stringable|string $httpValue): array
276276
{
277277
$map = [];
278278
$httpValue = (string) $httpValue;
@@ -287,7 +287,7 @@ private static function parseContainedParameters(Stringable|string $httpValue):
287287
if ('' !== $remainder && '=' === $remainder[0]) {
288288
$remainder = substr($remainder, 1);
289289

290-
[$map[$key], $offset] = self::parseBareItem($remainder);
290+
[$map[$key], $offset] = self::parseValue($remainder);
291291
$remainder = substr($remainder, $offset);
292292
}
293293
}

0 commit comments

Comments
 (0)