Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Zend Hydrator Laravel Package

zendframework/zend-hydrator

Zend Hydrator provides strategies and tools to extract data from objects and hydrate objects from arrays in PHP. Supports naming strategies, custom hydrators, and flexible configuration, useful for forms, APIs, and domain model mapping.

View on GitHub
Deep Wiki
Context7

Getting Started

This package provides hydrators for converting between arrays and objects — crucial for data transfer (e.g., form input → domain objects, Doctrine entities → API responses). Despite being archived and last updated in 2019, it remains stable and compatible with modern Laravel via Composer (via zendframework/zend-hydrator or its successor laminas/laminas-hydrator). Start by installing it:

composer require laminas/laminas-hydrator

Use the built-in hydrators:

  • ArraySerializable: For classes implementing ArraySerializable
  • ClassMethods: getter/setter-based (default convention: getFoo()/setFoo())
  • DelegateBased: Use custom extraction/dehydration callbacks
  • ObjectProperty: Direct property access (less common in Laravel models)

Minimal example:

use Laminas\Hydrator\ClassMethods;

$hydrator = new ClassMethods();
$entity = new User(); // with getUserName(), setUserName()
$data = $hydrator->extract($entity); // ['user_name' => 'Jane']
$newEntity = $hydrator->hydrate($data, new User());

Implementation Patterns

In Laravel, common use cases include:

  • API Resource Hydration: Convert incoming JSON requests into Eloquent models or DTOs before validation.
    // In a custom FormRequest or Service
    $hydrator = new ClassMethods();
    $user = $hydrator->hydrate($request->all(), new User());
    $user->save();
    
  • DTO <-> Database Mapping: Use hydrators in repository or service layers to isolate persistence concerns.
  • Custom Extraction for API Responses: Extract only needed fields (e.g., with ClassMethods(true) to use underscore-case names for array keys matching Laravel’s naming).
  • Hydrator Delegates: Combine hydrators for nested structures:
    $hydrator->addDelegate('posts', new ClassMethods());
    
  • Integration with Laravel Collections: Pipe extracted arrays into collections for further processing:
    $data = collect($hydrator->extract($entities))->map(...);
    

Gotchas and Tips

  • Underscore vs. CamelCase: ClassMethods uses lowercase with underscores (e.g., first_name) by default. Set constructor flag true to preserve camelCase (new ClassMethods(true)). Ensure this matches your API/DB conventions.
  • Legacy Package Name: This package is archived. Use laminas/laminas-hydrator (Laminas Project fork) to avoid autoloader warnings. Update use Zend\...use Laminas\....
  • Eloquent Compatibility: Eloquent models don’t follow strict getter/setter patterns — ClassMethods may miss attributes. Prefer ArraySerializable or custom hydrators for models.
  • Performance: Avoid instantiating hydrators on every request. Register as singleton or inject via Laravel’s container:
    // In a Service Provider
    $this->app->singleton(\Laminas\Hydrator\ClassMethods::class, fn () => new ClassMethods(true));
    
  • Debugging: Use extract($object, []) or hydrate([], $object) to test hydrator behavior.
  • Extension: Extend ClassMethods to override naming strategies or add custom logic:
    class AppClassMethods extends ClassMethods
    {
        protected function extractByName($object, $propertyName, $extraFields = [])
        {
            // Custom logic: e.g., camelCase-to-snake_case conversion
            return parent::extractByName($object, $propertyName, $extraFields);
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport