diff --git a/CHANGELOG.md b/CHANGELOG.md index 30b1573..8cd2dfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ All notable changes to this project will be documented in this file, in reverse class to the `HydratorPluginManagerFactory` class, and make the `HydratorManager` service an alias to the fully-qualified `HydratorPluginManager` class. +- [#45](https://github.com/zendframework/zend-hydrator/pull/45) changes the + `ClassMethods` hydrator to take into account naming strategies when present, + making it act consistently with the other hydrators. ### Deprecated diff --git a/src/AbstractHydrator.php b/src/AbstractHydrator.php index 332d6d5..c399fd2 100644 --- a/src/AbstractHydrator.php +++ b/src/AbstractHydrator.php @@ -59,6 +59,11 @@ public function getStrategy($name) { if (isset($this->strategies[$name])) { return $this->strategies[$name]; + } elseif ($this->hasNamingStrategy() + && ($hydrated = $this->getNamingStrategy()->hydrate($name)) + && isset($this->strategies[$hydrated]) + ) { + return $this->strategies[$hydrated]; } if (! isset($this->strategies['*'])) { @@ -80,8 +85,17 @@ public function getStrategy($name) */ public function hasStrategy($name) { - return array_key_exists($name, $this->strategies) - || array_key_exists('*', $this->strategies); + if (array_key_exists($name, $this->strategies)) { + return true; + } + + if ($this->hasNamingStrategy() + && array_key_exists($this->getNamingStrategy()->hydrate($name), $this->strategies) + ) { + return true; + } + + return array_key_exists('*', $this->strategies); } /** diff --git a/test/ArraySerializableTest.php b/test/ArraySerializableTest.php index c95cc61..d401e01 100644 --- a/test/ArraySerializableTest.php +++ b/test/ArraySerializableTest.php @@ -22,6 +22,8 @@ */ class ArraySerializableTest extends TestCase { + use HydratorTestTrait; + /** * @var ArraySerializable */ diff --git a/test/ClassMethodsTest.php b/test/ClassMethodsTest.php index dad0fef..9d1b4af 100644 --- a/test/ClassMethodsTest.php +++ b/test/ClassMethodsTest.php @@ -25,6 +25,8 @@ */ class ClassMethodsTest extends TestCase { + use HydratorTestTrait; + /** * @var ClassMethods */ diff --git a/test/HydratorTestTrait.php b/test/HydratorTestTrait.php new file mode 100644 index 0000000..ed33c9c --- /dev/null +++ b/test/HydratorTestTrait.php @@ -0,0 +1,46 @@ +createMock(NamingStrategyInterface::class); + $namingStrategy + ->expects($this->any()) + ->method('hydrate') + ->with($this->anything()) + ->will($this->returnValue('value')) + ; + + $strategy = $this->createMock(StrategyInterface::class); + $strategy + ->expects($this->any()) + ->method('hydrate') + ->with($this->anything()) + ->will($this->returnValue('hydrate')) + ; + + $this->hydrator->setNamingStrategy($namingStrategy); + $this->hydrator->addStrategy('value', $strategy); + + $entity = $this->hydrator->hydrate(['foo_bar_baz' => 'blub'], new SimpleEntity()); + $this->assertSame( + 'hydrate', + $entity->getValue(), + sprintf('Hydrator: %s', get_class($this->hydrator)) + ); + } +} diff --git a/test/ObjectPropertyTest.php b/test/ObjectPropertyTest.php index 8b18331..cb276c7 100644 --- a/test/ObjectPropertyTest.php +++ b/test/ObjectPropertyTest.php @@ -22,6 +22,8 @@ */ class ObjectPropertyTest extends TestCase { + use HydratorTestTrait; + /** * @var ObjectProperty */ diff --git a/test/ReflectionTest.php b/test/ReflectionTest.php index b7b562e..98cefa5 100644 --- a/test/ReflectionTest.php +++ b/test/ReflectionTest.php @@ -21,6 +21,8 @@ */ class ReflectionTest extends TestCase { + use HydratorTestTrait; + /** * @var Reflection */ diff --git a/test/TestAsset/SimpleEntity.php b/test/TestAsset/SimpleEntity.php new file mode 100644 index 0000000..3cdb426 --- /dev/null +++ b/test/TestAsset/SimpleEntity.php @@ -0,0 +1,55 @@ +value = $value; + } + + /** + * @return mixed + */ + public function getValue() + { + return $this->value; + } + + /** + * Exchange internal values from provided array + * + * @param array $array + * @return void + */ + public function exchangeArray(array $array) + { + if (array_key_exists('value', $array)) { + $this->setValue($array['value']); + } + } + + /** + * Return an array representation of the object + * + * @return array + */ + public function getArrayCopy() + { + return ['value' => $this->getValue()]; + } +}