Skip to content

Commit e0a56bc

Browse files
committed
Improve documentation v2
1 parent 84d73bc commit e0a56bc

File tree

5 files changed

+48
-32
lines changed

5 files changed

+48
-32
lines changed

docs/00-intro.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ $container[1]->parameterByName(name: 'q', default: 1.0); // returns 1.0 if the p
2323

2424
While they are plenty of HTTP headers and trailers, they have historically come each with their own set of
2525
rules and constraints when it came to parsing and serializing them. Trying to use the parsing logic of a cookie header
26-
to parse an accept header will fail. The various parsing logics hinders HTTP headers and trailers usage, modernization
27-
or security. The [Structured Field RFC](https://www.rfc-editor.org/rfc/rfc9651.html) aim at tackling those issues by
28-
unifying HTTP headers and trailers parsing and serializing.
26+
to parse an accept header will fail. The various parsing logics hinder HTTP headers and trailers usage, modernization
27+
or security. The [Structured Field RFC](https://www.rfc-editor.org/rfc/rfc9651.html) aim at tackling those issues by unifying HTTP headers and trailers
28+
parsing and serializing.
2929

3030
New HTTP headers or trailers (called HTTP fields) are encouraged to use the RFC algorithm, data and value types and
3131
ongoing discussions are happening to [retrofit existing headers that do not match the RFC](https://httpwg.org/http-extensions/draft-ietf-httpbis-retrofit.html) into new
@@ -37,7 +37,7 @@ shapes that would be compatible with it.
3737
> While this package parses and serializes HTTP field value, it does not validate its content
3838
> against any conformance rule out of the box. You are still required to perform such a
3939
> compliance check against the constraints of the corresponding field. While Content
40-
> validation is still possible and higly encouraged when using this library. Because
40+
> validation is still possible and highly encouraged when using this library. Because
4141
> of the wide variety of HTTP fields it can not be made mandatory.
4242
4343
## System Requirements
@@ -49,7 +49,7 @@ shapes that would be compatible with it.
4949
Use composer:
5050

5151
```
52-
composer require bakame/http-structured-fields
52+
composer require bakame/http-structured-fields:^2.0
5353
```
5454

5555
## Using the package

docs/01-basic-usage.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ order: 2
88
## Parsing the Field
99

1010
The first way to use the package is to enable HTTP header or HTTP trailer parsing. We will refer to them
11-
as fields for the rest of the documentation as it is how they are named in the IETF RFC.
11+
as HTTP fields for the rest of the documentation as it is how they are named in the IETF RFC.
1212

1313
Let's say we want to parse the [Permissions-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Permissions-Policy#syntax) field. The first thing to know
1414
is that each structured field is defined against one specific data type which is
@@ -37,8 +37,7 @@ $permissions->isEmpty(); // returns false the dictionary c
3737
```
3838

3939
> [!WARNING]
40-
> If parsing fails a `SyntaxError` exception is thrown with the information about why the conversion
41-
> could not be achieved.
40+
> If parsing fails a `SyntaxError` exception is thrown with the information about why it failed.
4241
4342
## Creating a new field
4443

@@ -54,11 +53,14 @@ echo DataType::Dictionary->serialize([
5453
// returns picture-in-picture=(), geolocation=(self "https://example.com/"), camera=*
5554
```
5655

57-
Again, we start from the knowledge that the field is a `Dictionary`, and by default content is added using
58-
tuple or pairs. As such we can turn the iterable construct we have into a proper HTTP field text
59-
representation by applying the serialization mechanism described in the RFC. While field building
60-
may look overwhelming, at first, it follows a fully described and tested process that the package
61-
can simplify for you once you read the documentation.
56+
Again, we start from the knowledge that the field is a `Dictionary`, content is added using
57+
pairs to respect value position. As such we can turn the iterable construct we have into a
58+
proper HTTP field text representation by applying the serialization mechanism described in
59+
the RFC.
60+
61+
While field building may look overwhelming, at first, it follows a fully described and tested
62+
process that the package can simplify for you once you read the documentation.
63+
6264
The goal of the example is to show that even without dwelling too much into the ins and out
6365
of the package you can easily and quickly parse or serialize compliant fields in PHP.
6466

docs/02-parsing-serializing.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ As example the following existing headers can be classified within the Structure
4747
These example are taken from a list of [already supported fields](https://httpwg.org/http-extensions/draft-ietf-httpbis-retrofit.html)
4848

4949
> [!NOTE]
50-
> This means that those headers are already supported when parsing or serializing them by the package
50+
> This means that all the headers listed are already parsable and/or serializable by the package
5151
5252
All the classes share the same methods for parsing the HTTP text representation of the field. They all use
5353
the `fromHttpValue` named constructor. This method will parse the field string representation and
@@ -97,7 +97,7 @@ construct can be summarized as follows:
9797
- `InnerList`and `Parameters` instances can only contain `Item`;
9898
- `OuterList` and `InnerList` members can only be accessed by their indices (ie: they are list);
9999
- `Dictionary` and `Parameters` members can also be accessed by their name (ie: they are ordered map);
100-
- `Item` and `InnerList` instancs can have a `Parameters` container attached to.
100+
- `Item` and `InnerList` instances can have a `Parameters` container attached to them.
101101
- `Item` contain in a `Parameters` container **can not have** parameters attached to them to avoid recursion. They are named **Bare Item**.
102102
- `Item` contain in a `InnerList` container **can have** parameters attached to them.
103103

@@ -142,8 +142,8 @@ $field[0]->parameterByName('q'); // returns null
142142
Each Data type implementation is able to convert itself into a proper HTTP text representation
143143
using its `toHttpValue` method. The method is complementary to the `fromHttpValue` named constructor
144144
in that it does the opposite of that method. Just like the `fromHttpValue`, it can take an optional
145-
`Ietf` enum to specifu which RFC version it should adhere to for serialization. And two syntactic
146-
methods `toRfc9651` and `toRfc8941` are also present to ease usage. Last but not least All classes
145+
`Ietf` enum to specify which RFC version it should adhere to for serialization. And two syntactic
146+
methods `toRfc9651` and `toRfc8941` are also present to ease usage. Last but not least, all classes
147147
implements the `Stringable` interface using the latest accepted RFC.
148148

149149
```php
@@ -158,7 +158,7 @@ $field->toHttpValue();
158158
The `toHttpValue` method applies by default all the normalizations recommended by the RFC.
159159

160160
> [!TIP]
161-
> This is the mechanism used by the `DataType::serialize` method. Once the Structured
162-
> field has been created, the method calls its `toHttpValue` method.
161+
> This is the mechanism used by the `DataType::serialize` method. Once the HTTP Structured
162+
> Field has been created, the method calls its `toHttpValue` method.
163163
164164
← [Basic Usage](01-basic-usage.md) | [Accessing Field Values](03-field-values.md) →

docs/03-field-values.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ echo Type::tryFromVariable(new SplTempFileObject()); // returns null
5454
```
5555

5656
To ease validation the `Type::equals` and `Type::isOneOf` methods are added to check if
57-
the variable is of expected type. It can also be used to compare types.
57+
the variable is one of the expected type. It can also be used to compare types.
5858

5959
```php
6060
use Bakame\Http\StructuredFields\Type;
@@ -149,9 +149,9 @@ use Bakame\Http\StructuredFields\Token;
149149
Item:new(DateTimeInterface|Byte|Token|DisplayString|string|int|array|float|bool $value): self
150150
Item:tryNew(mixed $value): ?self
151151
Item::fromDecodedBytes(Stringable|string $value): self;
152+
Item::fromEncodedBytes(Stringable|string $value): self;
152153
Item::fromEncodedDisplayString(Stringable|string $value): self;
153154
Item::fromDecodedDisplayString(Stringable|string $value): self;
154-
Item::fromEncodedBytes(Stringable|string $value): self;
155155
Item::fromToken(Stringable|string $value): self;
156156
Item::fromString(Stringable|string $value): self;
157157
Item::fromDate(DateTimeInterface $datetime): self;
@@ -169,14 +169,14 @@ To update the `Item` instance value, use the `withValue` method:
169169
```php
170170
use Bakame\Http\StructuredFields\Item;
171171

172-
Item::withValue(DateTimeInterface|Byte|Token|DisplayString|string|int|float|bool $value): static
172+
Item::withValue(DateTimeInterface|Byte|Token|DisplayString|string|int|float|bool $value): self
173173
```
174174

175175
### Item Parameters
176176

177-
Items can have parameters attached to them. A parameter is a bere item, an item which can not have parameters
177+
Items can have parameters attached to them. A parameter is a **bare item**, an item which can not have parameters
178178
attach to it, to avoid recursive behaviour. Parameters are grouped in an ordered map container called `Parameters`.
179-
They can be accessed by their indices **but also** by their required key attached to them.
179+
They can be accessed by their indices **but also** by their **required key** attached to them.
180180

181181
```php
182182

@@ -193,7 +193,7 @@ By default, you can access the member `Item` of a parameters using the following
193193
- `Item::parameterByName` returns the value of the bare item instance attached to the supplied `name`;
194194
- `Item::parameterByIndex` returns the value of the bare item instance attached to the supplied `index`;
195195

196-
It is possible to alter and modify the `Parameters` attached to an `Item` but this section
197-
will be explored in the next section about the containers.
196+
It is possible to alter and modify the `Parameters` attached to an `Item` but this will be explored in
197+
the next section about the containers.
198198

199199
← [Parsing and Serializing](02-parsing-serializing.md) | [Containers](04-containers.md) →

docs/04-containers.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ order: 5
55

66
# Working with Structured Fields Containers
77

8-
While building or updating a Bare Item is straightforward, doing the same with the structured field containers
8+
While building or updating a Bare Item is straightforward, doing the same with structured field containers
99
requires a bit more logic. In the following sections we will explore how we can access, build and update
1010
containers.
1111

@@ -65,7 +65,7 @@ unset($permissions['a']); // triggers a ForbiddenOperation exception
6565
> For ordered map the ArrayAccess interface will use the member name
6666
> whereas for lists the interface will use the member index.
6767
68-
The `Dictionary` and `Parameters` classes also allow accessing its members as value using their name:
68+
The `Dictionary` and `Parameters` classes also allow accessing their members as value using their name:
6969

7070
```php
7171
$permissions->hasName('picture-in-picture'); // returns true
@@ -87,13 +87,13 @@ $permissions->indexByName('geolocation'): // returns 1
8787
> The `getByName` method will throw an `InvalidOffset` exception if no member exists for the given `$offset`.
8888
8989
> [!TIP]
90-
> The `ArrayAccess` interface proxy the result from `getByIndex` with `OuterList` and `InnerList`.
91-
> The `ArrayAccess` interface proxy the result from `getByName` with `Dictionary` and `Parameters`.
90+
> The `ArrayAccess` interface proxy the result from `getByIndex` and `hasIndices` with `OuterList` and `InnerList`.
91+
> The `ArrayAccess` interface proxy the result from `getByName` and `hasNames` with `Dictionary` and `Parameters`.
9292
9393
### Accessing the parameters values
9494

9595
As we have already seen, it is possible to access the `Parameters` values directly
96-
from the `Item` instance. The same public API is used from the `InnerList`.
96+
from the `Item` instance. The same public API is used for the `InnerList`.
9797

9898
On the other hand if you already have a `Parameters` instance you can use the
9999
`valueByName` and `valueByIndex` methods to directly access the value from a single
@@ -103,7 +103,7 @@ parameter.
103103
> The `parameterByName` proxy the result from `valueByName`.
104104
> The `parameterByIndex` proxy the result from `valueByIndex`.
105105
106-
## Building and Updating COntainers
106+
## Building and Updating Containers
107107

108108
Every container can be used as a builder to create an HTTP field value. Because we are
109109
using immutable value objects any change to the value object will return a new instance
@@ -438,4 +438,18 @@ echo InnerList::new('foo', 'bar')
438438
// ("foo" "bar");expire=@1681538756;path="/";max-age=2500
439439
```
440440

441+
Last but not least, all datatypes exposes the conditional `when` method to improve building the structured field.
442+
This can be helpful if fdr instance your input value would otherwise trigger an exception.
443+
444+
In the example below we are conditionally building the an `Item` depending on the data found in the
445+
`$cache` value object. This is also possible for all containers.
446+
447+
```php
448+
Item::new($cache->name)
449+
->when($cache->hit, fn (Item $item) => $item->appendParameter('hit', $cacher->hit))
450+
->when(null !== $cache->ttl, fn (Item $item) => $item->appendParameter('ttl', $cache->ttl))
451+
->when(null !== $cache->key, fn (Item $item) => $item->appendParameter('key', $cache->key))
452+
->when(null !== $cache->detail, fn (Item $item) => $item->appendParameter('detail', $cache->detail));
453+
```
454+
441455
← [Accessing Field Values](03-field-values.md) | [Validation](05-validation) →

0 commit comments

Comments
 (0)