1. Go to this page and download the library: Download drabek-digital/hydra 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/ */
drabek-digital / hydra example snippets
declare(strict_types=1);
use DrabekDigital\Hydra\Caching\FileCache;
use DrabekDigital\Hydra\Caching\Psr16CacheAdapter;
use DrabekDigital\Hydra\Dehydrator;
use DrabekDigital\Hydra\Hydrator;
use DrabekDigital\Hydra\Exceptions\HydrationFailure;
use DrabekDigital\Hydra\Exceptions\DehydrationFailure;
use DrabekDigital\Hydra\Exceptions\ValidationFailure;
use DrabekDigital\Hydra\Validator;
use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
// Option A: built-in file cache (use a dedicated/private directory, not a shared world-writable temp path)
$cache = new FileCache(sys_get_temp_dir());
// Option B: any PSR-16 cache implementation from your DI container/framework
/** @var Psr16CacheInterface $psr16 */
$psr16 = $container->get(Psr16CacheInterface::class);
$cache = new Psr16CacheAdapter($psr16);
// Hydrating from array to object
$hydrator = new Hydrator($cache);
try {
$object = $hydrator->hydrate($input, MyDTO::class);
$object = $hydrator->hydrateArray($input, discriminator: 'type', mapping: [
'type1' => Type1::class,
'type2' => Type2::class,
]);
} catch (HydrationFailure $e) {
// handling
}
// Dehydration (excluding validation)
$dehydrator = new Dehydrator($cache);
try {
$output = $dehydrator->dehydrate($object);
$output = $dehydrator->dehydrateArray($arrayOfObjects);
} catch (DehydrationFailure $e) {
// handling
}
// Validation
$validator = new Validator();
try {
$validator->validate($object);
} catch (ValidationFailure $e) {
// handling
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.