Skip to content

Commit

Permalink
#59 Check for null before fetching target properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-gerarts committed May 5, 2020
1 parent 420c43d commit 0e9aa94
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/PropertyAccessor/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ private function isPublic($object, string $propertyName): bool
* @return iterable|\ReflectionProperty[]
*/
private function getReflectionProperties($object): iterable {
if ($object === null) {
return;
}

$reflectionClass = new \ReflectionObject($object);
$properties = $reflectionClass->getProperties();
foreach ($properties as $property) {
Expand Down
23 changes: 23 additions & 0 deletions test/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,27 @@ public function testAnExceptionIsThrownForNoIterableSourceInMultpleMappings()

$mapper->mapMultiple($sourceCollection, Destination::class);
}

/**
* @group test
*/
public function testItMapsANullObjectReturnedFromConstructorToNull()
{
$this->config->registerMapping(DataType::ARRAY, Address::class)
->beConstructedUsing(function () { return null; });
$this->config->registerMapping(DataType::ARRAY, Person::class)
->forMember('adres', Operation::mapTo(Address::class, true));
$mapper = new AutoMapper($this->config);

$source = [
'adres' => [
'street' => 'Main Street',
'number' => '314'
]
];

$result = $mapper->map($source, Person::class);

$this->assertNull($result->adres);
}
}

0 comments on commit 0e9aa94

Please sign in to comment.