From f9d040fed4ffb860f567e4e07cc7af39a245cdc3 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:20:31 +0200 Subject: [PATCH 01/15] Made the hydrating and extracting of values more generic. --- src/Hydrator/AbstractHydrator.php | 18 +++++++++++++++++ src/Hydrator/ArraySerializable.php | 32 ++++++++++-------------------- src/Hydrator/ObjectProperty.php | 15 ++++---------- src/Hydrator/Reflection.php | 15 +++----------- 4 files changed, 35 insertions(+), 45 deletions(-) diff --git a/src/Hydrator/AbstractHydrator.php b/src/Hydrator/AbstractHydrator.php index aeaf039b0..3b1247ae8 100644 --- a/src/Hydrator/AbstractHydrator.php +++ b/src/Hydrator/AbstractHydrator.php @@ -41,4 +41,22 @@ public function registerStrategy($name, StrategyInterface $strategy) { $this->strategies[$name] = $strategy; } + + public function extractValue($name, $value) + { + if ($this->hasStrategy($name)) { + $strategy = $this->getStrategy($name); + $value = $strategy->extract($value); + } + return $value; + } + + public function hydrateValue($name, $value) + { + if ($this->hasStrategy($name)) { + $strategy = $this->getStrategy($name); + $value = $strategy->hydrate($value); + } + return $value; + } } diff --git a/src/Hydrator/ArraySerializable.php b/src/Hydrator/ArraySerializable.php index 84ecf226d..3f2bee9b3 100644 --- a/src/Hydrator/ArraySerializable.php +++ b/src/Hydrator/ArraySerializable.php @@ -36,19 +36,12 @@ public function extract($object) __METHOD__ )); } - $data = $object->getArrayCopy(); - - $result = array(); - foreach ($data as $name => $value) { - if ($this->hasStrategy($name)) { - $strategy = $this->getStrategy($name); - $value = $strategy->extract($value); - } - - $result[$name] = $value; - } - return $result; + $data = $object->getArrayCopy(); + array_walk($data, function(&$value, $name) { + $value = $this->extractValue($name, $value); + }); + return $data; } /** @@ -64,19 +57,14 @@ public function extract($object) */ public function hydrate(array $data, $object) { - $hydrationData = array(); - foreach ($data as $name => $value) { - if ($this->hasStrategy($name)) { - $strategy = $this->getStrategy($name); - $value = $strategy->hydrate($value); - } - $hydrationData[$name] = $value; - } + array_walk($data, function(&$value, $name) { + $value = $this->hydrateValue($name, $value); + }); if (is_callable(array($object, 'exchangeArray'))) { - $object->exchangeArray($hydrationData); + $object->exchangeArray($data); } elseif (is_callable(array($object, 'populate'))) { - $object->populate($hydrationData); + $object->populate($data); } else { throw new Exception\BadMethodCallException(sprintf( '%s expects the provided object to implement exchangeArray() or populate()', diff --git a/src/Hydrator/ObjectProperty.php b/src/Hydrator/ObjectProperty.php index 52a2999e2..283b56aea 100644 --- a/src/Hydrator/ObjectProperty.php +++ b/src/Hydrator/ObjectProperty.php @@ -38,12 +38,9 @@ public function extract($object) } $data = get_object_vars($object); - foreach ($data as $name => $value) { - if ($this->hasStrategy($name)) { - $strategy = $this->getStrategy($name); - $data[$name] = $strategy->extract($value); - } - } + array_walk($data, function(&$value, $name) { + $value = $this->extractValue($name, $value); + }); return $data; } @@ -66,11 +63,7 @@ public function hydrate(array $data, $object) )); } foreach ($data as $property => $value) { - if ($this->hasStrategy($property)) { - $strategy = $this->getStrategy($property); - $value = $strategy->hydrate($value); - } - $object->$property = $value; + $object->$property = $this->hydrateValue($property, $value); } return $object; } diff --git a/src/Hydrator/Reflection.php b/src/Hydrator/Reflection.php index a9c5fc9e7..98588f6bb 100644 --- a/src/Hydrator/Reflection.php +++ b/src/Hydrator/Reflection.php @@ -33,14 +33,9 @@ public function extract($object) $result = array(); foreach(self::getReflProperties($object) as $property) { $propertyName = $property->getName(); - $value = $property->getValue($object); - - if ($this->hasStrategy($propertyName)) { - $strategy = $this->getStrategy($propertyName); - $value = $strategy->extract($value); - } - $result[$propertyName] = $value; + $value = $property->getValue($object); + $result[$propertyName] = $this->extractValue($propertyName, $value); } return $result; @@ -58,11 +53,7 @@ public function hydrate(array $data, $object) $reflProperties = self::getReflProperties($object); foreach($data as $key => $value) { if (isset($reflProperties[$key])) { - if ($this->hasStrategy($key)) { - $strategy = $this->getStrategy($key); - $value = $strategy->hydrate($value); - } - $reflProperties[$key]->setValue($object, $value); + $reflProperties[$key]->setValue($object, $this->hydrateValue($key, $value)); } } return $object; From 4cc1eb3955b9bceb6974973957b0832b672ab79f Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:34:14 +0200 Subject: [PATCH 02/15] Fixes for the CS --- src/Hydrator/ArraySerializable.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Hydrator/ArraySerializable.php b/src/Hydrator/ArraySerializable.php index 3f2bee9b3..d736741e0 100644 --- a/src/Hydrator/ArraySerializable.php +++ b/src/Hydrator/ArraySerializable.php @@ -19,6 +19,7 @@ */ class ArraySerializable extends AbstractHydrator { + /** * Extract values from the provided object * @@ -32,15 +33,14 @@ public function extract($object) { if (!is_callable(array($object, 'getArrayCopy'))) { throw new Exception\BadMethodCallException(sprintf( - '%s expects the provided object to implement getArrayCopy()', - __METHOD__ + '%s expects the provided object to implement getArrayCopy()', __METHOD__ )); } - + $data = $object->getArrayCopy(); - array_walk($data, function(&$value, $name) { - $value = $this->extractValue($name, $value); - }); + array_walk($data, function(&$value, $name) { + $value = $this->extractValue($name, $value); + }); return $data; } @@ -57,18 +57,17 @@ public function extract($object) */ public function hydrate(array $data, $object) { - array_walk($data, function(&$value, $name) { - $value = $this->hydrateValue($name, $value); - }); - + array_walk($data, function(&$value, $name) { + $value = $this->hydrateValue($name, $value); + }); + if (is_callable(array($object, 'exchangeArray'))) { $object->exchangeArray($data); } elseif (is_callable(array($object, 'populate'))) { $object->populate($data); } else { throw new Exception\BadMethodCallException(sprintf( - '%s expects the provided object to implement exchangeArray() or populate()', - __METHOD__ + '%s expects the provided object to implement exchangeArray() or populate()', __METHOD__ )); } return $object; From 830fda761a63f159d9ca6e34e37dcd10be15782d Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:34:57 +0200 Subject: [PATCH 03/15] Forgot to add the extraction for ClassMethods --- src/Hydrator/ClassMethods.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index e6139f2ef..ca29edaa9 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -71,15 +71,7 @@ public function extract($object) if ($this->underscoreSeparatedKeys) { $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute); } - - $value = $object->$method(); - - if ($this->hasStrategy($attribute)) { - $strategy = $this->getStrategy($attribute); - $value = $strategy->extract($value); - } - - $attributes[$attribute] = $value; + $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); } } From 4107c8920a17b4fc2d07bb0b45ded6abacfe80f8 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:35:19 +0200 Subject: [PATCH 04/15] Fixes for the CS --- src/Hydrator/ClassMethods.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index ca29edaa9..26d326fc1 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -48,8 +48,7 @@ public function extract($object) { if (!is_object($object)) { throw new Exception\BadMethodCallException(sprintf( - '%s expects the provided $object to be a PHP object)', - __METHOD__ + '%s expects the provided $object to be a PHP object)', __METHOD__ )); } @@ -59,11 +58,11 @@ public function extract($object) }; $attributes = array(); $methods = get_class_methods($object); - foreach($methods as $method) { - if(preg_match('/^get[A-Z]\w*/', $method)) { + foreach ($methods as $method) { + if (preg_match('/^get[A-Z]\w*/', $method)) { // setter verification $setter = preg_replace('/^get/', 'set', $method); - if(!in_array($setter, $methods)) { + if (!in_array($setter, $methods)) { continue; } $attribute = substr($method, 3); @@ -92,8 +91,7 @@ public function hydrate(array $data, $object) { if (!is_object($object)) { throw new Exception\BadMethodCallException(sprintf( - '%s expects the provided $object to be a PHP object)', - __METHOD__ + '%s expects the provided $object to be a PHP object)', __METHOD__ )); } @@ -112,7 +110,7 @@ public function hydrate(array $data, $object) $strategy = $this->getStrategy($property); $value = $strategy->hydrate($value); } - + $object->$method($value); } } From c34ae1f96970fd39c5b694eda856d472ed5e7052 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:37:40 +0200 Subject: [PATCH 05/15] Updated the ClassMethods hydrator --- src/Hydrator/ClassMethods.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 26d326fc1..42cce880e 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -106,10 +106,7 @@ public function hydrate(array $data, $object) } $method = 'set' . ucfirst($property); if (method_exists($object, $method)) { - if ($this->hasStrategy($property)) { - $strategy = $this->getStrategy($property); - $value = $strategy->hydrate($value); - } + $value = $this->hydrateValue($property, $value); $object->$method($value); } From 3ef1bfcf1080842f01b846c4c2e05a19e3dface8 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:42:01 +0200 Subject: [PATCH 06/15] Changed the array with an ArrayObject --- src/Hydrator/AbstractHydrator.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Hydrator/AbstractHydrator.php b/src/Hydrator/AbstractHydrator.php index 3b1247ae8..2343ccf59 100644 --- a/src/Hydrator/AbstractHydrator.php +++ b/src/Hydrator/AbstractHydrator.php @@ -10,7 +10,7 @@ namespace Zend\Stdlib\Hydrator; -use Zend\Stdlib\Exception; +use ArrayObject; use Zend\Stdlib\Hydrator\Strategy\StrategyInterface; /** @@ -20,11 +20,19 @@ */ abstract class AbstractHydrator implements HydratorInterface { + /** + * The list with strategies that this hydrator has. + * + * @var ArrayObject + */ protected $strategies; - + + /** + * Initializes a new instance of this class. + */ public function __construct() { - $this->strategies = array(); + $this->strategies = new ArrayObject(); } public function getStrategy($name) From 95b392777d79ebe24319c987dc4030649b201a4c Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:50:37 +0200 Subject: [PATCH 07/15] Updated the HydratorInterface and added docblocks --- src/Hydrator/AbstractHydrator.php | 48 ++++++++++++++++++++- src/Hydrator/HydratorInterface.php | 33 ++++++++++++++ src/Hydrator/Strategy/StrategyInterface.php | 6 +-- 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/Hydrator/AbstractHydrator.php b/src/Hydrator/AbstractHydrator.php index 2343ccf59..9dd9cabc7 100644 --- a/src/Hydrator/AbstractHydrator.php +++ b/src/Hydrator/AbstractHydrator.php @@ -35,21 +35,60 @@ public function __construct() $this->strategies = new ArrayObject(); } + /** + * Gets the strategy with the given name. + * + * @param string $name The name of the strategy to get. + * @return StrategyInterface + */ public function getStrategy($name) { return $this->strategies[$name]; } + /** + * Checks if the strategy with the given name exists. + * + * @param string $name The name of the strategy to check for. + * @return bool + */ public function hasStrategy($name) { return array_key_exists($name, $this->strategies); } - public function registerStrategy($name, StrategyInterface $strategy) + /** + * Adds the given strategy under the given name. + * + * @param string $name The name of the strategy to register. + * @param StrategyInterface $strategy The strategy to register. + * @return HydratorInterface + */ + public function addStrategy($name, StrategyInterface $strategy) { $this->strategies[$name] = $strategy; + return $this; } + /** + * Removes the strategy with the given name. + * + * @param string $name The name of the strategy to remove. + * @return HydratorInterface + */ + public function removeStrategy($name) + { + unset($this->strategies[$name]); + return $this; + } + + /** + * Converts a value for extraction. If no strategy exists the plain value is returned. + * + * @param string $name The name of the strategy to use. + * @param mixed $value The value that should be converted. + * @return mixed + */ public function extractValue($name, $value) { if ($this->hasStrategy($name)) { @@ -59,6 +98,13 @@ public function extractValue($name, $value) return $value; } + /** + * Converts a value for hydration. If no strategy exists the plain value is returned. + * + * @param string $name The name of the strategy to use. + * @param mixed $value The value that should be converted. + * @return mixed + */ public function hydrateValue($name, $value) { if ($this->hasStrategy($name)) { diff --git a/src/Hydrator/HydratorInterface.php b/src/Hydrator/HydratorInterface.php index d014b3fdc..488255121 100644 --- a/src/Hydrator/HydratorInterface.php +++ b/src/Hydrator/HydratorInterface.php @@ -17,6 +17,15 @@ */ interface HydratorInterface { + /** + * Adds the given strategy under the given name. + * + * @param string $name The name of the strategy to register. + * @param StrategyInterface $strategy The strategy to register. + * @return HydratorInterface + */ + public function addStrategy($name, StrategyInterface $strategy); + /** * Extract values from an object * @@ -25,6 +34,22 @@ interface HydratorInterface */ public function extract($object); + /** + * Gets the strategy with the given name. + * + * @param string $name The name of the strategy to get. + * @return StrategyInterface + */ + public function getStrategy($name); + + /** + * Checks if the strategy with the given name exists. + * + * @param string $name The name of the strategy to check for. + * @return bool + */ + public function hasStrategy($name); + /** * Hydrate $object with the provided $data. * @@ -33,4 +58,12 @@ public function extract($object); * @return object */ public function hydrate(array $data, $object); + + /** + * Removes the strategy with the given name. + * + * @param string $name The name of the strategy to remove. + * @return HydratorInterface + */ + public function removeStrategy($name); } diff --git a/src/Hydrator/Strategy/StrategyInterface.php b/src/Hydrator/Strategy/StrategyInterface.php index c901b21d7..7f8fe8df4 100644 --- a/src/Hydrator/Strategy/StrategyInterface.php +++ b/src/Hydrator/Strategy/StrategyInterface.php @@ -23,13 +23,13 @@ interface StrategyInterface * @param mixed $value The original value. * @return mixed Returns the value that should be extracted. */ - public function extract($value); - + public function extract($value); + /** * Converts the given value so that it can be hydrated by the hydrator. * * @param mixed $value The original value. * @return mixed Returns the value that should be hydrated. */ - public function hydrate($value); + public function hydrate($value); } From ce72b1c9593c87144b3439af6c233b267b28692f Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:51:52 +0200 Subject: [PATCH 08/15] Added a use statement to prevent PHP errors --- src/Hydrator/HydratorInterface.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Hydrator/HydratorInterface.php b/src/Hydrator/HydratorInterface.php index 488255121..68df7a8c5 100644 --- a/src/Hydrator/HydratorInterface.php +++ b/src/Hydrator/HydratorInterface.php @@ -10,6 +10,8 @@ namespace Zend\Stdlib\Hydrator; +use Zend\Stdlib\Hydrator\Strategy\StrategyInterface; + /** * @category Zend * @package Zend_Stdlib From 5e13efb558649408a8e5df93416bdf09f8b2ff13 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:53:24 +0200 Subject: [PATCH 09/15] Added indentation... Netbeans' code formatting sucks --- src/Hydrator/ArraySerializable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hydrator/ArraySerializable.php b/src/Hydrator/ArraySerializable.php index d736741e0..7b4054604 100644 --- a/src/Hydrator/ArraySerializable.php +++ b/src/Hydrator/ArraySerializable.php @@ -33,7 +33,7 @@ public function extract($object) { if (!is_callable(array($object, 'getArrayCopy'))) { throw new Exception\BadMethodCallException(sprintf( - '%s expects the provided object to implement getArrayCopy()', __METHOD__ + '%s expects the provided object to implement getArrayCopy()', __METHOD__ )); } From f3880604a44f1e4cdddb997902635fd208c04fc6 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:55:49 +0200 Subject: [PATCH 10/15] Netbeans' code formatting sucks --- src/Hydrator/ObjectProperty.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Hydrator/ObjectProperty.php b/src/Hydrator/ObjectProperty.php index 283b56aea..14cf9e5f6 100644 --- a/src/Hydrator/ObjectProperty.php +++ b/src/Hydrator/ObjectProperty.php @@ -32,15 +32,14 @@ public function extract($object) { if (!is_object($object)) { throw new Exception\BadMethodCallException(sprintf( - '%s expects the provided $object to be a PHP object)', - __METHOD__ + '%s expects the provided $object to be a PHP object)', __METHOD__ )); } $data = get_object_vars($object); - array_walk($data, function(&$value, $name) { - $value = $this->extractValue($name, $value); - }); + array_walk($data, function(&$value, $name) { + $value = $this->extractValue($name, $value); + }); return $data; } @@ -58,8 +57,7 @@ public function hydrate(array $data, $object) { if (!is_object($object)) { throw new Exception\BadMethodCallException(sprintf( - '%s expects the provided $object to be a PHP object)', - __METHOD__ + '%s expects the provided $object to be a PHP object)', __METHOD__ )); } foreach ($data as $property => $value) { From 24fe5ef79dd748ff814c29e845351564bbf8d898 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:56:47 +0200 Subject: [PATCH 11/15] Netbeans' code formatting sucks --- src/Hydrator/Strategy/DefaultStrategy.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Hydrator/Strategy/DefaultStrategy.php b/src/Hydrator/Strategy/DefaultStrategy.php index 5653003a6..efd17e1d3 100644 --- a/src/Hydrator/Strategy/DefaultStrategy.php +++ b/src/Hydrator/Strategy/DefaultStrategy.php @@ -23,18 +23,18 @@ class DefaultStrategy implements StrategyInterface * @param mixed $value The original value. * @return mixed Returns the value that should be extracted. */ - public function extract($value) + public function extract($value) { return $value; } - + /** * Converts the given value so that it can be hydrated by the hydrator. * * @param mixed $value The original value. * @return mixed Returns the value that should be hydrated. */ - public function hydrate($value) + public function hydrate($value) { return $value; } From f91196e7d0c48ef81ea0a92020eb50b3df02b9ea Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 10:59:22 +0200 Subject: [PATCH 12/15] Netbeans' code formatting sucks --- src/Hydrator/ArraySerializable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hydrator/ArraySerializable.php b/src/Hydrator/ArraySerializable.php index 7b4054604..9c8650787 100644 --- a/src/Hydrator/ArraySerializable.php +++ b/src/Hydrator/ArraySerializable.php @@ -67,7 +67,7 @@ public function hydrate(array $data, $object) $object->populate($data); } else { throw new Exception\BadMethodCallException(sprintf( - '%s expects the provided object to implement exchangeArray() or populate()', __METHOD__ + '%s expects the provided object to implement exchangeArray() or populate()', __METHOD__ )); } return $object; From 6f658cfe808781abb81dd7d8651c0e6534ada4dd Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 15:41:53 +0200 Subject: [PATCH 13/15] Created a StrategyEnabledInterface which allows the creation of hydrators without implementing strategies. --- src/Hydrator/AbstractHydrator.php | 3 +- src/Hydrator/HydratorInterface.php | 35 --------------- src/Hydrator/StrategyEnabledInterface.php | 54 +++++++++++++++++++++++ 3 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 src/Hydrator/StrategyEnabledInterface.php diff --git a/src/Hydrator/AbstractHydrator.php b/src/Hydrator/AbstractHydrator.php index 9dd9cabc7..6d19ebb8c 100644 --- a/src/Hydrator/AbstractHydrator.php +++ b/src/Hydrator/AbstractHydrator.php @@ -11,6 +11,7 @@ namespace Zend\Stdlib\Hydrator; use ArrayObject; +use Zend\Stdlib\Hydrator\StrategyEnabledInterface; use Zend\Stdlib\Hydrator\Strategy\StrategyInterface; /** @@ -18,7 +19,7 @@ * @package Zend_Stdlib * @subpackage Hydrator */ -abstract class AbstractHydrator implements HydratorInterface +abstract class AbstractHydrator implements HydratorInterface, StrategyEnabledInterface { /** * The list with strategies that this hydrator has. diff --git a/src/Hydrator/HydratorInterface.php b/src/Hydrator/HydratorInterface.php index 68df7a8c5..d014b3fdc 100644 --- a/src/Hydrator/HydratorInterface.php +++ b/src/Hydrator/HydratorInterface.php @@ -10,8 +10,6 @@ namespace Zend\Stdlib\Hydrator; -use Zend\Stdlib\Hydrator\Strategy\StrategyInterface; - /** * @category Zend * @package Zend_Stdlib @@ -19,15 +17,6 @@ */ interface HydratorInterface { - /** - * Adds the given strategy under the given name. - * - * @param string $name The name of the strategy to register. - * @param StrategyInterface $strategy The strategy to register. - * @return HydratorInterface - */ - public function addStrategy($name, StrategyInterface $strategy); - /** * Extract values from an object * @@ -36,22 +25,6 @@ public function addStrategy($name, StrategyInterface $strategy); */ public function extract($object); - /** - * Gets the strategy with the given name. - * - * @param string $name The name of the strategy to get. - * @return StrategyInterface - */ - public function getStrategy($name); - - /** - * Checks if the strategy with the given name exists. - * - * @param string $name The name of the strategy to check for. - * @return bool - */ - public function hasStrategy($name); - /** * Hydrate $object with the provided $data. * @@ -60,12 +33,4 @@ public function hasStrategy($name); * @return object */ public function hydrate(array $data, $object); - - /** - * Removes the strategy with the given name. - * - * @param string $name The name of the strategy to remove. - * @return HydratorInterface - */ - public function removeStrategy($name); } diff --git a/src/Hydrator/StrategyEnabledInterface.php b/src/Hydrator/StrategyEnabledInterface.php new file mode 100644 index 000000000..faff0abb8 --- /dev/null +++ b/src/Hydrator/StrategyEnabledInterface.php @@ -0,0 +1,54 @@ + Date: Wed, 1 Aug 2012 18:14:22 +0200 Subject: [PATCH 14/15] Added tests for the hydrator strategy --- test/HydratorStrategyTest.php | 109 +++++++++++++++++++++ test/TestAsset/HydratorStrategy.php | 64 ++++++++++++ test/TestAsset/HydratorStrategyEntityA.php | 75 ++++++++++++++ test/TestAsset/HydratorStrategyEntityB.php | 47 +++++++++ 4 files changed, 295 insertions(+) create mode 100644 test/HydratorStrategyTest.php create mode 100644 test/TestAsset/HydratorStrategy.php create mode 100644 test/TestAsset/HydratorStrategyEntityA.php create mode 100644 test/TestAsset/HydratorStrategyEntityB.php diff --git a/test/HydratorStrategyTest.php b/test/HydratorStrategyTest.php new file mode 100644 index 000000000..85316d43e --- /dev/null +++ b/test/HydratorStrategyTest.php @@ -0,0 +1,109 @@ +hydrator = new ClassMethods(); + } + + public function testAddingStrategy() + { + $this->assertAttributeCount(0, 'strategies', $this->hydrator); + + $this->hydrator->addStrategy('myStrategy', new HydratorStrategy()); + + $this->assertAttributeCount(1, 'strategies', $this->hydrator); + } + + public function testCheckStrategyEmpty() + { + $this->assertFalse($this->hydrator->hasStrategy('myStrategy')); + } + + public function testCheckStrategyNotEmpty() + { + $this->hydrator->addStrategy('myStrategy', new HydratorStrategy()); + + $this->assertTrue($this->hydrator->hasStrategy('myStrategy')); + } + + public function testRemovingStrategy() + { + $this->assertAttributeCount(0, 'strategies', $this->hydrator); + + $this->hydrator->addStrategy('myStrategy', new HydratorStrategy()); + $this->assertAttributeCount(1, 'strategies', $this->hydrator); + + $this->hydrator->removeStrategy('myStrategy'); + $this->assertAttributeCount(0, 'strategies', $this->hydrator); + } + + public function testRetrieveStrategy() + { + $strategy = new HydratorStrategy(); + $this->hydrator->addStrategy('myStrategy', $strategy); + + $this->assertEquals($strategy, $this->hydrator->getStrategy('myStrategy')); + } + + public function testExtractingObjects() + { + $this->hydrator->addStrategy('entities', new HydratorStrategy()); + + $entityA = new HydratorStrategyEntityA(); + $entityA->addEntity(new HydratorStrategyEntityB(111, 'AAA')); + $entityA->addEntity(new HydratorStrategyEntityB(222, 'BBB')); + + $attributes = $this->hydrator->extract($entityA); + + $this->assertContains(111, $attributes['entities']); + $this->assertContains(222, $attributes['entities']); + } + + public function testHydratingObjects() + { + $this->hydrator->addStrategy('entities', new HydratorStrategy()); + + $entityA = new HydratorStrategyEntityA(); + $entityA->addEntity(new HydratorStrategyEntityB(111, 'AAA')); + $entityA->addEntity(new HydratorStrategyEntityB(222, 'BBB')); + + $attributes = $this->hydrator->extract($entityA); + $attributes['entities'][] = 333; + + $this->hydrator->hydrate($attributes, $entityA); + $entities = $entityA->getEntities(); + + $this->assertCount(3, $entities); + } +} diff --git a/test/TestAsset/HydratorStrategy.php b/test/TestAsset/HydratorStrategy.php new file mode 100644 index 000000000..ba222ff59 --- /dev/null +++ b/test/TestAsset/HydratorStrategy.php @@ -0,0 +1,64 @@ +simulatedStorageDevice = array(); + $this->simulatedStorageDevice[] = new HydratorStrategyEntityB(111, 'AAA'); + $this->simulatedStorageDevice[] = new HydratorStrategyEntityB(222, 'BBB'); + $this->simulatedStorageDevice[] = new HydratorStrategyEntityB(333, 'CCC'); + } + + public function extract($value) + { + $result = array(); + foreach ($value as $instance) { + $result[] = $instance->getField1(); + } + return $result; + } + + public function hydrate($value) + { + $result = $value; + if (is_array($value)) { + $result = array(); + foreach ($value as $field1) { + $result[] = $this->findEntity($field1); + } + } + return $result; + } + + private function findEntity($field1) + { + $result = null; + foreach ($this->simulatedStorageDevice as $entity) { + if ($entity->getField1() == $field1) { + $result = $entity; + break; + } + } + return $result; + } +} diff --git a/test/TestAsset/HydratorStrategyEntityA.php b/test/TestAsset/HydratorStrategyEntityA.php new file mode 100644 index 000000000..8af71a0e7 --- /dev/null +++ b/test/TestAsset/HydratorStrategyEntityA.php @@ -0,0 +1,75 @@ +entities = array(); + } + + public function addEntity(HydratorStrategyEntityB $entity) + { + $this->entities[] = $entity; + } + + public function getEntities() + { + return $this->entities; + } + + public function setEntities($entities) + { + $this->entities = $entities; + } + + public function getInputFilter() + { + if (!$this->inputFilter) { + $input = new Input(); + $input->setName('entities'); + $input->setRequired(false); + + $this->inputFilter = new InputFilter(); + $this->inputFilter->add($input); + } + + return $this->inputFilter; + } + + public function setInputFilter(InputFilterInterface $inputFilter) + { + $this->inputFilter = $inputFilter; + } + + // Add the getArrayCopy method so we can test the ArraySerializable hydrator: + public function getArrayCopy() + { + return get_object_vars($this); + } + + // Add the populate method so we can test the ArraySerializable hydrator: + public function populate($data) + { + foreach ($data as $name => $value) { + $this->$name = $value; + } + } +} \ No newline at end of file diff --git a/test/TestAsset/HydratorStrategyEntityB.php b/test/TestAsset/HydratorStrategyEntityB.php new file mode 100644 index 000000000..9aae0a4c1 --- /dev/null +++ b/test/TestAsset/HydratorStrategyEntityB.php @@ -0,0 +1,47 @@ +field1 = $field1; + $this->field2 = $field2; + } + + public function getField1() + { + return $this->field1; + } + + public function getField2() + { + return $this->field2; + } + + public function setField1($value) + { + $this->field1 = $value; + return $this; + } + + public function setField2($value) + { + $this->field2 = $value; + return $this; + } +} \ No newline at end of file From debdd75aae2913d72f17540c16f6277a0491d18a Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 1 Aug 2012 18:16:57 +0200 Subject: [PATCH 15/15] Added new lines to the end of the files. --- test/TestAsset/HydratorStrategyEntityA.php | 2 +- test/TestAsset/HydratorStrategyEntityB.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/TestAsset/HydratorStrategyEntityA.php b/test/TestAsset/HydratorStrategyEntityA.php index 8af71a0e7..988cfa99a 100644 --- a/test/TestAsset/HydratorStrategyEntityA.php +++ b/test/TestAsset/HydratorStrategyEntityA.php @@ -72,4 +72,4 @@ public function populate($data) $this->$name = $value; } } -} \ No newline at end of file +} diff --git a/test/TestAsset/HydratorStrategyEntityB.php b/test/TestAsset/HydratorStrategyEntityB.php index 9aae0a4c1..273cb481d 100644 --- a/test/TestAsset/HydratorStrategyEntityB.php +++ b/test/TestAsset/HydratorStrategyEntityB.php @@ -44,4 +44,4 @@ public function setField2($value) $this->field2 = $value; return $this; } -} \ No newline at end of file +}