Skip to content

Commit 5a0b2c1

Browse files
authored
feat(object): allow nested array to be transformed to object (#256)
Fix #203
2 parents 9b49552 + e510059 commit 5a0b2c1

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/Transformer/ObjectTransformerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private function isObjectType(Type $type): bool
4747
return false;
4848
}
4949

50-
if (Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType() && $type->isCollection()) {
50+
if (Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType() && $type->isCollection() && $type->getCollectionValueTypes()) {
5151
return false;
5252
}
5353

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
AutoMapper\Tests\AutoMapperTest\ArrayNested\TestTarget {
2+
-endpoints: AutoMapper\Tests\AutoMapperTest\ArrayNested\TestSubTarget {
3+
-endpoint: "https://example.com"
4+
-params: [
5+
"param1"
6+
"param2"
7+
]
8+
}
9+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AutoMapper\Tests\AutoMapperTest\ArrayNested;
6+
7+
use AutoMapper\Tests\AutoMapperBuilder;
8+
9+
class ClassContainer
10+
{
11+
public ?array $endpoints = null;
12+
}
13+
14+
class TestSubTarget
15+
{
16+
public function __construct(
17+
private ?string $endpoint = null,
18+
private ?array $params = null,
19+
) {
20+
}
21+
}
22+
23+
class TestTarget
24+
{
25+
public function __construct(
26+
private ?TestSubTarget $endpoints = null,
27+
) {
28+
}
29+
}
30+
31+
return (function () {
32+
$autoMapper = AutoMapperBuilder::buildAutoMapper();
33+
34+
$classContainer = new ClassContainer();
35+
$classContainer->endpoints = [
36+
'endpoint' => 'https://example.com',
37+
'params' => ['param1', 'param2'],
38+
];
39+
40+
yield 'array' => $autoMapper->map($classContainer, TestTarget::class);
41+
})();

0 commit comments

Comments
 (0)