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 both during extraction and hydration; the map will translate the key
based on the direction:
When a map is provided for hydration, but not extraction, the strategy will
perform an array_flip on the hydration map when performing lookups.
You can create an instance with this behavior using
MapNamingStrategy::createFromHydrationMap(array $hydrationMap) : MapNamingStrategy.
When a map is provided for extraction, but not hydration, the strategy will
perform an array_flip on the extraction map when performing lookups.
You can create an instance with this behavior using
MapNamingStrategy::createFromExtractionMap(array $extractionMap) : MapNamingStrategy.
When maps are provided for both extraction and hydration, the appropriate map
will be used during extraction and hydration operations. You can create an
instance with this behavior using
MapNamingStrategy::createFromAsymmetricMap(array $extractionMap, array $hydrationStrategy) : MapNamingStrategy.
Most of the time, you will want your maps symmetrical; as such, set either a hydration map or an extraction map, but not both.
$namingStrategy = Laminas\Hydrator\NamingStrategy\MapNamingStrategy::createFromHydrationMap(
[
'foo' => 'bar',
'baz' => 'bash'
]
);
echo $namingStrategy->extract('bar'); // outputs: foo
echo $namingStrategy->extract('bash'); // outputs: baz
echo $namingStrategy->hydrate('foo'); // outputs: bar
echo $namingStrategy->hydrate('baz'); // outputs: bash
$namingStrategy = Laminas\Hydrator\NamingStrategy\MapNamingStrategy::createFromExtractionMap(
[
'foo' => 'bar',
'baz' => 'bash'
]
);
echo $namingStrategy->extract('foo'); // outputs: bar
echo $namingStrategy->extract('baz'); // outputs: bash
echo $namingStrategy->hydrate('bar'); // outputs: foo
echo $namingStrategy->hydrate('bash'); // outputs: baz
$namingStrategy = Laminas\Hydrator\NamingStrategy\MapNamingStrategy::createFromAsymmetricMap(
[
'foo' => 'bar',
'baz' => 'bash'
],
[
'is_bar' => 'foo',
'bashable' => 'baz',
]
);
echo $namingStrategy->extract('foo'); // outputs: bar
echo $namingStrategy->extract('baz'); // outputs: bash
echo $namingStrategy->hydrate('is_bar'); // outputs: foo
echo $namingStrategy->hydrate('bashable'); // outputs: baz
This strategy can be used in hydrators to dictate how keys should be mapped:
class Foo
{
public $bar;
}
$namingStrategy = Laminas\Hydrator\NamingStrategy\MapNamingStrategy::createFromHydrationMap([
'foo' => 'bar',
'baz' => 'bash',
]);
$hydrator = new Laminas\Hydrator\ObjectPropertyHydrator();
$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?