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

Laminas Hydrator Laravel Package

laminas/laminas-hydrator

Laminas Hydrator provides flexible strategies to hydrate and extract data between objects and arrays. Supports reflection, class methods, naming strategies, and custom hydrators, making it easy to map entities, DTOs, and forms cleanly across your application.

View on GitHub
Deep Wiki
Context7

Hydrator

Available since version 3.1.0

The HydratorStrategy can be used to hydrate an object and its child objects with data from a nested array and vice versa.

Basic usage

The following code example shows standalone usage without adding the strategy to a hydrator.

Create and configure strategy

Create the strategy and set a hydrator and a classname for the handled object.

$strategy = new Laminas\Hydrator\Strategy\HydratorStrategy(
    new Laminas\Hydrator\ObjectPropertyHydrator(),
    stdClass::class
);

Hydrate data

$hydrated = $strategy->hydrate([
    'firstName' => 'David',
    'lastName'  => 'Bowie',
]);

echo $hydrated->firstName; // 'David'
echo $hydrated->lastName; // 'Bowie'

Extract data

$class            = new stdClass();
$class->firstName = 'David';
$class->lastName  = 'Bowie';

$extracted = $strategy->extract($class);

var_dump($extracted); // ['firstName' => 'David', 'lastName' => 'Bowie']

Example

The following example shows the hydration for a class with a property that consumes another class.

An example class which represents a music album.

class Album
{
    private ?int $id = null;

    private ?string $title = null;

    private ?Artist $artist = null;

    public function __construct(
        ?int $id = null,
        ?string $title = null,
        ?Artist $artist = null
    ) {
        $this->id     = $id;
        $this->title  = $title;
        $this->artist = $artist;
    }

    public function getId() : ?int
    {
        return $this->id;
    }

    public function getTitle() : ?string
    {
        return $this->title;
    }

    public function getArtist() : ?Artist
    {
        return $this->artist;
    }
}

An example class representing the artist of an album.

class Artist
{
    private ?string $firstName;

    private ?string $lastName;

    public function __construct(
        ?string $firstName = null,
        ?string $lastName = null
    ) {
        $this->firstName = $firstName;
        $this->lastName  = $lastName;
    }

    public function getFirstName() : ?string
    {
        return $this->firstName;
    }

    public function getLastName() : ?string
    {
        return $this->lastName;
    }
}

Create hydrator and add strategy

Create a hydrator and add HydratorStrategy as a strategy, with a hydrator and a classname for the handled object.

$hydrator = new Laminas\Hydrator\ReflectionHydrator();
$hydrator->addStrategy(
    'artist',
    new Laminas\Hydrator\Strategy\HydratorStrategy(
        new Laminas\Hydrator\ReflectionHydrator(),
        Artist::class
    )
);

Hydrate data

Create an instance of the example Album class and hydrate data.

$album = new Album();
$hydrator->hydrate(
    [
        'id'     => 100,
        'title'  => 'The Next Day (Deluxe Version)',
        'artist' => [
            'firstName' => 'David',
            'lastName'  => 'Bowie',
        ],
    ],
    $album
);

echo $album->getTitle(); // 'The Next Day (Deluxe Version)'
echo $album->getArtist()->getFirstName(); // 'David'
echo $album->getArtist()->getLastName(); // 'Bowie'

Extract data

var_dump($hydrator->extract($album));
/*
array(3) {
  'id' =>
  int(100)
  'title' =>
  string(29) "The Next Day (Deluxe Version)"
  'artist' =>
  array(2) {
    'firstName' =>
    string(5) "David"
    'lastName' =>
    string(5) "Bowie"
  }
}
*/
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle