-
-
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
96 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,82 @@ | ||
# Extension | ||
|
||
## Everything is final | ||
|
||
As of now everything is done within the package. But what about interacting with the | ||
PHP ecosystem as a whole. 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 should simply not implement or rely on that 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(): Item|InnerList|OuterList|Dictionary|Parameters; | ||
} | ||
``` | ||
|
||
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 ServiceProvider | ||
{ | ||
public function __construct( | ||
public string $value; | ||
public float $quality = 1.0; | ||
) {} | ||
|
||
public function toStructuredField(): Item | ||
{ | ||
return Iten::fromToken($value) | ||
->withParaneters( | ||
Parameter::new() | ||
->append('q', $this->quality), | ||
->filter(fn (array $pair): bool => $par[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. | ||
|
||
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. | ||
|
||
← [Containers](06-validation.md) |