Skip to content

Commit 1cfef31

Browse files
author
Яценко Андрей
committed
fix: scalar array
1 parent edc32e4 commit 1cfef31

File tree

6 files changed

+54
-3
lines changed

6 files changed

+54
-3
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "yzen.dev/plain-to-class",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Class-transformer to transform your dataset into a structured object",
55
"minimum-stability": "dev",
66
"prefer-stable": true,
@@ -48,6 +48,9 @@
4848
"phpcs": [
4949
"./vendor/bin/phpcs --standard=./phpcs.xml -n --no-cache -s"
5050
],
51+
"phpcbf": [
52+
"./vendor/bin/phpcbf --standard=./phpcs.xml -n --no-cache -s"
53+
],
5154
"coverage": [
5255
"./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --coverage-text --colors=never --coverage-clover coverage.xml"
5356
],

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ClassTransformer.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private static function dataConverting(string $className, ...$args)
155155
$propertyType = $property->getType();
156156
$propertyClassTypeName = self::getPropertyTypes($propertyType);
157157

158-
if (count(array_intersect($propertyClassTypeName, ['int', 'float', 'string', 'bool'])) > 0) {
158+
if (self::propertyIsScalar($propertyClassTypeName)) {
159159
$instance->{$item->name} = $value;
160160
continue;
161161
}
@@ -169,6 +169,10 @@ private static function dataConverting(string $className, ...$args)
169169
$arrayType = self::getClassFromPhpDoc($property->getDocComment());
170170
}
171171
if (!empty($arrayType)) {
172+
if (self::propertyIsScalar($arrayType)) {
173+
$instance->{$item->name} = $value;
174+
continue;
175+
}
172176
foreach ($value as $el) {
173177
/** @phpstan-ignore-next-line */
174178
$instance->{$item->name}[] = self::dataConverting($arrayType, $el);
@@ -190,6 +194,19 @@ private static function dataConverting(string $className, ...$args)
190194
return $instance;
191195
}
192196

197+
/**
198+
* @param array<string>|string $type
199+
*
200+
* @return bool
201+
*/
202+
private static function propertyIsScalar(array|string $type): bool
203+
{
204+
if (is_array($type)) {
205+
return count(array_intersect($type, ['int', 'float', 'string', 'bool'])) > 0;
206+
}
207+
return in_array($type, ['int', 'float', 'string', 'bool']);
208+
}
209+
193210
/**
194211
* @param string|false $phpDoc
195212
*

tests/ClassTransformerFromArrayTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests;
66

7+
use Tests\DTO\ArrayScalarDTO;
78
use Tests\DTO\UserDTO;
89
use Tests\DTO\BasketDTO;
910
use ReflectionException;
@@ -39,6 +40,16 @@ public function testBaseArray(): void
3940
self::assertIsFloat($userDTO->balance);
4041
}
4142

43+
/**
44+
* @throws ReflectionException|ClassNotFoundException
45+
*/
46+
public function testScalarArray(): void
47+
{
48+
$data = $this->getDataWithScalarArray();
49+
$userDTO = ClassTransformer::transform(ArrayScalarDTO::class, $data);
50+
self::assertInstanceOf(ArrayScalarDTO::class, $userDTO);
51+
}
52+
4253
/**
4354
* @throws ReflectionException|ClassNotFoundException
4455
*/

tests/DTO/ArrayScalarDTO.php

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

tests/FakerData.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public function getBaseArrayData(): array
1515
];
1616
}
1717

18+
public function getDataWithScalarArray(): array
19+
{
20+
return [
21+
'id' => 1,
22+
'history' => ['test1', 'test2', 'test3']
23+
];
24+
}
25+
1826
public function getArrayUsers(): array
1927
{
2028
return [

0 commit comments

Comments
 (0)