Skip to content

Commit ca7f9b0

Browse files
committed
Update documentation
1 parent 0326653 commit ca7f9b0

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

README.md

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ header. Content validation is out of scope for this library.**
6363

6464
### Parsing and Serializing Structured Fields
6565

66+
#### Basic usage
67+
6668
Parsing the header value is done via the `fromHttpValue` named constructor.
6769
The method is attached to each library's structured fields representation
6870
as shown below:
@@ -115,14 +117,27 @@ All five (5) structured data type as defined in the RFC are provided inside the
115117

116118
#### Advance usage
117119

118-
In order to allow:
120+
Starting with version `1.1` the internal parser has been made public in order to allow:
119121

120122
- clearer decoupling betwen parsing and objet building
121123
- different parsers implementations
122124
- improve the package usage in testing.
123125

124-
Starting with version `1.1` the internal parser has been made public. The class exposes
125-
the following method each belonging to a different contract or interface.
126+
Each `fromHttpValue` method signature has been updated to take a second optional argument
127+
that represents the parser interface to use in order to allow parsing of the HTTP string
128+
representation value.
129+
130+
By default, if no parser is provided, the package will default to use the package `Parser` class,
131+
132+
```php
133+
Item::fromHttpValue(Stringable|string $httpValue, ItemParser $parser = new Parser()): Item;
134+
InnerList::fromHttpValue(Stringable|string $httpValue, InnerListParser $parser = new Parser()): InnerList;
135+
Dictionary::fromHttpValue(Stringable|string $httpValue, DictionaryParser $parser = new Parser()): Dictionary;
136+
OuterList::fromHttpValue(Stringable|string $httpValue, ListParser $parser = new Parser()): OuterList;
137+
Parameters::fromHttpValue(Stringable|string $httpValue, ParametersParser $parser = new Parser()): Parameters;
138+
```
139+
140+
The `Parser` class exposes the following method each belonging to a different contract or interface.
126141

127142
```php
128143
Parser::parseValue(Stringable|string $httpValue): ByteSequence|Token|DateTimeImmutable|string|int|float|bool;
@@ -133,8 +148,7 @@ Parser::parseList(Stringable|string $httpValue): array;
133148
Parser::parseDictionary(Stringable|string $httpValue): array;
134149
```
135150

136-
While the provided default `Parser` class implements all these methods
137-
you are free to only implement the methods you need.
151+
Once instantiated, calling one of the above listed method is straightforward:
138152

139153
```php
140154
use Bakame\Http\StructuredFields\Parser;
@@ -149,19 +163,8 @@ $parser->parseItem('@1234567890;file=24');
149163
// ]
150164
```
151165

152-
Each `fromHttpValue` method signature has been updated to take a second optional argument
153-
that represents the parser interface to use in order to allow parsing of the HTTP string
154-
representation value.
155-
156-
By default, if no parser is provided, the package will default to use the package `Parser` class,
157-
158-
```php
159-
Item::fromHttpValue(Stringable|string $httpValue, ItemParser $parser = new Parser()): Item;
160-
InnerList::fromHttpValue(Stringable|string $httpValue, InnerListParser $parser = new Parser()): InnerList;
161-
Dictionary::fromHttpValue(Stringable|string $httpValue, DictionaryParser $parser = new Parser()): Dictionary;
162-
OuterList::fromHttpValue(Stringable|string $httpValue, ListParser $parser = new Parser()): OuterList;
163-
Parameters::fromHttpValue(Stringable|string $httpValue, ParametersParser $parser = new Parser()): Parameters;
164-
```
166+
**While the provided default `Parser` class implements all these methods you are free to only implement
167+
the methods you need.**
165168

166169
### Accessing Structured Fields Values
167170

@@ -406,7 +409,7 @@ $map->append(string $key, $value): static;
406409
$map->prepend(string $key, $value): static;
407410
$map->mergeAssociative(...$others): static;
408411
$map->mergePairs(...$others): static;
409-
$map->remove(string ...$key): static;
412+
$map->remove(string|int ...$key): static;
410413
```
411414

412415
As shown below:
@@ -474,6 +477,19 @@ echo $value; //b=?0, a=(bar "42" 42 42.0), c=@1671800423
474477

475478
**⚠️WARNING: on duplicate `keys` pair values are merged as per RFC logic.**
476479

480+
The `remove` always accepted string or integer as input. Since version `1.1` it will also remove
481+
the corresponding pair if its index is given to the method.
482+
483+
```diff
484+
<?php
485+
486+
use Bakame\Http\StructuredFields\Dictionary;
487+
488+
$field = Dictionary::fromHttpValue('b=?0, a=(bar "42" 42 42.0), c=@1671800423');
489+
- echo $field->remove('b', 2)->toHttpValue(); // returns a=(bar "42" 42 42.0), c=@1671800423
490+
+ echo $field->remove('b', 2)->toHttpValue(); // returns a=(bar "42" 42 42.0)
491+
```
492+
477493
#### Automatic conversion
478494

479495
For all containers, to ease instantiation the following automatic conversion are applied on

src/Parser.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ final class Parser implements DictionaryParser, InnerListParser, ItemParser, Lis
4646

4747
public function parseValue(Stringable|string $httpValue): ByteSequence|Token|DateTimeImmutable|string|int|float|bool
4848
{
49-
$itemString = trim((string) $httpValue, ' ');
50-
if ('' === $itemString || 1 === preg_match(self::REGEXP_INVALID_CHARACTERS, $itemString)) {
49+
$valueString = trim((string) $httpValue, ' ');
50+
if ('' === $valueString || 1 === preg_match(self::REGEXP_INVALID_CHARACTERS, $valueString)) {
5151
throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item value contains invalid characters.');
5252
}
5353

54-
[$value, $offset] = self::extractValue($itemString);
55-
if ('' !== substr($itemString, $offset)) {
54+
[$value, $offset] = self::extractValue($valueString);
55+
if ('' !== substr($valueString, $offset)) {
5656
throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item value contains invalid characters.');
5757
}
5858

@@ -297,14 +297,11 @@ private static function extractParametersValues(Stringable|string $httpValue): a
297297
$remainder = $httpValue;
298298
while ('' !== $remainder && ';' === $remainder[0]) {
299299
$remainder = ltrim(substr($remainder, 1), ' ');
300-
301300
$key = MapKey::fromStringBeginning($remainder)->value;
302301
$map[$key] = true;
303-
304302
$remainder = substr($remainder, strlen($key));
305303
if ('' !== $remainder && '=' === $remainder[0]) {
306304
$remainder = substr($remainder, 1);
307-
308305
[$map[$key], $offset] = self::extractValue($remainder);
309306
$remainder = substr($remainder, $offset);
310307
}

src/ValueParser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ interface ValueParser
1212
/**
1313
* Returns the data type value represented as a PHP type from an HTTP textual representation.
1414
*
15-
* @see https://www.rfc-editor.org/rfc/rfc8941.html#name-parsing-an-item
15+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.4
16+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.5
17+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.6
18+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.7
19+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.2.8
20+
* @see https://www.ietf.org/archive/id/draft-ietf-httpbis-sfbis-02.html#section-4.2.9
1621
*
1722
* @throws SyntaxError
1823
*/

0 commit comments

Comments
 (0)