aljerom / simple-hydrator
Reflection-based array-to-object hydrator with snake_case to camelCase mapping
v1.0.5
2026-11-06 15:44 UTC
Requires
- php: >=8.3
Requires (Dev)
- phpunit/phpunit: ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-23 01:01:04 UTC
README
Reflection-based array-to-object hydrator with snake_case to camelCase key mapping.
Installation
composer require aljerom/simple-hydrator
Usage
use Aljerom\SimpleHydrator\SimpleHydrator; class AddressDto { public string $city; public string $country; } class UserDto { public string $name; public DateTimeInterface $createdAt; public array $tags; public AddressDto $address; } $data = [ 'name' => 'Alice', 'created_at' => '2024-01-01 00:00:00', 'tags' => '["php","oop"]', // JSON strings are decoded 'address' => ['city' => 'Berlin', 'country' => 'DE'], // nested objects are hydrated recursively ]; $user = (new SimpleHydrator())->hydrate($data, UserDto::class);
Behavior
- Instantiates the target class without calling its constructor (promoted property defaults are preserved as a workaround for php#81386).
- Converts
snake_case(andUpperCamelCase) keys tocamelCaseproperty names. - Converts strings to
DateTimeImmutableforDateTimeInterfaceproperties;nullbecomes epoch0, nullable properties acceptnull. - Decodes JSON strings for
arrayproperties. - Hydrates class-typed properties recursively.
nullpassed for a non-nullable property keeps the property's default value.- Unknown keys in the input array are ignored.
Requirements
- PHP >= 8.3
Testing
composer install vendor/bin/phpunit
License
MIT