Installation:
composer require eag/easy-hydrator
No additional configuration is required unless using Symfony bundles (add to config/bundles.php if applicable).
First Use Case: Hydrate a simple array into a DTO/Value Object:
use EAG\EasyHydrator\ArrayToValueObjectHydrator;
$hydrator = new ArrayToValueObjectHydrator();
$person = $hydrator->hydrate([
'name' => 'John Doe',
'age' => 30,
'metAt' => '2023-01-01T12:00:00+00:00'
], Person::class);
ArrayToValueObjectHydrator (core hydrator).Dependency Injection:
Bind the hydrator in Laravel’s service container (e.g., AppServiceProvider):
$this->app->bind(ArrayToValueObjectHydrator::class, fn() => new ArrayToValueObjectHydrator());
Use it in repositories/services:
public function __construct(private ArrayToValueObjectHydrator $hydrator) {}
Hydration Pipeline:
$user = $this->hydrator->hydrate($request->all(), User::class);
Nested Objects: Automatically hydrates nested arrays if the target class has typed properties:
class Address {
public function __construct(public string $street, public string $city) {}
}
class User {
public function __construct(public string $name, public Address $address) {}
}
$user = $hydrator->hydrate([
'name' => 'Alice',
'address' => ['street' => '123 Main', 'city' => 'Metropolis']
], User::class);
Custom Object Creation:
Override default behavior via setObjectFactory:
$hydrator->setObjectFactory(fn(string $class) => new $class(...));
$validated = $request->validate([...]);
$model = $hydrator->hydrate($validated, CreateUserRequest::class);
return response()->json($hydrator->hydrate($data, UserResponse::class));
$hydrator->enableCache();
Case Sensitivity:
Array keys must match constructor parameter names exactly (case-sensitive).
Fix: Use array_change_case or normalize keys before hydrating.
Unsupported Types:
DateTimeInterface).
Fix: Implement __toString() or a custom type mapper.setPropertyMapper for dynamic properties.Circular References:
Nested objects with circular references (e.g., User->Address->User) will fail.
Fix: Break cycles or use lazy loading.
Symfony Bundle Overhead:
The EasyHydratorBundle adds minimal value for Laravel. Skip it unless using Symfony components.
$hydrator->setStrictMode(true); // Throws exceptions on missing keys/types.
hydrate() return values for null (indicates failure). Use try-catch:
try {
$obj = $hydrator->hydrate($data, User::class);
} catch (HydrationException $e) {
Log::error($e->getMessage());
}
TypeMapper for unsupported types (e.g., UUIDs):
$hydrator->setTypeMapper(new CustomTypeMapper());
$hydrator->setPropertyMapper(fn(string $property, $value) => "custom_$property");
$hydrator->disableCache();
$this->app->tag(ArrayToValueObjectHydrator::class, ['hydrators']);
$mockHydrator = Mockery::mock(ArrayToValueObjectHydrator::class)
->shouldReceive('hydrate')
->once()
->andReturn(new User(...));
$hydrator = app(ArrayToValueObjectHydrator::class);
foreach ($csvData as $row) {
$model = $hydrator->hydrate($row, Product::class);
$model->save();
}
How can I help you explore Laravel packages today?