Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'feature/hydrator-class-methods' of https://github.com/c…

  • Loading branch information...
commit 401600973eca569a93fc5f59a113132073ec32a3 2 parents d0dde47 + 33a0806
@weierophinney weierophinney authored
View
11 library/Zend/Stdlib/Hydrator/ClassMethods.php
@@ -71,6 +71,17 @@ public function extract($object)
$attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute);
}
$attributes[$attribute] = $this->extractValue($attribute, $object->$method());
+ } elseif (preg_match('/^is[A-Z]\w*/', $method)) {
+ // 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());
}
}
View
38 tests/ZendTest/Stdlib/HydratorTest.php
@@ -58,8 +58,12 @@ public function testInitiateValues()
{
$this->assertEquals($this->classMethodsCamelCase->getFooBar(), '1');
$this->assertEquals($this->classMethodsCamelCase->getFooBarBaz(), '2');
+ $this->assertEquals($this->classMethodsCamelCase->getIsFoo(), true);
+ $this->assertEquals($this->classMethodsCamelCase->isBar(), 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);
}
public function testHydratorReflection()
@@ -87,10 +91,24 @@ public function testHydratorClassMethodsCamelCase()
$this->assertEquals($datas['fooBar'], '1');
$this->assertTrue(isset($datas['fooBarBaz']));
$this->assertFalse(isset($datas['foo_bar']));
- $test = $hydrator->hydrate(array('fooBar' => 'foo', 'fooBarBaz' => 'bar'), $this->classMethodsCamelCase);
+ $this->assertTrue(isset($datas['isFoo']));
+ $this->assertEquals($datas['isFoo'], true);
+ $this->assertTrue(isset($datas['isBar']));
+ $this->assertEquals($datas['isBar'], true);
+ $test = $hydrator->hydrate(
+ array(
+ 'fooBar' => 'foo',
+ 'fooBarBaz' => 'bar',
+ 'isFoo' => false,
+ 'isBar' => false,
+ ),
+ $this->classMethodsCamelCase
+ );
$this->assertSame($this->classMethodsCamelCase, $test);
$this->assertEquals($test->getFooBar(), 'foo');
$this->assertEquals($test->getFooBarBaz(), 'bar');
+ $this->assertEquals($test->getIsFoo(), false);
+ $this->assertEquals($test->isBar(), false);
}
public function testHydratorClassMethodsCamelCaseWithSetterMissing()
@@ -115,10 +133,26 @@ public function testHydratorClassMethodsUnderscore()
$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->assertTrue(isset($datas['is_foo']));
+ $this->assertFalse(isset($datas['isFoo']));
+ $this->assertEquals($datas['is_foo'], true);
+ $this->assertTrue(isset($datas['is_bar']));
+ $this->assertFalse(isset($datas['isBar']));
+ $this->assertEquals($datas['is_bar'], true);
+ $test = $hydrator->hydrate(
+ array(
+ 'foo_bar' => 'foo',
+ 'foo_bar_baz' => 'bar',
+ 'is_foo' => false,
+ 'is_bar' => false,
+ ),
+ $this->classMethodsUnderscore
+ );
$this->assertSame($this->classMethodsUnderscore, $test);
$this->assertEquals($test->getFooBar(), 'foo');
$this->assertEquals($test->getFooBarBaz(), 'bar');
+ $this->assertEquals($test->getIsFoo(), false);
+ $this->assertEquals($test->isBar(), false);
}
public function testHydratorClassMethodsIgnoresInvalidValues()
View
26 tests/ZendTest/Stdlib/TestAsset/ClassMethodsCamelCase.php
@@ -16,6 +16,10 @@ class ClassMethodsCamelCase
protected $fooBarBaz = '2';
+ protected $isFoo = true;
+
+ protected $isBar = true;
+
public function getFooBar()
{
return $this->fooBar;
@@ -37,4 +41,26 @@ public function setFooBarBaz($value)
$this->fooBarBaz = $value;
return $this;
}
+
+ public function getIsFoo()
+ {
+ return $this->isFoo;
+ }
+
+ public function setIsFoo($value)
+ {
+ $this->isFoo = $value;
+ return $this;
+ }
+
+ public function isBar()
+ {
+ return $this->isBar;
+ }
+
+ public function setIsBar($value)
+ {
+ $this->isBar = $value;
+ return $this;
+ }
}
View
26 tests/ZendTest/Stdlib/TestAsset/ClassMethodsUnderscore.php
@@ -16,6 +16,10 @@ class ClassMethodsUnderscore
protected $foo_bar_baz = '2';
+ protected $is_foo = true;
+
+ protected $is_bar = true;
+
public function getFooBar()
{
return $this->foo_bar;
@@ -37,4 +41,26 @@ public function setFooBarBaz($value)
$this->foo_bar_baz = $value;
return $this;
}
+
+ public function getIsFoo()
+ {
+ return $this->is_foo;
+ }
+
+ public function setIsFoo($value)
+ {
+ $this->is_foo = $value;
+ return $this;
+ }
+
+ public function isBar()
+ {
+ return $this->is_bar;
+ }
+
+ public function setIsBar($value)
+ {
+ $this->is_bar = $value;
+ return $this;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.