Привел работоспособность кожидаемому результату
This commit is contained in:
parent
ca1b291fd0
commit
fc09440e24
@ -9,6 +9,7 @@
|
|||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"symfony/string": "^5.3",
|
"symfony/string": "^5.3",
|
||||||
"rinsvent/attribute-extractor": "^0.0",
|
"rinsvent/attribute-extractor": "^0.0",
|
||||||
|
"rinsvent/transformer": "^0.0",
|
||||||
"symfony/property-access": "^6.0"
|
"symfony/property-access": "^6.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Attribute;
|
|
||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
|
|
||||||
class DataPath
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
public string $path,
|
|
||||||
/** @var string[] $tags */
|
|
||||||
public array $tags = ['default']
|
|
||||||
) {}
|
|
||||||
}
|
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Attribute;
|
namespace Rinsvent\DTO2Data\Attribute;
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Attribute;
|
namespace Rinsvent\DTO2Data\Attribute;
|
||||||
|
|
||||||
@ -9,5 +10,6 @@ class PropertyPath
|
|||||||
public string $path,
|
public string $path,
|
||||||
/** @var string[] $tags */
|
/** @var string[] $tags */
|
||||||
public array $tags = ['default']
|
public array $tags = ['default']
|
||||||
) {}
|
) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Attribute;
|
namespace Rinsvent\DTO2Data\Attribute;
|
||||||
|
|
||||||
@ -9,8 +10,6 @@ class Schema
|
|||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public ?array $map = null,
|
public ?array $map = null,
|
||||||
/** @var string[] $tags */
|
|
||||||
public array $tags = ['default']
|
|
||||||
) {
|
) {
|
||||||
$this->map = $map ?? $this->baseMap;
|
$this->map = $map ?? $this->baseMap;
|
||||||
}
|
}
|
||||||
|
@ -1,321 +1,168 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data;
|
namespace Rinsvent\DTO2Data;
|
||||||
|
|
||||||
use ReflectionMethod;
|
|
||||||
use ReflectionProperty;
|
use ReflectionProperty;
|
||||||
use Rinsvent\AttributeExtractor\ClassExtractor;
|
|
||||||
use Rinsvent\AttributeExtractor\MethodExtractor;
|
use Rinsvent\AttributeExtractor\MethodExtractor;
|
||||||
use Rinsvent\AttributeExtractor\PropertyExtractor;
|
use Rinsvent\AttributeExtractor\PropertyExtractor;
|
||||||
use Rinsvent\DTO2Data\Attribute\DataPath;
|
|
||||||
use Rinsvent\DTO2Data\Attribute\HandleTags;
|
|
||||||
use Rinsvent\DTO2Data\Attribute\PropertyPath;
|
|
||||||
use Rinsvent\DTO2Data\Attribute\Schema;
|
use Rinsvent\DTO2Data\Attribute\Schema;
|
||||||
use Rinsvent\DTO2Data\Resolver\TransformerResolverStorage;
|
use Rinsvent\Transformer\Transformer;
|
||||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
use Rinsvent\Transformer\Transformer\Meta;
|
||||||
use Rinsvent\DTO2Data\Transformer\TransformerInterface;
|
|
||||||
use Symfony\Component\PropertyAccess\PropertyAccess;
|
use Symfony\Component\PropertyAccess\PropertyAccess;
|
||||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
||||||
|
|
||||||
class Dto2DataConverter
|
class Dto2DataConverter
|
||||||
{
|
{
|
||||||
private PropertyAccessorInterface $propertyAccessor;
|
private PropertyAccessorInterface $propertyAccessor;
|
||||||
|
private Transformer $transformer;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
|
||||||
->disableExceptionOnInvalidPropertyPath()
|
->disableExceptionOnInvalidPropertyPath()
|
||||||
|
->enableMagicMethods()
|
||||||
->getPropertyAccessor();
|
->getPropertyAccessor();
|
||||||
|
$this->transformer = new Transformer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTags(object $object, array $tags = []): array
|
public function convert($data, string $schemaClass): array
|
||||||
{
|
{
|
||||||
return $this->processTags($object, $tags);
|
$schema = new $schemaClass();
|
||||||
}
|
if (!$schema instanceof Schema) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
public function convert($data, array $tags = []): array
|
'Schema should be instance of Rinsvent\DTO2Data\Attribute\Schema'
|
||||||
{
|
);
|
||||||
if (!is_object($data) && !is_iterable($data)) {
|
|
||||||
throw new \InvalidArgumentException();
|
|
||||||
}
|
}
|
||||||
return is_iterable($data)
|
return $this->convertByMap($data, $schema->map);
|
||||||
? $this->convertArray($data, $tags)
|
|
||||||
: $this->convertObject($data, $tags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function convertArray(iterable $items, array $tags = []): array
|
private function convertByMap($data, array $map): array
|
||||||
{
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
foreach ($items as $item) {
|
if (is_iterable($data)) {
|
||||||
if (!is_object($item)) {
|
foreach ($data as $item) {
|
||||||
throw new \InvalidArgumentException();
|
$result[] = $this->processItem($item, $map);
|
||||||
}
|
}
|
||||||
$result[] = $this->convertObject($item, $tags);
|
} else {
|
||||||
|
$result = $this->processItem($data, $map);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function convertObject(object $object, array $tags = []): array
|
private function processItem($data, array $map): array
|
||||||
{
|
{
|
||||||
$tags = empty($tags) ? ['default'] : $tags;
|
$result = [];
|
||||||
$schema = $this->grabSchema($object, $tags);
|
foreach ($map as $key => $item) {
|
||||||
return $this->convertObjectByMap($object, $schema->map, $tags);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function convertObjectByMap(object $object, array $map, array $tags = []): array
|
|
||||||
{
|
|
||||||
$data = [];
|
|
||||||
$reflectionObject = new \ReflectionObject($object);
|
|
||||||
foreach ($map as $key => $propertyInfo) {
|
|
||||||
try {
|
try {
|
||||||
$sourceName = is_array($propertyInfo) ? $key : $propertyInfo;
|
switch (true) {
|
||||||
$value = $this->grabValue($object, $sourceName, $tags);
|
// key -> propertyPath (===).
|
||||||
|
case is_int($key) && is_string($item):
|
||||||
// Если нет карты, то не сериализуем.
|
$result[$item] = $this->grabValue($data, $item);
|
||||||
if (is_iterable($value)) {
|
$result[$item] = $this->transform($data, $item, $result[$item]);
|
||||||
$childMap = is_array($propertyInfo) ? $propertyInfo : null;
|
break;
|
||||||
$value = $this->convertArrayByMap($value, $childMap, $tags);
|
// key -> propertyPath (!==)
|
||||||
} elseif (is_object($value) && is_array($propertyInfo)) {
|
case is_string($key) && is_string($item):
|
||||||
$value = $this->convertObjectByMap($value, $propertyInfo, $tags);
|
$result[$key] = $this->grabValue($data, $item);
|
||||||
|
$result[$key] = $this->transform($data, $item, $result[$key]);
|
||||||
|
break;
|
||||||
|
// key -> recursive data processing
|
||||||
|
case is_string($key) && is_array($item):
|
||||||
|
$result[$key] = $this->convertByMap($this->grabValue($data, $key), $item);
|
||||||
|
break;
|
||||||
|
// key -> virtual field
|
||||||
|
case is_string($key) && is_callable($item):
|
||||||
|
$result[$key] = call_user_func_array($item, [$data]);
|
||||||
|
break;
|
||||||
|
// key -> data processing with transformer
|
||||||
|
case $item instanceof Meta:
|
||||||
|
case (new $item) instanceof Meta:
|
||||||
|
$meta = is_string($item) ? new $item : $item;
|
||||||
|
$result[$key] = $this->transformer->transform($data, $meta);
|
||||||
|
break;
|
||||||
|
// key -> recursive data processing with other schema
|
||||||
|
case $item instanceof Schema:
|
||||||
|
case (new $item) instanceof Schema:
|
||||||
|
$schemaClass = is_object($item) ? $item::class : $item;
|
||||||
|
$result[$key] = $this->convert($data[$key] ?? null, $schemaClass);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$result[$key] = null;
|
||||||
}
|
}
|
||||||
|
} catch (\Throwable) {
|
||||||
$this->processIterationTransformers($object, $sourceName, $value, $tags);
|
$result[$key] = null;
|
||||||
|
|
||||||
$dataPath = $this->grabIterationDataPath($object, $sourceName, $tags);
|
|
||||||
$data[$dataPath] = $value;
|
|
||||||
} catch (\Throwable $e) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->processClassTransformers($reflectionObject, $data, $tags);
|
return $result;
|
||||||
return $data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function convertArrayByMap($data, ?array $map, array $tags = []): ?array
|
private function transform(object|array|null $data, string $path, mixed $value): mixed
|
||||||
{
|
{
|
||||||
$tempValue = [];
|
$metas = $this->grabTransformMetas($data, $path);
|
||||||
foreach ($data as $key => $item) {
|
foreach ($metas as $meta) {
|
||||||
if (is_scalar($item)) {
|
$value = $this->transformer->transform($value, $meta);
|
||||||
$tempValue[$key] = $item;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (is_iterable($item) && $map) {
|
|
||||||
$tempValue[$key] = $this->convertArrayByMap($item, $map, $tags);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (is_object($item) && $map) {
|
|
||||||
$tempValue[$key] = $this->convertObjectByMap($item, $map, $tags);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return $tempValue;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function grabValue(object $object, string $sourceName, array $tags)
|
private function grabValue(object|array|null $value, string $path): mixed
|
||||||
{
|
{
|
||||||
if (!method_exists($object, $sourceName) && !property_exists($object, $sourceName)) {
|
if (null === $value) {
|
||||||
return $this->getValueByPath($object, $sourceName);
|
|
||||||
}
|
|
||||||
$propertyExtractor = new PropertyExtractor($object::class, $sourceName);
|
|
||||||
/** @var PropertyPath $propertyPath */
|
|
||||||
while ($propertyPath = $propertyExtractor->fetch(PropertyPath::class)) {
|
|
||||||
$filteredTags = array_diff($tags, $propertyPath->tags);
|
|
||||||
if (count($filteredTags) === count($tags)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
return $this->getValueByPath($object, $propertyPath->path);
|
|
||||||
}
|
|
||||||
return $this->getValueByPath($object, $sourceName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function processIterationTransformers(object $object, string $sourceName, &$value, array $tags): void
|
|
||||||
{
|
|
||||||
if (property_exists($object, $sourceName)) {
|
|
||||||
$reflectionSource = new ReflectionProperty($object, $sourceName);
|
|
||||||
$this->processTransformers($reflectionSource, $value, $tags);
|
|
||||||
}
|
|
||||||
$getter = $this->grabGetterName($object, $sourceName);
|
|
||||||
if ($getter) {
|
|
||||||
$reflectionSource = new ReflectionMethod($object, $getter);
|
|
||||||
$this->processMethodTransformers($reflectionSource, $value, $tags);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function grabIterationDataPath(object $object, string $sourceName, array $tags): string
|
|
||||||
{
|
|
||||||
$getter = $this->grabGetterName($object, $sourceName);
|
|
||||||
if ($getter) {
|
|
||||||
$reflectionSource = new ReflectionMethod($object, $getter);
|
|
||||||
$dataPath = $this->grabMethodDataPath($reflectionSource, $tags);
|
|
||||||
if ($dataPath) {
|
|
||||||
return $dataPath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (property_exists($object, $sourceName)) {
|
|
||||||
$reflectionSource = new ReflectionProperty($object, $sourceName);
|
|
||||||
$dataPath = $this->grabDataPath($reflectionSource, $tags);
|
|
||||||
}
|
|
||||||
return $dataPath ?? $sourceName;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Получаем теги для обработки
|
|
||||||
*/
|
|
||||||
protected function processTags(object $object, array $tags): array
|
|
||||||
{
|
|
||||||
$classExtractor = new ClassExtractor($object::class);
|
|
||||||
/** @var HandleTags $tagsMeta */
|
|
||||||
if ($tagsMeta = $classExtractor->fetch(HandleTags::class)) {
|
|
||||||
if (method_exists($object, $tagsMeta->method)) {
|
|
||||||
$reflectionMethod = new ReflectionMethod($object, $tagsMeta->method);
|
|
||||||
if (!$reflectionMethod->isPublic()) {
|
|
||||||
$reflectionMethod->setAccessible(true);
|
|
||||||
}
|
|
||||||
$methodTags = $reflectionMethod->invoke($object, ...[$tags]);
|
|
||||||
if (!$reflectionMethod->isPublic()) {
|
|
||||||
$reflectionMethod->setAccessible(false);
|
|
||||||
}
|
|
||||||
return $methodTags;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $tags;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Трнансформируем на уровне класса
|
|
||||||
*/
|
|
||||||
protected function processClassTransformers(\ReflectionObject $object, &$data, array $tags): void
|
|
||||||
{
|
|
||||||
$className = $object->getName();
|
|
||||||
$classExtractor = new ClassExtractor($className);
|
|
||||||
/** @var Meta $transformMeta */
|
|
||||||
while ($transformMeta = $classExtractor->fetch(Meta::class)) {
|
|
||||||
$transformMeta->returnType = $className;
|
|
||||||
$filteredTags = array_diff($tags, $transformMeta->tags);
|
|
||||||
if (count($filteredTags) === count($tags)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$transformer = $this->grabTransformer($transformMeta);
|
|
||||||
$transformer->transform($data, $transformMeta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Трнансформируем на уровне свойст объекта
|
|
||||||
*/
|
|
||||||
protected function processTransformers(\ReflectionProperty $property, &$data, array $tags): void
|
|
||||||
{
|
|
||||||
$propertyName = $property->getName();
|
|
||||||
$propertyExtractor = new PropertyExtractor($property->class, $propertyName);
|
|
||||||
/** @var Meta $transformMeta */
|
|
||||||
while ($transformMeta = $propertyExtractor->fetch(Meta::class)) {
|
|
||||||
$filteredTags = array_diff($tags, $transformMeta->tags);
|
|
||||||
if (count($filteredTags) === count($tags)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
/** @var \ReflectionNamedType $reflectionPropertyType */
|
|
||||||
$reflectionPropertyType = $property->getType();
|
|
||||||
$propertyType = $reflectionPropertyType->getName();
|
|
||||||
$transformMeta->returnType = $propertyType;
|
|
||||||
$transformMeta->allowsNull = $reflectionPropertyType->allowsNull();
|
|
||||||
$transformer = $this->grabTransformer($transformMeta);
|
|
||||||
$transformer->transform($data, $transformMeta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function processMethodTransformers(ReflectionMethod $method, &$data, array $tags): void
|
|
||||||
{
|
|
||||||
$methodName = $method->getName();
|
|
||||||
$methodExtractor = new MethodExtractor($method->class, $methodName);
|
|
||||||
/** @var Meta $transformMeta */
|
|
||||||
while ($transformMeta = $methodExtractor->fetch(Meta::class)) {
|
|
||||||
$filteredTags = array_diff($tags, $transformMeta->tags);
|
|
||||||
if (count($filteredTags) === count($tags)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
/** @var \ReflectionNamedType $reflectionMethodType */
|
|
||||||
$reflectionMethodType = $method->getReturnType();
|
|
||||||
$methodType = $reflectionMethodType->getName();
|
|
||||||
$transformMeta->returnType = $methodType;
|
|
||||||
$transformMeta->allowsNull = $reflectionMethodType->allowsNull();
|
|
||||||
$transformer = $this->grabTransformer($transformMeta);
|
|
||||||
$transformer->transform($data, $transformMeta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function grabTransformer(Meta $meta): TransformerInterface
|
|
||||||
{
|
|
||||||
$storage = TransformerResolverStorage::getInstance();
|
|
||||||
$resolver = $storage->get($meta::TYPE);
|
|
||||||
return $resolver->resolve($meta);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getValueByPath($data, string $path)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
return $this->propertyAccessor->getValue($data, $path);
|
|
||||||
} catch (\Throwable $e) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
$path = is_array($value) && 0 === mb_substr_count($path, '.') &&
|
||||||
|
false === mb_strpos($path, '[')
|
||||||
|
? "[{$path}]"
|
||||||
|
: $path;
|
||||||
|
return $this->propertyAccessor->getValue($value, $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function grabSchema(object $object, array $tags): ?Schema
|
/**
|
||||||
|
* @return Meta[]
|
||||||
|
*/
|
||||||
|
private function grabTransformMetas(mixed $data, string $propertyPath): array
|
||||||
{
|
{
|
||||||
$classExtractor = new ClassExtractor($object::class);
|
$result = [];
|
||||||
/** @var Schema $schema */
|
$propertyName = $propertyPath;
|
||||||
while ($schema = $classExtractor->fetch(Schema::class)) {
|
$propertyPathParts = explode('.', $propertyPath);
|
||||||
$filteredTags = array_diff($tags, $schema->tags);
|
if (count($propertyPathParts) > 1) {
|
||||||
if (count($filteredTags) === count($tags)) {
|
$propertyName = array_shift($propertyPathParts);
|
||||||
continue;
|
$pathToObject = implode('.', $propertyPathParts);
|
||||||
}
|
$object = $this->grabValue($data, $pathToObject);
|
||||||
return $schema;
|
if (!is_object($object)) {
|
||||||
}
|
return [];
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function grabDataPath(\ReflectionProperty $property, array $tags): ?string
|
|
||||||
{
|
|
||||||
$propertyName = $property->getName();
|
|
||||||
$propertyExtractor = new PropertyExtractor($property->class, $propertyName);
|
|
||||||
/** @var DataPath $schema */
|
|
||||||
while ($dataPath = $propertyExtractor->fetch(DataPath::class)) {
|
|
||||||
$filteredTags = array_diff($tags, $dataPath->tags);
|
|
||||||
if (count($filteredTags) === count($tags)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
return $dataPath->path;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function grabMethodDataPath(ReflectionMethod $method, array $tags): ?string
|
|
||||||
{
|
|
||||||
$methodName = $method->getName();
|
|
||||||
$methodExtractor = new MethodExtractor($method->class, $methodName);
|
|
||||||
/** @var DataPath $schema */
|
|
||||||
while ($dataPath = $methodExtractor->fetch(DataPath::class)) {
|
|
||||||
$filteredTags = array_diff($tags, $dataPath->tags);
|
|
||||||
if (count($filteredTags) === count($tags)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
return $dataPath->path;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function grabGetterName(object $object, string $sourceName): ?string
|
|
||||||
{
|
|
||||||
$prefixes = ['get', 'has', 'is'];
|
|
||||||
foreach ($prefixes as $prefix) {
|
|
||||||
if (method_exists($object, $prefix . ucfirst($sourceName))) {
|
|
||||||
return $prefix . ucfirst($sourceName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
if (!is_object($data)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (property_exists($data, $propertyName)) {
|
||||||
|
$reflectionProperty = new ReflectionProperty($data, $propertyName);
|
||||||
|
$methodExtractor = new PropertyExtractor($reflectionProperty->class, $propertyName);
|
||||||
|
while ($meta = $methodExtractor->fetch(Meta::class)) {
|
||||||
|
$result[] = $meta;
|
||||||
|
}
|
||||||
|
if ($result) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (['get', 'has', 'is'] as $prefix) {
|
||||||
|
$methodName = $prefix . ucfirst($propertyName);
|
||||||
|
if (method_exists($data, $methodName)) {
|
||||||
|
$reflectionMethod = new \ReflectionMethod($data, $methodName);
|
||||||
|
$methodExtractor = new MethodExtractor($reflectionMethod->class, $methodName);
|
||||||
|
while ($meta = $methodExtractor->fetch(Meta::class)) {
|
||||||
|
$result[] = $meta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($result) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Resolver;
|
|
||||||
|
|
||||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
|
||||||
use Rinsvent\DTO2Data\Transformer\TransformerInterface;
|
|
||||||
|
|
||||||
class SimpleResolver implements TransformerResolverInterface
|
|
||||||
{
|
|
||||||
public function resolve(Meta $meta): TransformerInterface
|
|
||||||
{
|
|
||||||
$metaClass = $meta::class;
|
|
||||||
$transformerClass = $metaClass . 'Transformer';
|
|
||||||
return new $transformerClass;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Resolver;
|
|
||||||
|
|
||||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
|
||||||
use Rinsvent\DTO2Data\Transformer\TransformerInterface;
|
|
||||||
|
|
||||||
interface TransformerResolverInterface
|
|
||||||
{
|
|
||||||
public function resolve(Meta $meta): TransformerInterface;
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Resolver;
|
|
||||||
|
|
||||||
class TransformerResolverStorage
|
|
||||||
{
|
|
||||||
private array $items = [];
|
|
||||||
|
|
||||||
public static function getInstance(): self
|
|
||||||
{
|
|
||||||
static $instance = null;
|
|
||||||
|
|
||||||
if ($instance) {
|
|
||||||
return $instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
$instance = new self();
|
|
||||||
$instance->add('simple', new SimpleResolver());
|
|
||||||
|
|
||||||
return $instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function add(string $code, TransformerResolverInterface $transformerResolver): void
|
|
||||||
{
|
|
||||||
$this->items[$code] = $transformerResolver;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get(string $code): TransformerResolverInterface
|
|
||||||
{
|
|
||||||
return $this->items[$code];
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Transformer;
|
|
||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
|
|
||||||
class DateTimeFormat extends Meta
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
public array $tags = ['default'],
|
|
||||||
public string $format = \DateTimeInterface::ATOM
|
|
||||||
) {
|
|
||||||
parent::__construct(...func_get_args());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Transformer;
|
|
||||||
|
|
||||||
class DateTimeFormatTransformer implements TransformerInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param \DateTimeImmutable|\DateTime|null $data
|
|
||||||
* @param DateTimeFormat $meta
|
|
||||||
*/
|
|
||||||
public function transform(&$data, Meta $meta): void
|
|
||||||
{
|
|
||||||
if ($data === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$data = $data->format($meta->format);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Transformer;
|
|
||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
|
|
||||||
abstract class Meta
|
|
||||||
{
|
|
||||||
public const TYPE = 'simple';
|
|
||||||
public ?string $returnType = null;
|
|
||||||
public ?bool $allowsNull = null;
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
public array $tags = ['default']
|
|
||||||
) {}
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Transformer;
|
|
||||||
|
|
||||||
interface TransformerInterface
|
|
||||||
{
|
|
||||||
public function transform(&$data, Meta $meta): void;
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Transformer;
|
|
||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
|
|
||||||
class Trim extends Meta
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
public array $tags = ['default'],
|
|
||||||
public string $characters = " \t\n\r\0\x0B"
|
|
||||||
) {
|
|
||||||
parent::__construct(...func_get_args());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Transformer;
|
|
||||||
|
|
||||||
class TrimTransformer implements TransformerInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param string|null $data
|
|
||||||
* @param Trim $meta
|
|
||||||
*/
|
|
||||||
public function transform(&$data, Meta $meta): void
|
|
||||||
{
|
|
||||||
if ($data === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$data = trim($data, $meta->characters);
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,6 +9,7 @@ use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\BuyRequest;
|
|||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Collection;
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Collection;
|
||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\CollectionItem;
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\CollectionItem;
|
||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\HelloRequest;
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\HelloRequest;
|
||||||
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\HelloSchema;
|
||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\UUID;
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\UUID;
|
||||||
|
|
||||||
class FillTest extends \Codeception\Test\Unit
|
class FillTest extends \Codeception\Test\Unit
|
||||||
@ -107,7 +108,7 @@ class FillTest extends \Codeception\Test\Unit
|
|||||||
$helloRequest->setPcreatedAt(new \DateTimeImmutable('2020-05-21 13:36:22'));
|
$helloRequest->setPcreatedAt(new \DateTimeImmutable('2020-05-21 13:36:22'));
|
||||||
|
|
||||||
|
|
||||||
$dto = $dto2DataConverter->convert($helloRequest);
|
$dto = $dto2DataConverter->convert($helloRequest, HelloSchema::class);
|
||||||
// codecept_debug(json_encode($dto));
|
// codecept_debug(json_encode($dto));
|
||||||
|
|
||||||
$this->assertEquals([
|
$this->assertEquals([
|
||||||
@ -134,7 +135,14 @@ class FillTest extends \Codeception\Test\Unit
|
|||||||
"name" => "Sapkovsky"
|
"name" => "Sapkovsky"
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
"authors3" => [],
|
"authors3" => [
|
||||||
|
[
|
||||||
|
"name" => "Tolkien"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"name" => "Sapkovsky"
|
||||||
|
]
|
||||||
|
],
|
||||||
"buy" => [
|
"buy" => [
|
||||||
"phrase" => "Buy buy!!!",
|
"phrase" => "Buy buy!!!",
|
||||||
"length" => 10,
|
"length" => 10,
|
||||||
@ -180,7 +188,14 @@ class FillTest extends \Codeception\Test\Unit
|
|||||||
"name" => "Sapkovsky"
|
"name" => "Sapkovsky"
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
"pauthors3" => [],
|
"pauthors3" => [
|
||||||
|
[
|
||||||
|
"name" => "Tolkien"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"name" => "Sapkovsky"
|
||||||
|
]
|
||||||
|
],
|
||||||
"pbuy" => [
|
"pbuy" => [
|
||||||
"phrase" => "Buy buy!!!",
|
"phrase" => "Buy buy!!!",
|
||||||
"length" => 10,
|
"length" => 10,
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Tests\Converter;
|
|
||||||
|
|
||||||
use Rinsvent\DTO2Data\Dto2DataConverter;
|
|
||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\HelloTagsRequest;
|
|
||||||
|
|
||||||
class TagsTest extends \Codeception\Test\Unit
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var \UnitTester
|
|
||||||
*/
|
|
||||||
protected $tester;
|
|
||||||
|
|
||||||
protected function _before()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function _after()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// tests
|
|
||||||
public function testSuccessFillRequestData()
|
|
||||||
{
|
|
||||||
$dto2DataConverter = new Dto2DataConverter();
|
|
||||||
|
|
||||||
$helloTagsRequest = new HelloTagsRequest();
|
|
||||||
$helloTagsRequest->surname = ' Surname1234';
|
|
||||||
$helloTagsRequest->age = 3;
|
|
||||||
$helloTagsRequest->emails = [
|
|
||||||
'sfdgsa',
|
|
||||||
'af234f',
|
|
||||||
'asdf33333'
|
|
||||||
];
|
|
||||||
$tags = $dto2DataConverter->getTags($helloTagsRequest);
|
|
||||||
$this->assertEquals(['surname-group'], $tags);
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,17 +2,16 @@
|
|||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||||
|
|
||||||
use Rinsvent\DTO2Data\Attribute\DataPath;
|
|
||||||
use Rinsvent\DTO2Data\Attribute\PropertyPath;
|
use Rinsvent\DTO2Data\Attribute\PropertyPath;
|
||||||
use Rinsvent\DTO2Data\Transformer\DateTimeFormat;
|
use Rinsvent\Transformer\Transformer\DateTimeFormat;
|
||||||
use Rinsvent\DTO2Data\Transformer\Trim;
|
use Rinsvent\Transformer\Transformer\ToString;
|
||||||
|
use Rinsvent\Transformer\Transformer\Trim;
|
||||||
|
|
||||||
#[HelloSchema]
|
#[HelloSchema]
|
||||||
class HelloRequest
|
class HelloRequest
|
||||||
{
|
{
|
||||||
#[Trim]
|
#[Trim]
|
||||||
public string $surname;
|
public string $surname;
|
||||||
#[DataPath('fake_age')]
|
|
||||||
public int $age;
|
public int $age;
|
||||||
public array $emails;
|
public array $emails;
|
||||||
public array $authors;
|
public array $authors;
|
||||||
@ -20,7 +19,7 @@ class HelloRequest
|
|||||||
public array $authors3;
|
public array $authors3;
|
||||||
public BuyRequest $buy;
|
public BuyRequest $buy;
|
||||||
public BarInterface $bar;
|
public BarInterface $bar;
|
||||||
#[PropertyPath(path: 'uuid.id')]
|
#[ToString()]
|
||||||
public UUID $uuid;
|
public UUID $uuid;
|
||||||
public Collection $collection;
|
public Collection $collection;
|
||||||
#[DateTimeFormat]
|
#[DateTimeFormat]
|
||||||
@ -28,7 +27,6 @@ class HelloRequest
|
|||||||
|
|
||||||
#[Trim]
|
#[Trim]
|
||||||
private string $psurname;
|
private string $psurname;
|
||||||
#[DataPath('pfake_age')]
|
|
||||||
private int $page;
|
private int $page;
|
||||||
private array $pemails;
|
private array $pemails;
|
||||||
private array $pauthors;
|
private array $pauthors;
|
||||||
@ -36,7 +34,7 @@ class HelloRequest
|
|||||||
private array $pauthors3;
|
private array $pauthors3;
|
||||||
private BuyRequest $pbuy;
|
private BuyRequest $pbuy;
|
||||||
private BarInterface $pbar;
|
private BarInterface $pbar;
|
||||||
#[PropertyPath(path: 'puuid.id')]
|
#[ToString()]
|
||||||
private UUID $puuid;
|
private UUID $puuid;
|
||||||
private Collection $pcollection;
|
private Collection $pcollection;
|
||||||
#[DateTimeFormat]
|
#[DateTimeFormat]
|
||||||
@ -164,7 +162,6 @@ class HelloRequest
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[Trim]
|
#[Trim]
|
||||||
#[DataPath('fake_Pdevdo')]
|
|
||||||
public function getPdevdo(): string
|
public function getPdevdo(): string
|
||||||
{
|
{
|
||||||
return ' getPdevdo';
|
return ' getPdevdo';
|
||||||
|
@ -9,7 +9,7 @@ class HelloSchema extends Schema
|
|||||||
{
|
{
|
||||||
public ?array $baseMap = [
|
public ?array $baseMap = [
|
||||||
'surname',
|
'surname',
|
||||||
'age',
|
'fake_age' => 'age',
|
||||||
'emails',
|
'emails',
|
||||||
'authors' => [
|
'authors' => [
|
||||||
'name',
|
'name',
|
||||||
@ -33,7 +33,7 @@ class HelloSchema extends Schema
|
|||||||
'createdAt',
|
'createdAt',
|
||||||
|
|
||||||
'psurname',
|
'psurname',
|
||||||
'page',
|
'pfake_age' => 'page',
|
||||||
'pemails',
|
'pemails',
|
||||||
'pauthors' => [
|
'pauthors' => [
|
||||||
'name',
|
'name',
|
||||||
@ -56,6 +56,6 @@ class HelloSchema extends Schema
|
|||||||
],
|
],
|
||||||
'pcreatedAt',
|
'pcreatedAt',
|
||||||
|
|
||||||
'pdevdo'
|
'fake_Pdevdo' => 'pdevdo'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||||
|
|
||||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
use Rinsvent\Transformer\Transformer\Meta;
|
||||||
|
|
||||||
#[\Attribute]
|
#[\Attribute]
|
||||||
class ClassData extends Meta
|
class ClassData extends Meta
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||||
|
|
||||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
use Rinsvent\Transformer\Transformer\Meta;
|
||||||
use Rinsvent\DTO2Data\Transformer\TransformerInterface;
|
use Rinsvent\Transformer\Transformer\TransformerInterface;
|
||||||
|
|
||||||
class ClassDataTransformer implements TransformerInterface
|
class ClassDataTransformer implements TransformerInterface
|
||||||
{
|
{
|
||||||
@ -11,14 +11,15 @@ class ClassDataTransformer implements TransformerInterface
|
|||||||
* @param array|null $data
|
* @param array|null $data
|
||||||
* @param ClassData $meta
|
* @param ClassData $meta
|
||||||
*/
|
*/
|
||||||
public function transform(&$data, Meta $meta): void
|
public function transform(mixed $data, Meta $meta): mixed
|
||||||
{
|
{
|
||||||
if ($data === null) {
|
if ($data === null) {
|
||||||
return;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($data['surname'])) {
|
if (isset($data['surname'])) {
|
||||||
$data['surname'] = '123454321';
|
$data['surname'] = '123454321';
|
||||||
}
|
}
|
||||||
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||||
|
|
||||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
use Rinsvent\Transformer\Transformer\Meta;
|
||||||
|
|
||||||
#[\Attribute]
|
#[\Attribute]
|
||||||
class ClassObject extends Meta
|
class ClassObject extends Meta
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||||
|
|
||||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest2;
|
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest2;
|
||||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
use Rinsvent\Transformer\Transformer\Meta;
|
||||||
use Rinsvent\DTO2Data\Transformer\TransformerInterface;
|
use Rinsvent\Transformer\Transformer\TransformerInterface;
|
||||||
|
|
||||||
class ClassObjectTransformer implements TransformerInterface
|
class ClassObjectTransformer implements TransformerInterface
|
||||||
{
|
{
|
||||||
@ -12,13 +12,14 @@ class ClassObjectTransformer implements TransformerInterface
|
|||||||
* @param array|null $data
|
* @param array|null $data
|
||||||
* @param ClassData $meta
|
* @param ClassData $meta
|
||||||
*/
|
*/
|
||||||
public function transform(&$data, Meta $meta): void
|
public function transform(mixed $data, Meta $meta): mixed
|
||||||
{
|
{
|
||||||
if ($data === null) {
|
if ($data === null) {
|
||||||
return;
|
return $data;
|
||||||
}
|
}
|
||||||
$object = new HelloClassTransformersRequest2();
|
$object = new HelloClassTransformersRequest2();
|
||||||
$object->surname = '98789';
|
$object->surname = '98789';
|
||||||
$data = $object;
|
$data = $object;
|
||||||
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,4 +11,9 @@ class UUID
|
|||||||
{
|
{
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user