laminas/laminas-hydrator
Laminas Hydrator provides flexible tools to hydrate and extract data between arrays and objects. Includes hydrator strategies, naming conventions, and integration helpers for forms and domain models, supporting multiple hydrator implementations and extensions.
laminas-hydrator excels in DTO (Data Transfer Object) mapping, form handling, and API serialization/deserialization—core needs for Laravel applications dealing with complex data flows (e.g., API requests/responses, database hydration, or form submissions).Arrayable, Jsonable, or Fillable traits, this package offers fine-grained control over hydration/extraction logic (e.g., custom strategies, nested objects, or conditional filtering). It integrates seamlessly with Laravel’s service container and dependency injection.stdClass/array and Eloquent models/DTOs.Illuminate\Container via Psr\Container adapter (used by Laminas).AggregateHydrator supports event listeners, alignable with Laravel’s event system (though manual wiring may be needed).array_map/array_reduce loops with declarative hydrators (e.g., ObjectPropertyHydrator for Eloquent models).collect() or array_walk_recursive can coexist during transition.laminas-eventmanager, laminas-serializer, and laminas-servicemanager if using advanced features (e.g., AggregateHydrator or SerializableStrategy). Core functionality is self-contained.StrategyInterface) require understanding of Laminas’ design, which may differ from Laravel’s conventions.null where object is expected).ReflectionHydrator uses PHP reflection, which can be slower than manual property access. Benchmark against Laravel’s native array_merge or collect() for critical paths.AggregateHydrator and lazy loading).ObjectProperty → ObjectPropertyHydrator) require refactoring if using v2. Laravel’s autoloader will handle aliases until v4.ClassMethods) are deprecated; enforce new names in CI early.ArraySerializableHydrator for APIs vs. ClassMethodsHydrator for forms).->toArray() on Eloquent).fill() operations).AppServiceProvider:
$this->app->bind(HydratorInterface::class, function ($app) {
return new ObjectPropertyHydrator();
});
ArraySerializableHydrator for JsonResource or ApiResource transformations:
$hydrator = app(HydratorInterface::class);
$data = $hydrator->extract($model);
->only()/->except() with FilterEnabledInterface:
$hydrator = new ObjectPropertyHydrator();
$hydrator->addFilter('allowList', new AllowListFilter(['id', 'name']));
$data = $hydrator->extract($request->all());
$hydrator = new ClassMethodsHydrator();
$props = $hydrator->hydrate($request->input(), new UserProps());
HydratorAwareInterface for test doubles:
$mockHydrator = Mockery::mock(HydratorInterface::class);
$service->setHydrator($mockHydrator);
->toArray() calls with ArraySerializableHydrator.ObjectPropertyHydrator for Eloquent model hydration.AggregateHydrator for complex nested objects.User::create($request->all()) with:
$user = new User();
$hydrator->hydrate($request->validated(), $user);
$user->save();
Doctrine\Common\Persistence\ObjectManager for hydration where possible to avoid redundancy.HydratorAwareTrait for services needing hydrator access:
use Laminas\Hydrator\HydratorAwareTrait;
class UserService {
use HydratorAwareTrait;
public function update(User $user, array $data) {
$this->getHydrator()->hydrate($data, $user);
}
}
ObjectPropertyHydrator for Eloquent models.->toArray() with ArraySerializableHydrator in API responses.AggregateHydrator for nested objects (e.g., User with Address).DateTimeStrategy).array_walk loops with hydrator equivalents.array_map calls.AggregateHydrator events for observability:
$hydrator->getEventManager()->attach(
'extract',
[$logger, 'logExtractionEvent']
);
How can I help you explore Laravel packages today?