Skip to content

Commit 52a6333

Browse files
committed
Fixing case when property not typed and documented as an array of custom values
1 parent 4a6d854 commit 52a6333

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/.env
66
/auth.json
77
/nbproject/*
8+
/.phpunit.result.cache
89
vendor
910
composer.phar
1011
.DS_Store

.phpunit.result.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Mapper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public function map(array $input, string $className)
7979
settype($value, $docBlockType->getName());
8080
$property->setValue($object, $value);
8181
}
82+
} elseif (!$docBlockType->isSingle()) {
83+
$property->setValue($object, $this->castArray($value, $docBlockType));
8284
} else {
8385
$this->setCustomValue($object, $property, $docBlockType->getName(), $value);
8486
}

test/Unit/MapperTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ public function testMapWhenArrayTypedPropertyGivenWithCustomDockBlockType_should
117117
$this->assertSame(2, $mappedArray[1]->getInt());
118118
}
119119

120+
public function testMapWhenArrayTypedPropertyGivenWithCustomDockBlockTypeWithNullValue_shouldSetToEmptyArray()
121+
{
122+
$mapper = $this->getMapper();
123+
124+
$this->expectTypeRetrievedFromDocBlock(
125+
'/** @var \Emul\ArrayToClassMapper\Test\Unit\Stub\ScalarTypedStub[] */',
126+
new DocBlockType('\Emul\ArrayToClassMapper\Test\Unit\Stub\ScalarTypedStub', false, false, false)
127+
);
128+
129+
$input = [];
130+
131+
/** @var ClassDocBlockTypedArrayStub $result */
132+
$result = $mapper->map($input, ClassDocBlockTypedArrayStub::class);
133+
134+
$this->assertSame([], $result->getObjectArray());
135+
}
136+
120137
public function testMapWhenClassTypedPropertyGivenWithNullValue_shouldSetToNull()
121138
{
122139
$mapper = $this->getMapper();

test/Unit/Stub/ClassDocBlockTypedArrayStub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class ClassDocBlockTypedArrayStub
77
{
88
/** @var \Emul\ArrayToClassMapper\Test\Unit\Stub\ScalarTypedStub[] */
9-
private array $objectArray;
9+
private $objectArray = [];
1010

1111
public function getObjectArray(): array
1212
{

0 commit comments

Comments
 (0)