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

Commit

Permalink
Merge branch 'hotfix/classmethods-hydrator-default-to-underscore' of h…
Browse files Browse the repository at this point in the history
…ttps://github.com/adamlundrigan/zf2 into feature/hydrator-classmethods-underscores
  • Loading branch information
weierophinney committed Jul 6, 2012
2 parents 63fe94c + d6b7e50 commit b93694e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/Hydrator/ClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
22 changes: 18 additions & 4 deletions test/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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',
Expand All @@ -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');
}
}

0 comments on commit b93694e

Please sign in to comment.