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

CompositeNamingStrategy

Laminas\Hydrator\NamingStrategy\CompositeNamingStrategy allows you to specify which naming strategy should be used for each key encountered during hydration or extraction.

Basic Usage

When invoked, the following composite strategy will extract the property bar to the array key foo (using the MapNamingStrategy), and the property barBat to the array key bar_bat (using the UnderscoreNamingStrategy):

class Foo
{
    public $bar;
    public $barBat;
}

$mapStrategy = new Laminas\Hydrator\NamingStrategy\MapNamingStrategy([
    'foo' => 'bar'
]);

$underscoreNamingStrategy = new Laminas\Hydrator\NamingStrategy\UnderscoreNamingStrategy();

$namingStrategy = new Laminas\Hydrator\NamingStrategy\CompositeNamingStrategy([
    'bar' => $mapStrategy,
    'barBat' => $underscoreNamingStrategy,
]);

$hydrator = new Laminas\Hydrator\ObjectProperty();
$hydrator->setNamingStrategy($namingStrategy);

$foo = new Foo();
$foo->bar = 123;
$foo->barBat = 42;

print_r($foo); // Foo Object ( [bar] => 123 [barBat] => 42 )
print_r($hydrator->extract($foo)); // Array ( [foo] => 123 [bar_bat] => 42 )

Unfortunately, the CompositeNamingStrategy can only be used for extraction as it will not know how to handle the keys necessary for hydration (foo and bar_bat, respectively). To rectify this we have to cover the keys for both hydration and extraction in our composite strategy:

class Foo
{
    public $bar;
    public $barBat;
}

$mapStrategy = new Laminas\Hydrator\NamingStrategy\MapNamingStrategy([
    'foo' => 'bar'
]);

$underscoreNamingStrategy = new Laminas\Hydrator\NamingStrategy\UnderscoreNamingStrategy();

$namingStrategy = new Laminas\Hydrator\NamingStrategy\CompositeNamingStrategy([
    // Define both directions for the foo => bar mapping
    'bar' => $mapStrategy,
    'foo' => $mapStrategy,
    // Define both directions for the barBat => bar_bat mapping
    'barBat' => $underscoreNamingStrategy,
    'bar_bat' => $underscoreNamingStrategy,
]);

$hydrator = new Laminas\Hydrator\ObjectProperty();
$hydrator->setNamingStrategy($namingStrategy);

$foo = new Foo();
$foo->bar = 123;
$foo->barBat = 42;

$array = $hydrator->extract($foo);

print_r($foo); // Foo Object ( [bar] => 123 [barBat] => 42 )
print_r($array); // Array ( [foo] => 123 [bar_bat] => 42 )

$foo2 = new Foo();
$hydrator->hydrate($array, $foo2);

print_r($foo2); // Foo Object ( [bar] => 123 [barBat] => 42 )
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi