Skip to content

Commit 6a7ef17

Browse files
authored
Array field mapping in property doc blocks (#24)
1 parent 29b8f9e commit 6a7ef17

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/Objects/DocBlockParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ private function parseType(string $contents, int &$i): string
5454
continue;
5555
}
5656

57-
if (\in_array($char, ['|', ','])) {
57+
if (\in_array($char, ['|', ',', ':'])) {
5858
$type .= $char;
5959
$lastChar = $char;
6060
$sawSpace = false;
6161
continue;
6262
}
6363

64-
if ($sawSpace && ! \in_array($lastChar, ['|', ','])) {
64+
if ($sawSpace && ! \in_array($lastChar, ['|', ',', ':'])) {
6565
break;
6666
}
6767

tests/MapperTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Jerodev\DataMapper\Mapper;
88
use Jerodev\DataMapper\MapperConfig;
99
use Jerodev\DataMapper\Tests\_Mocks\Aliases;
10+
use Jerodev\DataMapper\Tests\_Mocks\ArrayProperties;
1011
use Jerodev\DataMapper\Tests\_Mocks\Constructor;
1112
use Jerodev\DataMapper\Tests\_Mocks\Nullable;
1213
use Jerodev\DataMapper\Tests\_Mocks\SelfMapped;
@@ -209,5 +210,20 @@ public static function objectValuesDataProvider(): Generator
209210
],
210211
$dto,
211212
];
213+
214+
// Special array cases
215+
$dto = new ArrayProperties();
216+
$dto->ints = [2, 5, 8];
217+
$dto->fieldsWithKeys = [
218+
3 => [
219+
'foo' => 'bar',
220+
'bar' => 8,
221+
],
222+
];
223+
yield [
224+
ArrayProperties::class,
225+
(array)$dto,
226+
$dto,
227+
];
212228
}
213229
}

tests/_Mocks/ArrayProperties.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Jerodev\DataMapper\Tests\_Mocks;
4+
5+
class ArrayProperties
6+
{
7+
/** @var array<int> */
8+
public array $ints;
9+
10+
/** @var array<int, array{foo: string, bar: int}> */
11+
public array $fieldsWithKeys;
12+
}

0 commit comments

Comments
 (0)