Skip to content

Commit

Permalink
Ignore tags with null or empty values (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil4 authored Jul 22, 2022
1 parent ef77c37 commit 2bd2d34
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/PHPExif/Hydrator/Mutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class Mutator implements HydratorInterface
public function hydrate($object, array $data) : void
{
foreach ($data as $property => $value) {
$mutator = $this->determineMutator($property);
if ($value !== null && $value !== '') {
$mutator = $this->determineMutator($property);

if (method_exists($object, $mutator)) {
$object->$mutator($value); // @phpstan-ignore-line, PhpStan does not like variadic calls
if (method_exists($object, $mutator)) {
$object->$mutator($value); // @phpstan-ignore-line, PhpStan does not like variadic calls
}
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPExif/Hydrator/MutatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,41 @@ public function testHydrateCallsMutatorsOnObject()
$hydrator = new \PHPExif\Hydrator\Mutator;
$hydrator->hydrate($mock, $input);
}

/**
* @group hydrator
* @covers \PHPExif\Hydrator\Mutator::hydrate
*/
public function testHydrateCallsEmptyValues()
{
// input data
$input = array(
'foo' => null,
'bar' => '',
);

// create mock
$mock = $this->getMockBuilder('TestClass')
->setMethods(array('setFoo', 'setBar'))
->getMock();

$mock->expects($this->exactly(0))
->method('setFoo');
$mock->expects($this->exactly(0))
->method('setBar');

// do the test
$hydrator = new \PHPExif\Hydrator\Mutator;
$hydrator->hydrate($mock, $input);
}
}

class TestClass
{
public function setFoo()
{
}

public function setBar()
{
}
Expand Down

0 comments on commit 2bd2d34

Please sign in to comment.