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

Commit

Permalink
Merge branch 'feature/stdlib-hydrator-is'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/Hydrator/ClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
38 changes: 36 additions & 2 deletions test/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
26 changes: 26 additions & 0 deletions test/TestAsset/ClassMethodsCamelCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class ClassMethodsCamelCase

protected $fooBarBaz = '2';

protected $isFoo = true;

protected $isBar = true;

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

0 comments on commit a2df21b

Please sign in to comment.