diff --git a/src/Hydrator/ClassMethods.php b/src/Hydrator/ClassMethods.php index 7034c683f..3ee8da8dd 100644 --- a/src/Hydrator/ClassMethods.php +++ b/src/Hydrator/ClassMethods.php @@ -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; } diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 0d8afab8f..2b9fe0462 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -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(); @@ -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); + } }