Skip to content

Commit 9f44a00

Browse files
author
Яценко Андрей
committed
fix: empty array type
1 parent 285ee8e commit 9f44a00

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/ClassTransformer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public static function transform(string $className, ...$args)
108108
}
109109
continue;
110110
}
111+
112+
$instance->{$item->name} = $value;
113+
continue;
111114
}
112115

113116
if ($propertyType instanceof ReflectionNamedType) {

tests/CustomTransformerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ReflectionException;
1111
use Tests\DTO\CustomTransformUserDTO;
1212
use Tests\DTO\CustomTransformUserDTOArray;
13+
use Tests\DTO\UserNoTypeArrayDTO;
1314

1415
/**
1516
* Class CustomTransformerTest
@@ -37,6 +38,32 @@ public function testCustomTransform(): void
3738
self::assertEquals('Corey', $userDTO->username);
3839
}
3940

41+
/**
42+
* @throws ReflectionException|ClassNotFoundException
43+
*/
44+
public function testArrayNotType(): void
45+
{
46+
$data = [
47+
'id' => 1,
48+
'products' => [
49+
['id' => 1, 'price' => 43.03,],
50+
['id' => 2, 'price' => 10.56,],
51+
],
52+
];
53+
$userDTO = ClassTransformer::transform(UserNoTypeArrayDTO::class, $data);
54+
55+
self::assertInstanceOf(UserNoTypeArrayDTO::class, $userDTO);
56+
57+
self::assertTrue(isset($userDTO->id));
58+
self::assertTrue(isset($userDTO->products));
59+
self::assertEquals($data['id'], $userDTO->id);
60+
61+
foreach ($userDTO->products as $key => $product) {
62+
self::assertEquals($data['products'][$key]['id'], $product['id']);
63+
self::assertEquals($data['products'][$key]['price'], $product['price']);
64+
}
65+
}
66+
4067
/**
4168
* @throws ReflectionException|ClassNotFoundException
4269
*/

tests/DTO/UserNoTypeArrayDTO.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Tests\DTO;
5+
6+
class UserNoTypeArrayDTO
7+
{
8+
public $id;
9+
public array $products;
10+
}

0 commit comments

Comments
 (0)