diff --git a/Readme.md b/Readme.md index d45dba0..94d2032 100644 --- a/Readme.md +++ b/Readme.md @@ -75,6 +75,6 @@ $dto = $data2DtoConverter->convert([ 'barField' => 32 ], 'extraData1' => 'qwer' -], HelloRequest::class); +], new HelloRequest); ``` diff --git a/src/Data2DtoConverter.php b/src/Data2DtoConverter.php index 8d61280..c01ea39 100644 --- a/src/Data2DtoConverter.php +++ b/src/Data2DtoConverter.php @@ -14,10 +14,9 @@ use function Symfony\Component\String\u; class Data2DtoConverter { - public function convert(array $data, string $class, array $tags = [], ?object $instance = null): object + public function convert(array $data, object $object, array $tags = []): object { $tags = empty($tags) ? ['default'] : $tags; - $object = $instance ?? new $class; $reflectionObject = new \ReflectionObject($object); $this->processClassTransformers($reflectionObject, $data, $tags); if (is_object($data)) { @@ -74,9 +73,9 @@ class Data2DtoConverter if (class_exists($preparedPropertyType)) { if ($property->isInitialized($object)) { $propertyValue = $property->getValue($object); - $value = $this->convert($value, $preparedPropertyType, $tags, $propertyValue); + $value = $this->convert($value, new $preparedPropertyType, $tags, $propertyValue); } else { - $value = $this->convert($value, $preparedPropertyType, $tags); + $value = $this->convert($value, new $preparedPropertyType, $tags); } } @@ -192,7 +191,7 @@ class Data2DtoConverter } $tempValue = []; foreach ($value as $itemValue) { - $tempValue[] = $this->convert($itemValue, $attributedPropertyClass, $tags); + $tempValue[] = $this->convert($itemValue, new $attributedPropertyClass, $tags); } $value = $tempValue; } @@ -210,4 +209,4 @@ class Data2DtoConverter } return $attributedPropertyClass; } -} \ No newline at end of file +} diff --git a/tests/unit/Converter/FillTest.php b/tests/unit/Converter/FillTest.php index cc939c1..4194ec4 100644 --- a/tests/unit/Converter/FillTest.php +++ b/tests/unit/Converter/FillTest.php @@ -52,7 +52,7 @@ class FillTest extends \Codeception\Test\Unit 'barField' => 32 ], 'extraData1' => 'qwer' - ], HelloRequest::class); + ], new HelloRequest); $this->assertInstanceOf(HelloRequest::class, $dto); $this->assertEquals('asdf', $dto->surname); $this->assertEquals(3, $dto->age);