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 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.

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.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle