Skip to content

Commit b475a9e

Browse files
committed
Improve documentation structure
1 parent 7bc1df2 commit b475a9e

File tree

7 files changed

+105
-109
lines changed

7 files changed

+105
-109
lines changed

docs/00-intro.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ composer require bakame/http-structured-fields
5656

5757
- [Basic usage](01-basic-usage.md)
5858
- [Parsing and Serializing](02-parsing-serializing.md)
59-
- [Structured Field Value Types](03-value-types.md)
60-
- [The Item Data Type](04-item.md)
61-
- [The Structured Field Containers](05-containers.md)
62-
- [HTTP Fields Validation](06-validation.md)
59+
- [Accessing The Field Values](03-field-values.md)
60+
- [Working with the Containers Data Type](04-containers.md)
61+
- [Structured Field Validation](05-validation.md)
6362
- [Interacting with the PHP Ecosystem](07-extensions.md)

docs/02-parsing-serializing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,4 @@ The `toHttpValue` method applies by default all the normalizations recommended b
161161
> This is the mechanism used by the `DataType::serialize` method. Once the Structured
162162
> field has been created, the method calls its `toHttpValue` method.
163163
164-
← [Basic Usage](01-basic-usage.md) | [Value Types](03-value-types.md) →
164+
← [Basic Usage](01-basic-usage.md) | [Accessing Field Values](03-field-values.md) →

docs/03-value-types.md renamed to docs/03-field-values.md

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
title: HTTP Fields value types
2+
title: Accessing the HTTP field values
33
order: 4
44
---
55

6-
# Structured Fields Values
6+
# HTTP Fields Values
77

88
## Value type conversion to PHP
99

@@ -121,4 +121,79 @@ $displayString->type(); // returns Type::DisplayString
121121
> Values are not directly accessible. They can only be retrieved from an Item
122122
> Data type.
123123
124-
← [Parsing and Serializing](02-parsing-serializing.md) | [Item](04-item.md) →
124+
## The Item Data Type
125+
126+
This is the structure from which you will be able to access the actual field content.
127+
128+
### Item value
129+
130+
The eight (8) defined value types are all attached to an `Item` object where their value and
131+
type are accessible using the following methods:
132+
133+
```php
134+
use Bakame\Http\StructuredFields\Item;
135+
136+
$item = Item::fromHttpValue('@1234567890');
137+
$item->type(); // return Type::Date;
138+
$item->value() // return the equivalent to DateTimeImmutable('@1234567890');
139+
```
140+
141+
The `Item` value object exposes the following named constructors to instantiate
142+
bare items (ie: item without parameters attached to them).
143+
144+
```php
145+
use Bakame\Http\StructuredFields\Byte;
146+
use Bakame\Http\StructuredFields\Item;
147+
use Bakame\Http\StructuredFields\Token;
148+
149+
Item:new(DateTimeInterface|Byte|Token|DisplayString|string|int|array|float|bool $value): self
150+
Item:tryNew(mixed $value): ?self
151+
Item::fromDecodedByteSequence(Stringable|string $value): self;
152+
Item::fromEncodedDisplayString(Stringable|string $value): self;
153+
Item::fromDecodedDisplayString(Stringable|string $value): self;
154+
Item::fromEncodedByteSequence(Stringable|string $value): self;
155+
Item::fromToken(Stringable|string $value): self;
156+
Item::fromString(Stringable|string $value): self;
157+
Item::fromDate(DateTimeInterface $datetime): self;
158+
Item::fromDateFormat(string $format, string $datetime): self;
159+
Item::fromDateString(string $datetime, DateTimeZone|string|null $timezone = null): self;
160+
Item::fromTimestamp(int $value): self;
161+
Item::fromDecimal(int|float $value): self;
162+
Item::fromInteger(int|float $value): self;
163+
Item::true(): self;
164+
Item::false(): self;
165+
```
166+
167+
To update the `Item` instance value, use the `withValue` method:
168+
169+
```php
170+
use Bakame\Http\StructuredFields\Item;
171+
172+
Item::withValue(DateTimeInterface|Byte|Token|DisplayString|string|int|float|bool $value): static
173+
```
174+
175+
### Item Parameters
176+
177+
Items can have parameters attached to them. A parameter is a bere item, an item which can not have parameters
178+
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.
180+
181+
```php
182+
183+
$item = Item::fromHttpValue('application/xml;q=0.9;foobar');
184+
$item->value()->toString(); // returns 'application/xhtml+xml'
185+
$item->parameterByName(name: 'q', default: 1.0); // returns 1.0 if the parameter is not defined
186+
$item->parameterByIndex(index: 1, default: ['toto', false]); // returns ['foobar', true] because there's a parameter at index 1
187+
$item->parameters(); // returns a Parameters instance.
188+
```
189+
190+
By default, you can access the member `Item` of a parameters using the following methods:
191+
192+
- `Item::parameters` returns a `Parameters` instance;
193+
- `Item::parameterByName` returns the value of the bare item instance attached to the supplied `name`;
194+
- `Item::parameterByIndex` returns the value of the bare item instance attached to the supplied `index`;
195+
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.
198+
199+
← [Parsing and Serializing](02-parsing-serializing.md) | [Containers](04-containers.md) →

docs/05-containers.md renamed to docs/04-containers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,4 @@ echo InnerList::new('foo', 'bar')
428428
// ("foo" "bar");expire=@1681538756;path="/";max-age=2500
429429
```
430430

431-
← [Item](04-item.md) | [Validation](06-validation.md) →
431+
← [Accessing Field Values](03-field-values.md) | [Validation](05-validation) →

docs/04-item.md

Lines changed: 0 additions & 81 deletions
This file was deleted.

docs/06-validation.md renamed to docs/05-validation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,4 @@ class it becomes easier to reuse it to validate your data.
432432
433433
To show how this can be achieved you can check the codebase from [HTTP Cache Status](https://github.com/bakame-php/http-cache-status)
434434

435-
← [Containers](05-containers.md) | [Extending the package functionalities](07-extensions.md)
435+
← [Containers](04-containers.md) | [Extending the package functionalities](07-extensions.md)

docs/07-extensions.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
---
2-
title: Interacting with PHP ecosystem
2+
title: Interacting with the PHP ecosystem
33
order: 8
44
---
55

6-
# Interacting with PHP ecosystem
6+
# Interacting with the PHP ecosystem
7+
8+
## Everything is final
9+
10+
As a rule of thumb all the classes from this package are final and immutable and
11+
expose no particular interface.
12+
13+
The reason is to ensure that in any situation the RFC is being respected. The contract
14+
is between the RFC and your code the package only acts as a link between both parties.
15+
16+
## DataTypes are Stringable
717

818
All Datatypes expose the `Stringable` interface while it is recommended to use
9-
the `toHttpValue` method for better granularity. Supporting the `Stringable`
19+
the `toHttpValue` method for better granularity but supporting the `Stringable`
1020
interface allows the package to easily interface with packages and frameworks
1121
which expects a string or a stringable object when adding or updating
1222
HTTP field values.
@@ -18,17 +28,10 @@ $container->toHttpValue(); // returns '(:SGVsbG8gV29ybGQ=: 42.0 42)'
1828
echo $container; // returns '(:SGVsbG8gV29ybGQ=: 42.0 42)'
1929
```
2030

21-
## Everything is final
22-
23-
As a rule of thumb all the classes from this package are final and immutable and
24-
expose no particular interface.
25-
26-
The reason is to ensure that in any situation the RFC is being respected. The contract
27-
is between the RFC and your code the package only acts as a link between both parties.
28-
2931
## Closed for extension opened for composition
3032

31-
On the other hand to allow composition the package does expose the `StructuredFieldProvider` interface.
33+
While the DataTypes can not be extended, to allow composition, the package exposes
34+
the `StructuredFieldProvider` interface.
3235

3336
```php
3437
interface StructuredFieldProvider
@@ -40,11 +43,11 @@ interface StructuredFieldProvider
4043
}
4144
```
4245

43-
This interface should return one of the DataType class and this is the interface that needs
44-
to be implemented. All the containers are able to work with a `StructuredFieldProvider`.
46+
This interface should return one of the DataType instance and all the containers are able to work
47+
with object that implement the interface.
4548

46-
Imagine you have an `AcceptHeaderItem` class, and you want to take advantage of the package. You
47-
only have to implement the `StructuredFieldProvider`. Once done, your class will be able to
49+
As an example, imagine you have an `AcceptHeaderItem` class, and you want to take advantage of the package.
50+
You will have to implement the `StructuredFieldProvider`. Once done, your class will be able to
4851
work with the `OuterList` class.
4952

5053
```php
@@ -91,11 +94,11 @@ echo OuterList::new($json, $csv);
9194
```
9295

9396
In the example provided we added the interface on the class itself but of course you are free to use
94-
a different approach, as long as you end up have a class that implements the `StructuredFieldProvider`
97+
a different approach, as long as you end up having a class that implements the `StructuredFieldProvider`
9598
contract.
9699

97100
To show how this can be achieved you can check the codebase from [HTTP Cache Status](https://github.com/bakame-php/http-cache-status)
98101
which uses the interface. Of note by using this interface you can completely hide the presence of
99102
this package to your end users if needed.
100103

101-
← [Validation](06-validation.md)
104+
← [Validation](05-validation.md)

0 commit comments

Comments
 (0)