diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index b7566dc23..d48082987 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -58,31 +58,28 @@ public function extract($object) }; $attributes = array(); $methods = get_class_methods($object); + foreach ($methods as $method) { - if (preg_match('/^get[A-Z]\w*/', $method)) { + if (!preg_match('/^(get|has|is)[A-Z]\w*/', $method)) { + continue; + } + if (preg_match('/^get/', $method)) { // setter verification $setter = preg_replace('/^get/', 'set', $method); - if (!in_array($setter, $methods)) { - continue; - } $attribute = substr($method, 3); $attribute = lcfirst($attribute); - if ($this->underscoreSeparatedKeys) { - $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute); - } - $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); - } elseif (preg_match('/^is[A-Z]\w*/', $method)) { + } else { // setter verification $setter = 'set' . ucfirst($method); - if (!in_array($setter, $methods)) { - continue; - } $attribute = $method; - if ($this->underscoreSeparatedKeys) { - $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute); - } - $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); } + if (!in_array($setter, $methods)) { + continue; + } + if ($this->underscoreSeparatedKeys) { + $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute); + } + $attributes[$attribute] = $this->extractValue($attribute, $object->$method()); } return $attributes; diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 62913ce5a..30615e116 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -60,10 +60,14 @@ public function testInitiateValues() $this->assertEquals($this->classMethodsCamelCase->getFooBarBaz(), '2'); $this->assertEquals($this->classMethodsCamelCase->getIsFoo(), true); $this->assertEquals($this->classMethodsCamelCase->isBar(), true); + $this->assertEquals($this->classMethodsCamelCase->getHasFoo(), true); + $this->assertEquals($this->classMethodsCamelCase->hasBar(), true); $this->assertEquals($this->classMethodsUnderscore->getFooBar(), '1'); $this->assertEquals($this->classMethodsUnderscore->getFooBarBaz(), '2'); $this->assertEquals($this->classMethodsUnderscore->getIsFoo(), true); $this->assertEquals($this->classMethodsUnderscore->isBar(), true); + $this->assertEquals($this->classMethodsUnderscore->getHasFoo(), true); + $this->assertEquals($this->classMethodsUnderscore->hasBar(), true); } public function testHydratorReflection() @@ -95,12 +99,18 @@ public function testHydratorClassMethodsCamelCase() $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->classMethodsCamelCase ); @@ -109,6 +119,8 @@ public function testHydratorClassMethodsCamelCase() $this->assertEquals($test->getFooBarBaz(), 'bar'); $this->assertEquals($test->getIsFoo(), false); $this->assertEquals($test->isBar(), false); + $this->assertEquals($test->getHasFoo(), false); + $this->assertEquals($test->hasBar(), false); } public function testHydratorClassMethodsCamelCaseWithSetterMissing() @@ -139,12 +151,20 @@ public function testHydratorClassMethodsUnderscore() $this->assertTrue(isset($datas['is_bar'])); $this->assertFalse(isset($datas['isBar'])); $this->assertEquals($datas['is_bar'], true); + $this->assertTrue(isset($datas['has_foo'])); + $this->assertFalse(isset($datas['hasFoo'])); + $this->assertEquals($datas['has_foo'], true); + $this->assertTrue(isset($datas['has_bar'])); + $this->assertFalse(isset($datas['hasBar'])); + $this->assertEquals($datas['has_bar'], true); $test = $hydrator->hydrate( array( 'foo_bar' => 'foo', 'foo_bar_baz' => 'bar', 'is_foo' => false, 'is_bar' => false, + 'has_foo' => false, + 'has_bar' => false, ), $this->classMethodsUnderscore ); @@ -153,6 +173,8 @@ public function testHydratorClassMethodsUnderscore() $this->assertEquals($test->getFooBarBaz(), 'bar'); $this->assertEquals($test->getIsFoo(), false); $this->assertEquals($test->isBar(), false); + $this->assertEquals($test->getHasFoo(), false); + $this->assertEquals($test->hasBar(), false); } public function testHydratorClassMethodsIgnoresInvalidValues() diff --git a/test/TestAsset/ClassMethodsCamelCase.php b/test/TestAsset/ClassMethodsCamelCase.php index 545a142f9..50ccd4ec6 100644 --- a/test/TestAsset/ClassMethodsCamelCase.php +++ b/test/TestAsset/ClassMethodsCamelCase.php @@ -20,6 +20,10 @@ class ClassMethodsCamelCase protected $isBar = true; + protected $hasFoo = true; + + protected $hasBar = true; + public function getFooBar() { return $this->fooBar; @@ -63,4 +67,26 @@ public function setIsBar($value) $this->isBar = $value; return $this; } + + public function getHasFoo() + { + return $this->hasFoo; + } + + public function setHasFoo($value) + { + $this->hasFoo = $value; + return $this; + } + + public function hasBar() + { + return $this->hasBar; + } + + public function setHasBar($value) + { + $this->hasBar = $value; + return $this; + } } diff --git a/test/TestAsset/ClassMethodsUnderscore.php b/test/TestAsset/ClassMethodsUnderscore.php index b30b458de..a51883805 100644 --- a/test/TestAsset/ClassMethodsUnderscore.php +++ b/test/TestAsset/ClassMethodsUnderscore.php @@ -20,6 +20,10 @@ class ClassMethodsUnderscore protected $is_bar = true; + protected $has_foo = true; + + protected $has_bar = true; + public function getFooBar() { return $this->foo_bar; @@ -63,4 +67,26 @@ public function setIsBar($value) $this->is_bar = $value; return $this; } + + public function getHasFoo() + { + return $this->has_foo; + } + + public function setHasFoo($value) + { + $this->has_foo = $value; + return $this; + } + + public function hasBar() + { + return $this->has_bar; + } + + public function setHasBar($value) + { + $this->has_bar = $value; + return $this; + } }