diff --git a/src/PropertyAccessor/PropertyAccessor.php b/src/PropertyAccessor/PropertyAccessor.php index 746529e..dd2e104 100644 --- a/src/PropertyAccessor/PropertyAccessor.php +++ b/src/PropertyAccessor/PropertyAccessor.php @@ -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) { diff --git a/test/AutoMapperTest.php b/test/AutoMapperTest.php index 01c2730..8cc3f61 100644 --- a/test/AutoMapperTest.php +++ b/test/AutoMapperTest.php @@ -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); + } }