diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 012e01150..4218eef3c 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -33,18 +33,18 @@ class ClassMethods implements HydratorInterface { /** - * CamelCase usage to extract attribute with getter/setter method name + * Flag defining whether array keys are underscore-separated (true) or camel case (false) * @var boolean */ - protected $useCamelCase; + protected $underscoreSeparatedKeys; /** * Define if extract values will use camel case or name with underscore - * @param boolean $useCamelCase + * @param boolean $underscoreSeparatedKeys */ - public function __construct($useCamelCase = true) + public function __construct($underscoreSeparatedKeys = true) { - $this->useCamelCase = $useCamelCase; + $this->underscoreSeparatedKeys = $underscoreSeparatedKeys; } /** @@ -81,7 +81,7 @@ public function extract($object) } $attribute = substr($method, 3); $attribute = lcfirst($attribute); - if (!$this->useCamelCase) { + if ($this->underscoreSeparatedKeys) { $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute); } @@ -131,7 +131,7 @@ public function hydrate(array $data, $object) }; foreach ($data as $property => $value) { - if (!$this->useCamelCase) { + if ($this->underscoreSeparatedKeys) { $property = preg_replace_callback('/(_[a-z])/', $transform, $property); } $method = 'set' . ucfirst($property); diff --git a/test/HydratorTest.php b/test/HydratorTest.php index d6fd41d23..85efb43c8 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -95,7 +95,7 @@ public function testHydratorReflection() public function testHydratorClassMethodsCamelCase() { - $hydrator = new ClassMethods(true); + $hydrator = new ClassMethods(false); $datas = $hydrator->extract($this->classMethodsCamelCase); $this->assertTrue(isset($datas['fooBar'])); $this->assertEquals($datas['fooBar'], '1'); @@ -109,7 +109,7 @@ public function testHydratorClassMethodsCamelCase() public function testHydratorClassMethodsCamelCaseWithSetterMissing() { - $hydrator = new ClassMethods(true); + $hydrator = new ClassMethods(false); $datas = $hydrator->extract($this->classMethodsCamelCaseMissing); $this->assertTrue(isset($datas['fooBar'])); $this->assertEquals($datas['fooBar'], '1'); @@ -123,7 +123,7 @@ public function testHydratorClassMethodsCamelCaseWithSetterMissing() public function testHydratorClassMethodsUnderscore() { - $hydrator = new ClassMethods(false); + $hydrator = new ClassMethods(true); $datas = $hydrator->extract($this->classMethodsUnderscore); $this->assertTrue(isset($datas['foo_bar'])); $this->assertEquals($datas['foo_bar'], '1'); @@ -137,7 +137,7 @@ public function testHydratorClassMethodsUnderscore() public function testHydratorClassMethodsIgnoresInvalidValues() { - $hydrator = new ClassMethods(false); + $hydrator = new ClassMethods(true); $data = array( 'foo_bar' => '1', 'foo_bar_baz' => '2', @@ -146,4 +146,18 @@ public function testHydratorClassMethodsIgnoresInvalidValues() $test = $hydrator->hydrate($data, $this->classMethodsUnderscore); $this->assertSame($this->classMethodsUnderscore, $test); } + + public function testHydratorClassMethodsDefaultBehaviorIsConvertUnderscoreToCamelCase() + { + $hydrator = new ClassMethods(); + $datas = $hydrator->extract($this->classMethodsUnderscore); + $this->assertTrue(isset($datas['foo_bar'])); + $this->assertEquals($datas['foo_bar'], '1'); + $this->assertTrue(isset($datas['foo_bar_baz'])); + $this->assertFalse(isset($datas['fooBar'])); + $test = $hydrator->hydrate(array('foo_bar' => 'foo', 'foo_bar_baz' => 'bar'), $this->classMethodsUnderscore); + $this->assertSame($this->classMethodsUnderscore, $test); + $this->assertEquals($test->getFooBar(), 'foo'); + $this->assertEquals($test->getFooBarBaz(), 'bar'); + } }