diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 679df32c8..5ad76c0a7 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -177,7 +177,7 @@ public function hydrate(array $data, $object) if ($this->underscoreSeparatedKeys) { $method = preg_replace_callback('/(_[a-z])/', $transform, $method); } - if (method_exists($object, $method)) { + if (is_callable(array($object, $method))) { $value = $this->hydrateValue($property, $value); $object->$method($value); diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 69cd24784..2baca1e47 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -17,6 +17,8 @@ use Zend\Stdlib\Hydrator\Filter\FilterComposite; use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCase; use ZendTest\Stdlib\TestAsset\ClassMethodsFilterProviderInterface; +use ZendTest\Stdlib\TestAsset\ClassMethodsMagicMethodSetter; +use ZendTest\Stdlib\TestAsset\ClassMethodsProtectedSetter; use ZendTest\Stdlib\TestAsset\ClassMethodsUnderscore; use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCaseMissing; use ZendTest\Stdlib\TestAsset\ClassMethodsInvalidParameter; @@ -397,4 +399,24 @@ public function testObjectBasedFilters() $this->assertSame("bar", $data["foo"]); $this->assertSame("foo", $data["bar"]); } + + public function testHydratorClassMethodsWithProtectedSetter() + { + $hydrator = new ClassMethods(false); + $object = new ClassMethodsProtectedSetter(); + $hydrator->hydrate(array('foo' => 'bar', 'bar' => 'BAR'), $object); + $data = $hydrator->extract($object); + + $this->assertEquals($data['bar'], 'BAR'); + } + + public function testHydratorClassMethodsWithMagicMethodSetter() + { + $hydrator = new ClassMethods(false); + $object = new ClassMethodsMagicMethodSetter(); + $hydrator->hydrate(array('foo' => 'bar'), $object); + $data = $hydrator->extract($object); + + $this->assertEquals($data['foo'], 'bar'); + } } diff --git a/test/TestAsset/ClassMethodsMagicMethodSetter.php b/test/TestAsset/ClassMethodsMagicMethodSetter.php new file mode 100644 index 000000000..8cf00a150 --- /dev/null +++ b/test/TestAsset/ClassMethodsMagicMethodSetter.php @@ -0,0 +1,27 @@ + 3 && strtolower(substr($method, 3)) == 'foo') { + $this->foo = $args[0]; + } + } + + public function getFoo() + { + return $this->foo; + } +} diff --git a/test/TestAsset/ClassMethodsProtectedSetter.php b/test/TestAsset/ClassMethodsProtectedSetter.php new file mode 100644 index 000000000..9f9085257 --- /dev/null +++ b/test/TestAsset/ClassMethodsProtectedSetter.php @@ -0,0 +1,31 @@ +foo = $foo; + } + + public function setBar($bar) + { + $this->bar = $bar; + } + + public function getBar() + { + return $this->bar; + } +}