diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index b5e4894ee..c50d821d0 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -136,7 +136,9 @@ public function extract($object) $attribute = $method; if (preg_match('/^get/', $method)) { $attribute = substr($method, 3); - $attribute = lcfirst($attribute); + if(!property_exists($object, $attribute)) { + $attribute = lcfirst($attribute); + } } if ($this->underscoreSeparatedKeys) { @@ -185,5 +187,4 @@ public function hydrate(array $data, $object) return $object; } - } diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 2baca1e47..bbccfe8c6 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -16,6 +16,7 @@ use Zend\Stdlib\Hydrator\ArraySerializable; use Zend\Stdlib\Hydrator\Filter\FilterComposite; use ZendTest\Stdlib\TestAsset\ClassMethodsCamelCase; +use ZendTest\Stdlib\TestAsset\ClassMethodsTitleCase; use ZendTest\Stdlib\TestAsset\ClassMethodsFilterProviderInterface; use ZendTest\Stdlib\TestAsset\ClassMethodsMagicMethodSetter; use ZendTest\Stdlib\TestAsset\ClassMethodsProtectedSetter; @@ -44,6 +45,11 @@ class HydratorTest extends \PHPUnit_Framework_TestCase */ protected $classMethodsCamelCase; + /** + * @var ClassMethodsTitleCase + */ + protected $classMethodsTitleCase; + /** * @var ClassMethodsCamelCaseMissing */ @@ -67,6 +73,7 @@ class HydratorTest extends \PHPUnit_Framework_TestCase public function setUp() { $this->classMethodsCamelCase = new ClassMethodsCamelCase(); + $this->classMethodsTitleCase = new ClassMethodsTitleCase(); $this->classMethodsCamelCaseMissing = new ClassMethodsCamelCaseMissing(); $this->classMethodsUnderscore = new ClassMethodsUnderscore(); $this->classMethodsInvalidParameter = new ClassMethodsInvalidParameter(); @@ -82,6 +89,12 @@ public function testInitiateValues() $this->assertEquals($this->classMethodsCamelCase->isBar(), true); $this->assertEquals($this->classMethodsCamelCase->getHasFoo(), true); $this->assertEquals($this->classMethodsCamelCase->hasBar(), true); + $this->assertEquals($this->classMethodsTitleCase->getFooBar(), '1'); + $this->assertEquals($this->classMethodsTitleCase->getFooBarBaz(), '2'); + $this->assertEquals($this->classMethodsTitleCase->getIsFoo(), true); + $this->assertEquals($this->classMethodsTitleCase->getIsBar(), true); + $this->assertEquals($this->classMethodsTitleCase->getHasFoo(), true); + $this->assertEquals($this->classMethodsTitleCase->getHasBar(), true); $this->assertEquals($this->classMethodsUnderscore->getFooBar(), '1'); $this->assertEquals($this->classMethodsUnderscore->getFooBarBaz(), '2'); $this->assertEquals($this->classMethodsUnderscore->getIsFoo(), true); @@ -143,6 +156,45 @@ public function testHydratorClassMethodsCamelCase() $this->assertEquals($test->hasBar(), false); } + + + public function testHydratorClassMethodsTitleCase() + { + $hydrator = new ClassMethods(false); + $datas = $hydrator->extract($this->classMethodsTitleCase); + $this->assertTrue(isset($datas['FooBar'])); + $this->assertEquals($datas['FooBar'], '1'); + $this->assertTrue(isset($datas['FooBarBaz'])); + $this->assertFalse(isset($datas['foo_bar'])); + $this->assertTrue(isset($datas['IsFoo'])); + $this->assertEquals($datas['IsFoo'], true); + $this->assertTrue(isset($datas['IsBar'])); + $this->assertEquals($datas['IsBar'], true); + $this->assertTrue(isset($datas['HasFoo'])); + $this->assertEquals($datas['HasFoo'], true); + $this->assertTrue(isset($datas['HasBar'])); + $this->assertEquals($datas['HasBar'], true); + $test = $hydrator->hydrate( + array( + 'FooBar' => 'foo', + 'FooBarBaz' => 'bar', + 'IsFoo' => false, + 'IsBar' => false, + 'HasFoo' => false, + 'HasBar' => false, + ), + $this->classMethodsTitleCase + ); + $this->assertSame($this->classMethodsTitleCase, $test); + $this->assertEquals($test->getFooBar(), 'foo'); + $this->assertEquals($test->getFooBarBaz(), 'bar'); + $this->assertEquals($test->getIsFoo(), false); + $this->assertEquals($test->getIsBar(), false); + $this->assertEquals($test->getHasFoo(), false); + $this->assertEquals($test->getHasBar(), false); + } + + public function testHydratorClassMethodsUnderscore() { $hydrator = new ClassMethods(true); diff --git a/test/TestAsset/ClassMethodsTitleCase.php b/test/TestAsset/ClassMethodsTitleCase.php new file mode 100644 index 000000000..8375e18a9 --- /dev/null +++ b/test/TestAsset/ClassMethodsTitleCase.php @@ -0,0 +1,91 @@ +FooBar; + } + + public function setFooBar($value) + { + $this->FooBar = $value; + return $this; + } + + public function getFooBarBaz() + { + return $this->FooBarBaz; + } + + public function setFooBarBaz($value) + { + $this->FooBarBaz = $value; + return $this; + } + + public function getIsFoo() + { + return $this->IsFoo; + } + + public function setIsFoo($IsFoo) + { + $this->IsFoo = $IsFoo; + return $this; + } + + public function getIsBar() + { + return $this->IsBar; + } + + public function setIsBar($IsBar) + { + $this->IsBar = $IsBar; + return $this; + } + + public function getHasFoo() + { + return $this->HasFoo; + } + + public function getHasBar() + { + return $this->HasBar; + } + + public function setHasFoo($HasFoo) + { + $this->HasFoo = $HasFoo; + return $this; + } + + public function setHasBar($HasBar) + { + $this->HasBar = $HasBar; + } +}