Skip to content

Commit 04c5834

Browse files
committed
Improve documentation
1 parent a6a727d commit 04c5834

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

README.md

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ $field[0]->value()->toString(); // returns 'text/html'
2525
$field[0]->parameter('q'); // returns null
2626

2727
//2 - building a Retrofit Cookie Header
28-
echo DataType::List->build[
28+
echo DataType::List->build([
2929
[
3030
['foo', 'bar'],
3131
[
@@ -69,11 +69,10 @@ header. Content validation is out of scope for this library.
6969
> [!NOTE]
7070
> New in version 1.2.0
7171
72-
The `DataType` enum list all five (5) available data type according to the RFC. It is also a
73-
Factory to enable parsing and building such data types. To parse a field you need to give
74-
the `parse` method a string or a stringable object representing the field text
75-
representation. On success, it will return a `StruncturedField` implementing
76-
object otherwise an exception will be thrown.
72+
To quickly parse or build one of the five (5) available data type according to the RFC, you can use the `DataType` enum.
73+
Apart from listing the data types (`List`, `InnerList`, `Parameters`, `Dictionary` and `Item`) you can give to
74+
its `parse` method a string or a stringable object representing a field text representation. On success,
75+
it will return an object representing the structured field otherwise an exception will be thrown.
7776

7877
```php
7978
$headerLine = 'bar;baz=42'; //the raw header line is a structured field item
@@ -102,12 +101,15 @@ echo DataType::List->build([
102101
// display "dumela lefatshe";a=?0, ("a" "b" @1703319068);a
103102
```
104103

104+
> [!TIP]
105+
> While the format can be overwhelming at first, you will come to understand it while reading
106+
> the rest of the documentation. Under the hood, the `DataType` enum uses the mechanism discussed hereafter.
107+
105108
#### Using specific named constructor
106109

107-
To complement the factory and to allow for more fine-grained manipulations, the package
108-
also provides specific classes for each data type. Parsing the header value is done
109-
via the `fromHttpValue` named constructor. The method is attached to each
110-
library's structured fields representation as shown below:
110+
The package provides specific classes for each data type. Parsing the structured field value is done
111+
via the `fromHttpValue` named constructor. The method is attached to each library's structured
112+
field representation as shown below:
111113

112114
```php
113115
declare(strict_types=1);
@@ -157,7 +159,7 @@ All five (5) structured data type as defined in the RFC are provided inside the
157159

158160
Starting with version `1.1` the internal parser has been made public in order to allow:
159161

160-
- clearer decoupling betwen parsing and objet building
162+
- clearer decoupling between parsing and objet building
161163
- different parsers implementations
162164
- improve the package usage in testing.
163165

@@ -216,37 +218,38 @@ Per the RFC, items can have different types that are translated to PHP using:
216218

217219
The table below summarizes the item value type.
218220

219-
| RFC Type | PHP Type | Package Enum Type |
220-
|-------------------|---------------------------|-----------------------|
221-
| Integer | `int` | `Type::Integer` |
222-
| Decimal | `float` | `Type::Decimal` |
223-
| String | `string` | `Type::String` |
224-
| Boolean | `bool` | `Type::Boolean` |
225-
| Token | class `Token` | `Type::Token` |
226-
| Byte Sequence | class `ByteSequence` | `Type::ByteSequence` |
227-
| Date (*) | class `DateTimeImmutable` | `Type::Date` |
228-
| DisplayString (*) | class `DisplayString` | `Type::DisplayString` |
221+
| RFC Type | PHP Type | Package Enum Name | Package Enum Value |
222+
|-------------------|---------------------------|-----------------------|--------------------|
223+
| Integer | `int` | `Type::Integer` | `ìnteger` |
224+
| Decimal | `float` | `Type::Decimal` | `decimal` |
225+
| String | `string` | `Type::String` | `string` |
226+
| Boolean | `bool` | `Type::Boolean` | `boolean` |
227+
| Token | class `Token` | `Type::Token` | `token` |
228+
| Byte Sequence | class `ByteSequence` | `Type::ByteSequence` | `bytesequence` |
229+
| Date (*) | class `DateTimeImmutable` | `Type::Date` | `date` |
230+
| DisplayString (*) | class `DisplayString` | `Type::DisplayString` | `displaystring` |
229231

230232
> [!NOTE]
231233
> The `Date` and `DisplayString` type are not yet part of any accepted
232234
> RFC. But they are already added as new types in the superseeding
233235
> RFC proposal.
234236
>
235237
> See https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-sfbis
236-
> for more information,
238+
> for more information.
237239
238240
The Enum `Type` which list all available types can be used to determine the RFC type
239241
corresponding to a PHP structure using the `Type::fromVariable` static method.
240-
The method will throw if the structure is not recognized Alternatively it is possible
241-
to use the `Type::tryFromValue` which will instead return `null` on unindentified type.
242-
On success both methods returns the corresponding enum `Type`.
242+
The method will throw if the structure is not recognized. Alternatively
243+
it is possible to use the `Type::tryFromVariable` which will instead
244+
return `null` on unidentified type. On success both methods
245+
return the corresponding enum `Type`.
243246

244247
```php
245248
use Bakame\Http\StructuredFields\Type;
246249

247250
echo Type::fromVariable(42)->value; // returns 'integer'
248251
echo Type::fromVariable(42.0)->name; // returns 'Decimal'
249-
echo Type::fromVariable(new SplTempFileObject()); // throws InvalidArgument
252+
echo Type::fromVariable(new SplTempFileObject()); // throws InvalidArgument
250253
echo Type::tryFromValue(new SplTempFileObject()); // returns null
251254
```
252255

@@ -268,8 +271,9 @@ PHP default type system, for them, we have defined three classes `Token`,
268271
`ByteSequence` and `DisplayString` to help with their representation.
269272

270273
```php
271-
use Bakame\Http\StructuredFields\Token;
272274
use Bakame\Http\StructuredFields\ByteSequence;
275+
use Bakame\Http\StructuredFields\DisplayString;
276+
use Bakame\Http\StructuredFields\Token;
273277

274278
Token::fromString(string|Stringable $value): Token
275279
ByteSequence::fromDecoded(string|Stringable $value): ByteSequence;
@@ -308,7 +312,7 @@ $displayString->type(); // returns Type::DisplayString
308312

309313
> [!WARNING]
310314
> The classes DO NOT expose the `Stringable` interface to distinguish them
311-
from a string or a string like object
315+
> from a regular string or a string like object
312316
313317
#### Item
314318

@@ -337,7 +341,7 @@ $container->hasMembers(): bool;
337341
$container->hasNoMembers(): bool;
338342
```
339343

340-
> [!NOTE]
344+
> [!IMPORTANT]
341345
> The `get` method will throw an `InvalidOffset` exception if no member exists for the given `$offset`.
342346
343347
To avoid invalid states, `ArrayAccess` modifying methods throw a `ForbiddenOperation`
@@ -362,7 +366,7 @@ $container->pair(int $offset): array{0:string, 1:StructuredField};
362366
$container->toPairs(): iterable<array{0:string, 1:StructuredField}>;
363367
```
364368

365-
> [!NOTE]
369+
> [!IMPORTANT]
366370
> The `pair` method will throw an `InvalidOffset` exception if no member exists for the given `$offset`.
367371
368372
#### Accessing the parameters values
@@ -377,7 +381,7 @@ use Bakame\Http\StructuredFields\Parameters;
377381

378382
$field->parameter(string $key): ByteSequence|Token|DisplayString|DateTimeImmutable|Stringable|string|int|float|bool|null;
379383
$field->parameters(): Parameters;
380-
$field->parameterByIndex(int $index): array{0:string, 1:ByteSequence|Token|DisplayString|DateTimeImmutable|Stringable|string|int|float|boo}|array{}
384+
$field->parameterByIndex(int $index): array{0:string, 1:ByteSequence|Token|DisplayString|DateTimeImmutable|Stringable|string|int|float|boo}
381385
InnerList::toPair(): array{0:list<Item>, 1:Parameters}>};
382386
Item::toPair(): array{0:ByteSequence|Token|DisplayString|DateTimeImmutable|Stringable|string|int|float|bool, 1:Parameters}>};
383387
```

0 commit comments

Comments
 (0)