Skip to content

Commit 2bd2d34

Browse files
authored
Ignore tags with null or empty values (#50)
1 parent ef77c37 commit 2bd2d34

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/PHPExif/Hydrator/Mutator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ class Mutator implements HydratorInterface
3333
public function hydrate($object, array $data) : void
3434
{
3535
foreach ($data as $property => $value) {
36-
$mutator = $this->determineMutator($property);
36+
if ($value !== null && $value !== '') {
37+
$mutator = $this->determineMutator($property);
3738

38-
if (method_exists($object, $mutator)) {
39-
$object->$mutator($value); // @phpstan-ignore-line, PhpStan does not like variadic calls
39+
if (method_exists($object, $mutator)) {
40+
$object->$mutator($value); // @phpstan-ignore-line, PhpStan does not like variadic calls
41+
}
4042
}
4143
}
4244
}

tests/PHPExif/Hydrator/MutatorTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,41 @@ public function testHydrateCallsMutatorsOnObject()
6161
$hydrator = new \PHPExif\Hydrator\Mutator;
6262
$hydrator->hydrate($mock, $input);
6363
}
64+
65+
/**
66+
* @group hydrator
67+
* @covers \PHPExif\Hydrator\Mutator::hydrate
68+
*/
69+
public function testHydrateCallsEmptyValues()
70+
{
71+
// input data
72+
$input = array(
73+
'foo' => null,
74+
'bar' => '',
75+
);
76+
77+
// create mock
78+
$mock = $this->getMockBuilder('TestClass')
79+
->setMethods(array('setFoo', 'setBar'))
80+
->getMock();
81+
82+
$mock->expects($this->exactly(0))
83+
->method('setFoo');
84+
$mock->expects($this->exactly(0))
85+
->method('setBar');
86+
87+
// do the test
88+
$hydrator = new \PHPExif\Hydrator\Mutator;
89+
$hydrator->hydrate($mock, $input);
90+
}
6491
}
6592

6693
class TestClass
6794
{
95+
public function setFoo()
96+
{
97+
}
98+
6899
public function setBar()
69100
{
70101
}

0 commit comments

Comments
 (0)