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\ObjectPropertyHydrator();
$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\ObjectPropertyHydrator();
$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.
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