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/stdlib-hydrator-class-methods-has-support' into …
Browse files Browse the repository at this point in the history
…releases/2.0.1
  • Loading branch information
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/Hydrator/ClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions test/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
);
Expand All @@ -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()
Expand Down Expand Up @@ -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
);
Expand All @@ -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()
Expand Down
26 changes: 26 additions & 0 deletions test/TestAsset/ClassMethodsCamelCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class ClassMethodsCamelCase

protected $isBar = true;

protected $hasFoo = true;

protected $hasBar = true;

public function getFooBar()
{
return $this->fooBar;
Expand Down Expand Up @@ -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;
}
}
26 changes: 26 additions & 0 deletions test/TestAsset/ClassMethodsUnderscore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 5ecbc99

Please sign in to comment.