This repository has been archived by the owner on May 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for Hydrator's CompositeNamingStrategy
- Loading branch information
1 parent
c034448
commit 037955b
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
docs/src/modules/zend.stdlib.hydrator.namingstrategy.compositenamingstrategy.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
.. _zend.stdlib.hydrator.namingstrategy.compositenamingstrategy: | ||
|
||
CompositeNamingStrategy | ||
====================== | ||
|
||
``Zend\Stdlib\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`` into array key ``foo`` | ||
(MapNamingStrategy) and property ``barBat`` into ``bar_bat`` (UnderscoreNamingStrategy): | ||
|
||
.. code-block:: php | ||
:linenos: | ||
class Foo | ||
{ | ||
public $bar; | ||
public $barBat; | ||
} | ||
$mapStrategy = new Zend\Stdlib\Hydrator\NamingStrategy\MapNamingStrategy([ | ||
'foo' => 'bar' | ||
]); | ||
$underscoreNamingStrategy = new Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy(); | ||
$namingStrategy = new Zend\Stdlib\Hydrator\NamingStrategy\CompositeNamingStrategy([ | ||
'bar' => $mapStrategy, | ||
'barBat' => $underscoreNamingStrategy, | ||
]); | ||
$hydrator = new Zend\Stdlib\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, this 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: | ||
|
||
.. code-block:: php | ||
:linenos: | ||
class Foo | ||
{ | ||
public $bar; | ||
public $barBat; | ||
} | ||
$mapStrategy = new Zend\Stdlib\Hydrator\NamingStrategy\MapNamingStrategy([ | ||
'foo' => 'bar' | ||
]); | ||
$underscoreNamingStrategy = new Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy(); | ||
$namingStrategy = new Zend\Stdlib\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 Zend\Stdlib\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 ) | ||