/php-api-hydrator

A PHP7 library for (de-) hydrating objects using api descriptions.

Primary LanguagePHPGNU Lesser General Public License v3.0LGPL-3.0

KleijnWeb\PhpApi\Hydrator

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version

A small PHP7 library for hydrating objects using api descriptions. Hydrating in this context refers to creating fully initialized typed objects from input consisting of arrays and instances of stdClass.

Works with OpenAPI 2.0 (FKA Swagger), RAML 1.0 support is in development.

Relies on functionality provided by KleijnWeb\PhpApi\Descriptions.

Usage

Converting from stdClass|stdClass[] to typed objects matching your API description on hydration, the reverse on dehydration:

$schema = $description->getPath('/foo')->getOperation('post')->getRequestSchema();
$hydrator = new ObjectHydrator(new ClassNameResolver(['A\\NameSpace\\Somewhere']));
$typedObjects = $hydrator->hydrate($input, $schema);
$output = $hydrator->dehydrate($typedObjects, $schema);
// $input == $output && $input !== $output

You can also technically use (abuse?) the hydrator to cast scalar values: it will accept any type of Schema.

DateTime

By default will toss strings in date and date-time format into the DateTime constructor, and lets it figure out how to parse. When serializing it uses Y-m-d\TH:i:s.uP.

The expected in- and output format can be tweaked by injecting a custom instance of DateTimeSerializer:

$hydrator = new ObjectHydrator(new ClassNameResolver(['A\\NameSpace\\Somewhere']), new DateTimeSerializer(\DateTime::RFC850);

Contributing

Pull requests are very welcome, but the code has to be PSR2 compliant, follow used conventions concerning parameter and return type declarations, and the coverage can not go below 100%.

License

KleijnWeb\PhpApi\Hydrator is made available under the terms of the LGPL, version 3.0.