Настроил чтобы сразу передавать объект.

Минус один опциональный параметр
master
Sipachev Igor 3 years ago
parent c49bac2315
commit ba54621a7f
  1. 2
      Readme.md
  2. 9
      src/Data2DtoConverter.php
  3. 2
      tests/unit/Converter/FillTest.php

@ -75,6 +75,6 @@ $dto = $data2DtoConverter->convert([
'barField' => 32
],
'extraData1' => 'qwer'
], HelloRequest::class);
], new HelloRequest);
```

@ -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;
}

@ -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);

Loading…
Cancel
Save