Skip to content

Commit

Permalink
Merge branch 'hotfix/reflection-class-null'
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-gerarts committed May 5, 2020
2 parents 420c43d + 2ca9fa7 commit b35e1a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/PropertyAccessor/PropertyAccessor.php
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
20 changes: 20 additions & 0 deletions test/AutoMapperTest.php
Expand Up @@ -719,4 +719,24 @@ public function testAnExceptionIsThrownForNoIterableSourceInMultpleMappings()

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

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 b35e1a3

Please sign in to comment.