Проинтегрировал теги в аттрибуты

Перенес проверку на null в конец
Настроил сетинг на уже стуществующие объекты
master
Rinsvent 3 years ago
parent 10f1937434
commit 2e689c826d
  1. 4
      src/Attribute/DTOMeta.php
  2. 4
      src/Attribute/PropertyPath.php
  3. 31
      src/Data2DtoConverter.php
  4. 6
      src/Transformer/Meta.php
  5. 7
      src/Transformer/Trim.php

@ -6,6 +6,8 @@ namespace Rinsvent\Data2DTO\Attribute;
class DTOMeta
{
public function __construct(
public string $class
public string $class,
/** @var string[] $tags */
public array $tags = ['default']
) {}
}

@ -6,6 +6,8 @@ namespace Rinsvent\Data2DTO\Attribute;
class PropertyPath
{
public function __construct(
public string $path
public string $path,
/** @var string[] $tags */
public array $tags = ['default']
) {}
}

@ -30,21 +30,18 @@ class Data2DtoConverter
$reflectionPropertyType = $property->getType();
$propertyType = $reflectionPropertyType->getName();
if ($dataPath = $this->grabDataPath($property, $data)) {
if ($dataPath = $this->grabDataPath($property, $data, $tags)) {
$value = $data[$dataPath];
// Трансформируем данные
$this->processTransformers($property, $value, $tags);
if ($this->checkNullRule($value, $reflectionPropertyType)) {
continue;
}
// В данных лежит объект, то дальше его не заполняем. Только присваиваем. Например, entity, document
if (is_object($value)) {
$property->setValue($object, $value);
continue;
}
if (!$this->transformArray($value, $property)) {
if (!$this->transformArray($value, $property, $tags)) {
continue;
}
@ -66,7 +63,16 @@ class Data2DtoConverter
// Если это class, то рекурсивно заполняем дальше
if (class_exists($preparedPropertyType)) {
$value = $this->convert($value, $preparedPropertyType);
if ($property->isInitialized($object)) {
$propertyValue = $property->getValue($object);
$value = $this->convert($value, $preparedPropertyType, $tags, $propertyValue);
} else {
$value = $this->convert($value, $preparedPropertyType, $tags);
}
}
if ($this->checkNullRule($value, $reflectionPropertyType)) {
continue;
}
// присваиваем получившееся значение
@ -77,13 +83,18 @@ class Data2DtoConverter
return $object;
}
protected function grabDataPath(\ReflectionProperty $property, array $data): ?string
protected function grabDataPath(\ReflectionProperty $property, array $data, array $tags): ?string
{
$propertyName = $property->getName();
$propertyExtractor = new PropertyExtractor($property->class, $propertyName);
/** @var PropertyPath $propertyPath */
if ($propertyPath = $propertyExtractor->fetch(PropertyPath::class)) {
return $propertyPath->path;
$filteredTags = array_diff($tags, $propertyPath->tags);
if (count($filteredTags) !== count($tags)) {
if (key_exists($propertyPath->path, $data)) {
return $propertyPath->path;
}
}
}
if (key_exists($propertyName, $data)) {
@ -156,7 +167,7 @@ class Data2DtoConverter
return $value === null && !$reflectionPropertyType->allowsNull();
}
private function transformArray(&$value, \ReflectionProperty $property): bool
private function transformArray(&$value, \ReflectionProperty $property, array $tags): bool
{
$attributedPropertyClass = $this->grabPropertyDTOClass($property);
@ -172,7 +183,7 @@ class Data2DtoConverter
}
$tempValue = [];
foreach ($value as $itemValue) {
$tempValue[] = $this->convert($itemValue, $attributedPropertyClass);
$tempValue[] = $this->convert($itemValue, $attributedPropertyClass, $tags);
}
$value = $tempValue;
}

@ -8,6 +8,8 @@ abstract class Meta
public const TYPE = 'simple';
public ?string $returnType = null;
public ?bool $allowsNull = null;
/** @var string[] $tags */
public array $tags = ['default'];
public function __construct(
public array $tags = ['default']
) {}
}

@ -5,5 +5,10 @@ namespace Rinsvent\Data2DTO\Transformer;
#[\Attribute]
class Trim extends Meta
{
public string $characters = " \t\n\r\0\x0B";
public function __construct(
public array $tags = ['default'],
public string $characters = " \t\n\r\0\x0B"
) {
parent::__construct(...func_get_args());
}
}
Loading…
Cancel
Save