Skip to content

Commit 64efc83

Browse files
committed
Handling case when a class typed property receives a null value
1 parent 05ca5bf commit 64efc83

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

src/Mapper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ public function map(array $input, string $className)
6666
$property->setValue($object, $value);
6767
}
6868
} else {
69-
$property->setValue($object, $this->castCustom($value, $type->getName()));
69+
if (
70+
$value === null
71+
&& empty($this->getCustomMapper($type->getName()))
72+
) {
73+
$property->setValue($object, null);
74+
}
75+
else {
76+
$property->setValue($object, $this->castCustom($value, $type->getName()));
77+
}
7078
}
7179
} else {
7280
if (empty($docBlockType)) {

test/Unit/MapperTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Emul\ArrayToClassMapper\DocBlock\DocBlockParser;
99
use Emul\ArrayToClassMapper\DocBlock\Entity\DocBlockType;
1010
use Emul\ArrayToClassMapper\Test\Unit\Stub\ClassDocBlockTypedArrayStub;
11+
use Emul\ArrayToClassMapper\Test\Unit\Stub\ClassTypedStub;
1112
use Emul\ArrayToClassMapper\Test\Unit\Stub\CustomDocBlockTypedArrayStub;
1213
use Emul\ArrayToClassMapper\Test\Unit\Stub\CustomDocBlockTypedStub;
1314
use Emul\ArrayToClassMapper\Test\Unit\Stub\CustomStub;
@@ -29,6 +30,20 @@ protected function setUp(): void
2930
$this->docBlockParser = \Mockery::mock(DocBlockParser::class);
3031
}
3132

33+
public function testMapWhenNullGiven_shouldSet()
34+
{
35+
$mapper = $this->getMapper();
36+
37+
$this->expectTypeRetrievedFromDocBlock('', null);
38+
39+
$input = ['int' => null];
40+
41+
/** @var ScalarTypedStub $result */
42+
$result = $mapper->map($input, ScalarTypedStub::class);
43+
44+
$this->assertNull($result->getInt());
45+
}
46+
3247
public function testMapWhenBuiltInTypedPropertyGiven_shouldCast()
3348
{
3449
$mapper = $this->getMapper();
@@ -101,6 +116,34 @@ public function testMapWhenArrayTypedPropertyGivenWithCustomDockBlockType_should
101116
$this->assertSame(2, $mappedArray[1]->getInt());
102117
}
103118

119+
public function testMapWhenClassTypedPropertyGivenWithNullValue_shouldSetToNull()
120+
{
121+
$mapper = $this->getMapper();
122+
123+
$this->expectTypeRetrievedFromDocBlock('', null);
124+
125+
$input = ['object' => null];
126+
127+
/** @var ClassTypedStub $result */
128+
$result = $mapper->map($input, ClassTypedStub::class);
129+
130+
$this->assertNull($result->getObject());
131+
}
132+
133+
public function testMapWhenClassTypedPropertyGiven_shouldMap()
134+
{
135+
$mapper = $this->getMapper();
136+
137+
$this->expectTypeRetrievedFromDocBlock('', null);
138+
139+
$input = ['object' => ['int' => 1]];
140+
141+
/** @var ClassTypedStub $result */
142+
$result = $mapper->map($input, ClassTypedStub::class);
143+
144+
$this->assertSame(1, $result->getObject()->getInt());
145+
}
146+
104147
public function testMapWhenArrayTypedPropertyGivenWithCustomDockBlockTypeAndCustomMapperProvided_shouldMapElementsWithGivenMapper()
105148
{
106149
$currentTime = '2020-01-01 01:01:01';

test/Unit/Stub/ClassTypedStub.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Emul\ArrayToClassMapper\Test\Unit\Stub;
5+
6+
class ClassTypedStub
7+
{
8+
private ?ScalarTypedStub $object;
9+
10+
public function getObject(): ?ScalarTypedStub
11+
{
12+
return $this->object;
13+
}
14+
}

test/Unit/Stub/CustomTypedStub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
class CustomTypedStub
99
{
10-
private Carbon $currentTime;
10+
private ?Carbon $currentTime;
1111

12-
public function getCurrentTime(): Carbon
12+
public function getCurrentTime(): ?Carbon
1313
{
1414
return $this->currentTime;
1515
}

test/Unit/Stub/ScalarTypedStub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function __construct()
1212
throw new \Exception('Constructor called');
1313
}
1414

15-
public function getInt(): int
15+
public function getInt(): ?int
1616
{
1717
return $this->int;
1818
}

0 commit comments

Comments
 (0)