Skip to content

Commit 0e5d665

Browse files
committed
Adding InnerList::fromHttpValue named constructor
1 parent aa306a8 commit 0e5d665

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
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+
- `InnerList::fromHttpValue` named constructor to make the public API consistent for all VOs
10+
11+
### Fixed
12+
13+
- None
14+
15+
### Deprecated
16+
17+
- None
18+
19+
### Removed
20+
21+
- None
22+
523
## [0.2.0] - 2022-03-20
624

725
### Added

src/InnerList.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,17 @@ private static function filterMember(Item|ByteSequence|Token|bool|int|float|stri
6363
};
6464
}
6565

66+
public static function fromHttpValue(string $httpValue): self
67+
{
68+
return Parser::parseInnerList($httpValue);
69+
}
70+
6671
public function toHttpValue(): string
6772
{
68-
return '('
69-
.implode(' ', array_map(fn (Item $value): string => $value->toHttpValue(), $this->members))
70-
.')'
71-
.$this->parameters->toHttpValue();
73+
return '('.implode(' ', array_map(
74+
fn (Item $value): string => $value->toHttpValue(),
75+
$this->members
76+
)).')'.$this->parameters->toHttpValue();
7277
}
7378

7479
public function count(): int

src/InnerListTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,20 @@ public function it_fails_to_access_unknown_parameter_values(): void
146146

147147
self::assertNull($instance->parameters->value('bar'));
148148
}
149+
150+
/**
151+
* @test
152+
*/
153+
public function it_successfully_parse_a_http_field(): void
154+
{
155+
$instance = InnerList::fromHttpValue('("hello)world" 42 42.0;john=doe);foo="bar("');
156+
157+
self::assertCount(3, $instance);
158+
self::assertCount(1, $instance->parameters);
159+
self::assertSame('bar(', $instance->parameters->value('foo'));
160+
self::assertSame('hello)world', $instance->get(0)->value);
161+
self::assertSame(42, $instance->get(1)->value);
162+
self::assertSame(42.0, $instance->get(2)->value);
163+
self::assertInstanceOf(Token::class, $instance->get(2)->parameters->value('john'));
164+
}
149165
}

src/Parser.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2
2222
*
23-
* @internal Use OrderedList::fromHttpValue() or Dictionary::fromHttpValue() instead
23+
* @internal Use Dictionary::fromHttpValue(), OrderedList::fromHttpValue(), InnerList::fromHttpValue() or Item::fromHttpValue() instead
2424
*/
2525
final class Parser
2626
{
@@ -95,6 +95,26 @@ public static function parseDictionary(string $httpValue): array
9595
return $members;
9696
}
9797

98+
/**
99+
* Returns a InnerList value object from an HTTP textual representation.
100+
*
101+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1.2
102+
*/
103+
public static function parseInnerList(string $httpValue): InnerList
104+
{
105+
if ('(' !== $httpValue[0]) {
106+
throw new SyntaxError("The HTTP textual representation `$httpValue` for a inner list is missing a parenthesis.");
107+
}
108+
109+
[$members, $offset] = self::parseInnerListValue($httpValue);
110+
$remainder = ltrim(substr($httpValue, $offset), " \t");
111+
if ('' !== $remainder) {
112+
throw new SyntaxError("The HTTP textual representation `$httpValue` for a inner list contains invalid data.");
113+
}
114+
115+
return $members;
116+
}
117+
98118
/**
99119
* Returns an Item or an InnerList value object from an HTTP textual representation.
100120
*
@@ -105,7 +125,7 @@ public static function parseDictionary(string $httpValue): array
105125
private static function parseItemOrInnerList(string $httpValue): array
106126
{
107127
if ('(' === $httpValue[0]) {
108-
return self::parseInnerList($httpValue);
128+
return self::parseInnerListValue($httpValue);
109129
}
110130

111131
[$value, $offset] = self::parseBareItem($httpValue);
@@ -118,13 +138,13 @@ private static function parseItemOrInnerList(string $httpValue): array
118138
}
119139

120140
/**
121-
* Returns an InnerList value object from an HTTP textual representation.
141+
* Returns an InnerList value object from an HTTP textual representation and the consumed offset.
122142
*
123143
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.1.2
124144
*
125145
* @return array{0:InnerList, 1:int}
126146
*/
127-
private static function parseInnerList(string $httpValue): array
147+
private static function parseInnerListValue(string $httpValue): array
128148
{
129149
$members = [];
130150
$remainder = substr($httpValue, 1);

0 commit comments

Comments
 (0)