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

Commit

Permalink
ClassMethods Hydrator ignores invalid values
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Hydrator/ClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ public function hydrate(array $data, $object)
$property = preg_replace_callback('/(_[a-z])/', $transform, $property);
}
$method = 'set' . ucfirst($property);
$object->$method($value);
if (method_exists($object, $method)) {
$object->$method($value);
}
}
return $object;
}
Expand Down
28 changes: 28 additions & 0 deletions test/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@
*/
class HydratorTest extends \PHPUnit_Framework_TestCase
{

/**
* @var ClassMethodsCamelCase
*/
protected $classMethodsCamelCase;

/**
* @var ClassMethodsCamelCaseMissing
*/
protected $classMethodsCamelCaseMissing;

/**
* @var ClassMethodsUnderscore
*/
protected $classMethodsUnderscore;

public function setUp()
{
$this->classMethodsCamelCase = new ClassMethodsCamelCase();
Expand Down Expand Up @@ -93,4 +109,16 @@ public function testHydratorClassMethodsUnderscore()
$this->assertEquals($test->getFooBar(), 'foo');
$this->assertEquals($test->getFooBarBaz(), 'bar');
}

public function testHydratorClassMethodsIgnoresInvalidValues()
{
$hydrator = new ClassMethods(false);
$data = array(
'foo_bar' => '1',
'foo_bar_baz' => '2',
'invalid' => 'value'
);
$test = $hydrator->hydrate($data, $this->classMethodsUnderscore);
$this->assertSame($this->classMethodsUnderscore, $test);
}
}

0 comments on commit 30aa565

Please sign in to comment.