PHP code example of azjezz / input-hydrator

1. Go to this page and download the library: Download azjezz/input-hydrator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

azjezz / input-hydrator example snippets


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;

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;