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.
Laminas\Hydrator\NamingStrategy\MapNamingStrategy allows you to provide a map of
keys to use when hydrating and extracting; the map will translate the key based
on the direction.
$namingStrategy = new Laminas\Hydrator\NamingStrategy\MapNamingStrategy(array(
'foo' => 'bar',
'baz' => 'bash'
));
echo $namingStrategy->hydrate('foo'); // outputs: bar
echo $namingStrategy->hydrate('baz'); // outputs: bash
echo $namingStrategy->extract('bar'); // outputs: foo
echo $namingStrategy->extract('bash'); // outputs: baz
This strategy can be used in hydrators to dictate how keys should be mapped:
class Foo
{
public $bar;
}
$namingStrategy = new Laminas\Hydrator\NamingStrategy\MapNamingStrategy([
'foo' => 'bar',
'baz' => 'bash'
]);
$hydrator = new Laminas\Hydrator\ObjectProperty();
$hydrator->setNamingStrategy($namingStrategy);
$foo = new Foo();
$hydrator->hydrate(['foo' => 123], $foo);
print_r($foo); // Foo Object ( [bar] => 123 )
print_r($hydrator->extract($foo)); // Array ( [foo] => 123 )
How can I help you explore Laravel packages today?