-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the extension page to the documentation
- Loading branch information
Showing
8 changed files
with
116 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Interacting with PHP ecosystem | ||
|
||
All Datatype expose the `Stringable` interface while it is recommended to use | ||
the `toHttpValue` method for better granularity. Supporting the `Stringable` | ||
interface allows the package to easily interface with packages and frameworks | ||
which expects a string or a stringable object when adding or updating | ||
HTTP field values. | ||
|
||
```php | ||
$container = InnerList::new(ByteSequence::fromDecoded('Hello World'), 42.0, 42); | ||
|
||
$container->toHttpValue(); // returns '(:SGVsbG8gV29ybGQ=: 42.0 42)' | ||
echo $container; // returns '(:SGVsbG8gV29ybGQ=: 42.0 42)' | ||
``` | ||
|
||
## Everything is final | ||
|
||
As a rule of thumb all the classes from this package are final and immutable and | ||
expose no particular interface. | ||
|
||
> [!IMPORTANT] | ||
> The `StructuredField` interface should be seen as an internal implementation detail | ||
> and should not be implemented outside the package. While PHP does not prohibit such | ||
> action, **you MUST NOT implement the `StructuredField` interface.** | ||
The reason for disallowing the `StructuredField` interface is simple. We want to ensure | ||
that in any situation the RFC is being respected. The contract is between the RFC and your | ||
code the package only acts as a link between both parties. | ||
|
||
## Closed for extension opened for composition | ||
|
||
On the other hand to allow composition the package does expose the `StructuredFieldProvider` interface. | ||
|
||
```php | ||
interface StructuredFieldProvider | ||
{ | ||
/** | ||
* Returns an object implementing the StructuredField interface. | ||
*/ | ||
public function toStructuredField(): StructuredField; | ||
} | ||
``` | ||
|
||
This interface should return one of the DataType class and this is the interface that needs | ||
to be implemented. All the containers are able to work with a `StructuredFieldProvider`. | ||
|
||
Imagine you have an `AcceptHeaderItem` class and you want to take advantage of the package. You | ||
only have to implemente the `StructuredFieldProvider`. Once done, your class will be able to | ||
work with the `OuterList` class. | ||
|
||
```php | ||
class readonly AcceptHeaderItem | ||
{ | ||
public function __construct( | ||
public string $value; | ||
public float $quality = 1.0; | ||
) {} | ||
} | ||
``` | ||
|
||
To use the package you will have to add the missing interface | ||
|
||
```php | ||
class readonly AcceptHeaderItem implements StructuredFieldProvider | ||
{ | ||
public function __construct( | ||
public string $value, | ||
public float $quality = 1.0 | ||
) {} | ||
|
||
public function toStructuredField(): StructuredField | ||
{ | ||
return Item::fromToken($this->value) | ||
->withParameters( | ||
Parameters::new() | ||
->append('q', $this->quality) | ||
->filter(fn (array $pair): bool => $pair[0] !== 'q' || 1.0 !== $pair[1]->value()) | ||
); | ||
} | ||
} | ||
``` | ||
|
||
Now this class can be used with the `OuterList` class to properly serialize an `Accept` header | ||
as mandated by the RFC. | ||
|
||
```php | ||
$json = new AcceptHeaderItem('application/json'); | ||
$csv = new AcceptHeaderItem('text/csv', 0.7); | ||
|
||
echo OuterList::new($json, $csv); | ||
//returns application/json, text/csv;q=0.7 | ||
``` | ||
|
||
In the example provided we added the interface on the class itself but of course you are free to use | ||
a different approach, as long as you end up have a class that implements the `StructuredFieldProvider` | ||
contract. | ||
|
||
To show how this can be achieved you can check the codebase from [HTTP Cache Status](https://github.com/bakame-php/http-cache-status) | ||
which uses the interface. Of note by using this interface you can completely hide the presence of | ||
this package to your end users if needed. | ||
|
||
← [Validation](06-validation.md) |