Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit b93694e

Browse files
committed
Merge branch 'hotfix/classmethods-hydrator-default-to-underscore' of https://github.com/adamlundrigan/zf2 into feature/hydrator-classmethods-underscores
2 parents 63fe94c + d6b7e50 commit b93694e

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

src/Hydrator/ClassMethods.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@
3333
class ClassMethods implements HydratorInterface
3434
{
3535
/**
36-
* CamelCase usage to extract attribute with getter/setter method name
36+
* Flag defining whether array keys are underscore-separated (true) or camel case (false)
3737
* @var boolean
3838
*/
39-
protected $useCamelCase;
39+
protected $underscoreSeparatedKeys;
4040

4141
/**
4242
* Define if extract values will use camel case or name with underscore
43-
* @param boolean $useCamelCase
43+
* @param boolean $underscoreSeparatedKeys
4444
*/
45-
public function __construct($useCamelCase = true)
45+
public function __construct($underscoreSeparatedKeys = true)
4646
{
47-
$this->useCamelCase = $useCamelCase;
47+
$this->underscoreSeparatedKeys = $underscoreSeparatedKeys;
4848
}
4949

5050
/**
@@ -81,7 +81,7 @@ public function extract($object)
8181
}
8282
$attribute = substr($method, 3);
8383
$attribute = lcfirst($attribute);
84-
if (!$this->useCamelCase) {
84+
if ($this->underscoreSeparatedKeys) {
8585
$attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute);
8686
}
8787

@@ -131,7 +131,7 @@ public function hydrate(array $data, $object)
131131
};
132132

133133
foreach ($data as $property => $value) {
134-
if (!$this->useCamelCase) {
134+
if ($this->underscoreSeparatedKeys) {
135135
$property = preg_replace_callback('/(_[a-z])/', $transform, $property);
136136
}
137137
$method = 'set' . ucfirst($property);

test/HydratorTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testHydratorReflection()
9595

9696
public function testHydratorClassMethodsCamelCase()
9797
{
98-
$hydrator = new ClassMethods(true);
98+
$hydrator = new ClassMethods(false);
9999
$datas = $hydrator->extract($this->classMethodsCamelCase);
100100
$this->assertTrue(isset($datas['fooBar']));
101101
$this->assertEquals($datas['fooBar'], '1');
@@ -109,7 +109,7 @@ public function testHydratorClassMethodsCamelCase()
109109

110110
public function testHydratorClassMethodsCamelCaseWithSetterMissing()
111111
{
112-
$hydrator = new ClassMethods(true);
112+
$hydrator = new ClassMethods(false);
113113
$datas = $hydrator->extract($this->classMethodsCamelCaseMissing);
114114
$this->assertTrue(isset($datas['fooBar']));
115115
$this->assertEquals($datas['fooBar'], '1');
@@ -123,7 +123,7 @@ public function testHydratorClassMethodsCamelCaseWithSetterMissing()
123123

124124
public function testHydratorClassMethodsUnderscore()
125125
{
126-
$hydrator = new ClassMethods(false);
126+
$hydrator = new ClassMethods(true);
127127
$datas = $hydrator->extract($this->classMethodsUnderscore);
128128
$this->assertTrue(isset($datas['foo_bar']));
129129
$this->assertEquals($datas['foo_bar'], '1');
@@ -137,7 +137,7 @@ public function testHydratorClassMethodsUnderscore()
137137

138138
public function testHydratorClassMethodsIgnoresInvalidValues()
139139
{
140-
$hydrator = new ClassMethods(false);
140+
$hydrator = new ClassMethods(true);
141141
$data = array(
142142
'foo_bar' => '1',
143143
'foo_bar_baz' => '2',
@@ -146,4 +146,18 @@ public function testHydratorClassMethodsIgnoresInvalidValues()
146146
$test = $hydrator->hydrate($data, $this->classMethodsUnderscore);
147147
$this->assertSame($this->classMethodsUnderscore, $test);
148148
}
149+
150+
public function testHydratorClassMethodsDefaultBehaviorIsConvertUnderscoreToCamelCase()
151+
{
152+
$hydrator = new ClassMethods();
153+
$datas = $hydrator->extract($this->classMethodsUnderscore);
154+
$this->assertTrue(isset($datas['foo_bar']));
155+
$this->assertEquals($datas['foo_bar'], '1');
156+
$this->assertTrue(isset($datas['foo_bar_baz']));
157+
$this->assertFalse(isset($datas['fooBar']));
158+
$test = $hydrator->hydrate(array('foo_bar' => 'foo', 'foo_bar_baz' => 'bar'), $this->classMethodsUnderscore);
159+
$this->assertSame($this->classMethodsUnderscore, $test);
160+
$this->assertEquals($test->getFooBar(), 'foo');
161+
$this->assertEquals($test->getFooBarBaz(), 'bar');
162+
}
149163
}

0 commit comments

Comments
 (0)