Thanks to visit codestin.com
Credit goes to github.com

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

Commit 30aa565

Browse files
committed
ClassMethods Hydrator ignores invalid values

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/Hydrator/ClassMethods.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ public function hydrate(array $data, $object)
120120
$property = preg_replace_callback('/(_[a-z])/', $transform, $property);
121121
}
122122
$method = 'set' . ucfirst($property);
123-
$object->$method($value);
123+
if (method_exists($object, $method)) {
124+
$object->$method($value);
125+
}
124126
}
125127
return $object;
126128
}

test/HydratorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@
3737
*/
3838
class HydratorTest extends \PHPUnit_Framework_TestCase
3939
{
40+
41+
/**
42+
* @var ClassMethodsCamelCase
43+
*/
44+
protected $classMethodsCamelCase;
45+
46+
/**
47+
* @var ClassMethodsCamelCaseMissing
48+
*/
49+
protected $classMethodsCamelCaseMissing;
50+
51+
/**
52+
* @var ClassMethodsUnderscore
53+
*/
54+
protected $classMethodsUnderscore;
55+
4056
public function setUp()
4157
{
4258
$this->classMethodsCamelCase = new ClassMethodsCamelCase();
@@ -93,4 +109,16 @@ public function testHydratorClassMethodsUnderscore()
93109
$this->assertEquals($test->getFooBar(), 'foo');
94110
$this->assertEquals($test->getFooBarBaz(), 'bar');
95111
}
112+
113+
public function testHydratorClassMethodsIgnoresInvalidValues()
114+
{
115+
$hydrator = new ClassMethods(false);
116+
$data = array(
117+
'foo_bar' => '1',
118+
'foo_bar_baz' => '2',
119+
'invalid' => 'value'
120+
);
121+
$test = $hydrator->hydrate($data, $this->classMethodsUnderscore);
122+
$this->assertSame($this->classMethodsUnderscore, $test);
123+
}
96124
}

0 commit comments

Comments
 (0)