Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/azjezz/input-hydrator
🧱 Hydrates input DTOs from request data.
https://github.com/azjezz/input-hydrator
Last synced: 13 days ago
JSON representation
🧱 Hydrates input DTOs from request data.
- Host: GitHub
- URL: https://github.com/azjezz/input-hydrator
- Owner: azjezz
- License: mit
- Created: 2020-10-31T05:08:19.000Z (about 4 years ago)
- Default Branch: develop
- Last Pushed: 2020-11-01T04:02:40.000Z (about 4 years ago)
- Last Synced: 2024-10-08T18:23:45.921Z (about 1 month ago)
- Language: PHP
- Homepage:
- Size: 26.4 KB
- Stars: 32
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Input Hydrator
![Unit tests status](https://github.com/azjezz/input-hydrator/workflows/unit%20tests/badge.svg?branch=develop)
![Static analysis status](https://github.com/azjezz/input-hydrator/workflows/static%20analysis/badge.svg?branch=develop)
![Security analysis status](https://github.com/azjezz/input-hydrator/workflows/security%20analysis/badge.svg?branch=develop)
![Coding standards status](https://github.com/azjezz/input-hydrator/workflows/coding%20standards/badge.svg?branch=develop)
[![TravisCI Build Status](https://travis-ci.com/azjezz/input-hydrator.svg?branch=develop)](https://travis-ci.com/azjezz/input-hydrator)
[![Coverage Status](https://coveralls.io/repos/github/azjezz/input-hydrator/badge.svg?branch=develop)](https://coveralls.io/github/azjezz/input-hydrator?branch=develop)
[![Type Coverage](https://shepherd.dev/github/azjezz/input-hydrator/coverage.svg)](https://shepherd.dev/github/azjezz/input-hydrator)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fazjezz%2Finput-hydrator%2Fdevelop)](https://dashboard.stryker-mutator.io/reports/github.com/azjezz/input-hydrator/develop)
[![Total Downloads](https://poser.pugx.org/azjezz/input-hydrator/d/total.svg)](https://packagist.org/packages/azjezz/input-hydrator)
[![Latest Stable Version](https://poser.pugx.org/azjezz/input-hydrator/v/stable.svg)](https://packagist.org/packages/azjezz/input-hydrator)
[![License](https://poser.pugx.org/azjezz/input-hydrator/license.svg)](https://packagist.org/packages/azjezz/input-hydrator)Input hydrator is a simple hydrator made for the sole purpose of hydrating data-transfer input objects.
## Installation
```console
$ composer require azjezz/input-hydrator
```## Example:
```php
use AzJezz\Input;final class Search implements Input\InputInterface
{
public string $query;
}/**
* @var Search $search
*/
$search = (new Input\Hydrator())->hydrate(Search::class, $_GET);print $search->query;
```While hydrating objects, some exceptions might be thrown:
- `AzJezz\Input\Exception\TypeException`: this exception should result in 500 HTTP status code,
as it represents an issue within the input class itself. such as the usage of a non-supported type,
or missing type for a specific property.- `AzJezz\Input\Exception\BadInputException`: this exception should result in a 400 HTTP status code,
as it means that the supplied request data doesn't match the input DTO structure.
Currently, Input-Hydrator is limited to a small set of types:
- `scalar` ( `string`, `int`, `float`, and `bool` )
- `null`
- *any object that implements `AzJezz\Input\InputInterface`*Union types are supported for PHP >= 8.0, for example:
```php
use AzJezz\Input;final class Filter implements Input\InputInterface
{
public ?int $maximumPrice;
public ?int $minimumPrice;
}final class Search implements Input\InputInterface
{
public string $query;
public null|Filter|string $filter = null;
}/**
* $filter is optional, and is missing from the request, therefore it's gonna contain the default value.
*
* @var Search $search
*/
$search = (new Input\Hydrator())->hydrate(Search::class, [
'query' => 'hello'
]);/**
* $search->filter is now an instance of `Filter`
*
* @var Search $search
*/
$search = (new Input\Hydrator())->hydrate(Search::class, [
'query' => 'hello',
'filter' => [
'maximum_price' => 1000,
'minimum_price' => 10, // the field is optional ( nullable ), so we can remove this line.
]
]);/**
* $search->filter is now a string
*
* @var Search $search
*/
$search = (new Input\Hydrator())->hydrate(Search::class, [
'query' => 'hello',
// this is okay as the `null|Filter|string` union contains `string`
'filter' => 'maximum_price=1000&minimum_price=10',
]);print $search->query;
```## License
The MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information.