Первоначальная заготовка для экспорера в массив
This commit is contained in:
commit
8adc3f19be
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
.idea
|
||||
/var/
|
||||
/vendor/
|
||||
###> phpunit/phpunit ###
|
||||
/phpunit.xml
|
||||
.phpunit.result.cache
|
||||
###< phpunit/phpunit ###
|
18
.gitlab-ci.yml
Normal file
18
.gitlab-ci.yml
Normal file
@ -0,0 +1,18 @@
|
||||
image: dh.rinsvent.ru/ci
|
||||
|
||||
variables:
|
||||
DOCKER_DRIVER: overlay2
|
||||
DOCKER_TLS_CERTDIR: ""
|
||||
|
||||
services:
|
||||
- docker:dind
|
||||
|
||||
before_script:
|
||||
- bash bin/docker/prepare-ci.sh
|
||||
|
||||
build:
|
||||
stage: build
|
||||
script:
|
||||
- docker login --username ${REGISTRY_USERNAME} --password ${REGISTRY_PASSWORD} dh.rinsvent.ru
|
||||
- bash bin/docker/ci.sh
|
||||
|
33
Makefile
Normal file
33
Makefile
Normal file
@ -0,0 +1,33 @@
|
||||
auth:
|
||||
docker exec -it -u1000:1000 dto2data_php bash
|
||||
|
||||
auth-root:
|
||||
docker exec -it dto2data_php bash
|
||||
|
||||
test:
|
||||
bin/codecept run $p
|
||||
|
||||
coverage:
|
||||
vendor/bin/codecept run --coverage --coverage-html=/app/var/temp.html
|
||||
|
||||
# make out container
|
||||
coverage-open:
|
||||
google-chrome var/temp.html/index.html
|
||||
|
||||
#docker
|
||||
start:
|
||||
docker-compose up -d
|
||||
stop:
|
||||
docker-compose down
|
||||
pull:
|
||||
docker-compose pull
|
||||
restart: stop start
|
||||
restart-php:
|
||||
docker-compose restart backend-php-fpm
|
||||
down-clear:
|
||||
docker-compose down -v --remove-orphans
|
||||
init: down-clear pull start
|
||||
|
||||
#prepare
|
||||
prepare-environment:
|
||||
bash bin/docker/prepare.sh
|
80
Readme.md
Normal file
80
Readme.md
Normal file
@ -0,0 +1,80 @@
|
||||
[![pipeline status](https://git.rinsvent.ru/rinsvent/dto2data/badges/master/pipeline.svg)](https://git.rinsvent.ru/rinsvent/dto2data/-/commits/master)
|
||||
[![coverage report](https://git.rinsvent.ru/rinsvent/dto2data/badges/master/coverage.svg)](https://git.rinsvent.ru/rinsvent/dto2data/-/commits/master)
|
||||
|
||||
Dto2data
|
||||
===
|
||||
|
||||
## Установка
|
||||
```php
|
||||
composer require rinsvent/dto2data
|
||||
```
|
||||
|
||||
## Пример
|
||||
|
||||
### Описания ДТО
|
||||
```php
|
||||
class BuyRequest
|
||||
{
|
||||
public string $phrase;
|
||||
public int $length;
|
||||
public bool $isFirst;
|
||||
}
|
||||
|
||||
interface BarInterface
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class Bar implements BarInterface
|
||||
{
|
||||
public float $barField;
|
||||
}
|
||||
|
||||
class HelloRequest
|
||||
{
|
||||
#[Trim]
|
||||
public string $surname;
|
||||
#[PropertyPath('fake_age')]
|
||||
public int $age;
|
||||
public array $emails;
|
||||
#[DTOMeta(class: Author::class)]
|
||||
public array $authors;
|
||||
public BuyRequest $buy;
|
||||
#[DTOMeta(class: Bar::class)]
|
||||
public BarInterface $bar;
|
||||
}
|
||||
```
|
||||
### Использование
|
||||
```php
|
||||
use Rinsvent\DTO2Data\Dto2DataConverter;
|
||||
|
||||
$dto2DataConverter = new Dto2DataConverter();
|
||||
$dto = $dto2DataConverter->convert([
|
||||
'surname' => ' asdf',
|
||||
'fake_age' => 3,
|
||||
'emails' => [
|
||||
'sfdgsa',
|
||||
'af234f',
|
||||
'asdf33333'
|
||||
],
|
||||
'authors' => [
|
||||
[
|
||||
'name' => 'Tolkien',
|
||||
],
|
||||
[
|
||||
'name' => 'Sapkovsky'
|
||||
]
|
||||
],
|
||||
'buy' => [
|
||||
'phrase' => 'Buy buy!!!',
|
||||
'length' => 10,
|
||||
'isFirst' => true,
|
||||
'extraData2' => '1234'
|
||||
],
|
||||
'bar' => [
|
||||
'barField' => 32
|
||||
],
|
||||
'extraData1' => 'qwer'
|
||||
], new HelloRequest);
|
||||
```
|
||||
|
9
bin/docker/ci.sh
Executable file
9
bin/docker/ci.sh
Executable file
@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
docker-compose -f ./docker-compose-ci.yml up -d
|
||||
|
||||
echo 'composer installing'
|
||||
docker exec -i dto2data_php composer install -q
|
||||
echo 'composer installed !!'
|
||||
|
||||
docker exec -i dto2data_php vendor/bin/codecept run --coverage
|
8
bin/docker/prepare-ci.sh
Executable file
8
bin/docker/prepare-ci.sh
Executable file
@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
FULL_PROJECT_NETWORK=$(docker network ls | grep full-project)
|
||||
if [ -z "$FULL_PROJECT_NETWORK" ]
|
||||
then
|
||||
docker network create full-project --subnet=192.168.221.0/25
|
||||
fi
|
||||
|
16
codeception.yml
Normal file
16
codeception.yml
Normal file
@ -0,0 +1,16 @@
|
||||
namespace: Rinsvent\DTO2Data\Tests
|
||||
paths:
|
||||
tests: tests
|
||||
output: tests/_output
|
||||
data: tests/_data
|
||||
support: tests/_support
|
||||
envs: tests/_envs
|
||||
actor_suffix: Tester
|
||||
extensions:
|
||||
enabled:
|
||||
- Codeception\Extension\RunFailed
|
||||
|
||||
coverage:
|
||||
enabled: true
|
||||
include:
|
||||
- src/*
|
29
composer.json
Normal file
29
composer.json
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "rinsvent/dto2data",
|
||||
"description": "Convert data to dto object",
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": "^8.0",
|
||||
"ext-ctype": "*",
|
||||
"ext-iconv": "*",
|
||||
"ext-json": "*",
|
||||
"symfony/string": "^5.3",
|
||||
"rinsvent/attribute-extractor": "^0.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"codeception/codeception": "^4.1",
|
||||
"codeception/module-phpbrowser": "^1.0.0",
|
||||
"codeception/module-asserts": "^1.0.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"tests\\": "tests/",
|
||||
"Rinsvent\\DTO2Data\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Rinsvent\\DTO2Data\\Tests\\": "tests/"
|
||||
}
|
||||
}
|
||||
}
|
4417
composer.lock
generated
Normal file
4417
composer.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
docker-compose-ci.yml
Normal file
15
docker-compose-ci.yml
Normal file
@ -0,0 +1,15 @@
|
||||
version: '3.3'
|
||||
|
||||
services:
|
||||
dto2data_php:
|
||||
image: dh.rinsvent.ru/php8dev
|
||||
container_name: dto2data_php
|
||||
volumes:
|
||||
- ./:/app
|
||||
environment:
|
||||
USE_COMPOSER_SCRIPTS: 0
|
||||
|
||||
networks:
|
||||
default:
|
||||
external:
|
||||
name: full-project
|
16
docker-compose.yml
Normal file
16
docker-compose.yml
Normal file
@ -0,0 +1,16 @@
|
||||
version: '3.3'
|
||||
|
||||
services:
|
||||
dto2data_php:
|
||||
image: dh.rinsvent.ru/php8dev
|
||||
container_name: dto2data_php
|
||||
volumes:
|
||||
- ./:/app
|
||||
environment:
|
||||
USE_COMPOSER_SCRIPTS: 1
|
||||
PHP_IDE_CONFIG: "serverName=dto2data_php"
|
||||
|
||||
networks:
|
||||
default:
|
||||
external:
|
||||
name: full-project
|
13
src/Attribute/DataPath.php
Normal file
13
src/Attribute/DataPath.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?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']
|
||||
) {}
|
||||
}
|
11
src/Attribute/HandleTags.php
Normal file
11
src/Attribute/HandleTags.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Attribute;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
|
||||
class HandleTags
|
||||
{
|
||||
public function __construct(
|
||||
public string $method,
|
||||
) {}
|
||||
}
|
15
src/Attribute/Schema.php
Normal file
15
src/Attribute/Schema.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Attribute;
|
||||
|
||||
use Rinsvent\DTO2Data\DTO\Map;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
|
||||
class Schema
|
||||
{
|
||||
public function __construct(
|
||||
public Map $map,
|
||||
/** @var string[] $tags */
|
||||
public array $tags = ['default']
|
||||
) {}
|
||||
}
|
21
src/DTO/Map.php
Normal file
21
src/DTO/Map.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\DTO;
|
||||
|
||||
class Map
|
||||
{
|
||||
public array $properties = [];
|
||||
public array $methods = [];
|
||||
|
||||
public function addProperty(string $propertyName, ?Map $map = null): static
|
||||
{
|
||||
$this->properties[$propertyName] = $map;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addMethod(string $methodName, ?Map $map = null): static
|
||||
{
|
||||
$this->methods[$methodName] = $map;
|
||||
return $this;
|
||||
}
|
||||
}
|
247
src/Dto2DataConverter.php
Normal file
247
src/Dto2DataConverter.php
Normal file
@ -0,0 +1,247 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data;
|
||||
|
||||
use ReflectionProperty;
|
||||
use Rinsvent\AttributeExtractor\ClassExtractor;
|
||||
use Rinsvent\AttributeExtractor\MethodExtractor;
|
||||
use Rinsvent\AttributeExtractor\PropertyExtractor;
|
||||
use Rinsvent\DTO2Data\Attribute\DataPath;
|
||||
use Rinsvent\DTO2Data\Attribute\HandleTags;
|
||||
use Rinsvent\DTO2Data\Attribute\Schema;
|
||||
use Rinsvent\DTO2Data\DTO\Map;
|
||||
use Rinsvent\DTO2Data\Resolver\TransformerResolverStorage;
|
||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
||||
use Rinsvent\DTO2Data\Transformer\TransformerInterface;
|
||||
|
||||
class Dto2DataConverter
|
||||
{
|
||||
public function getTags(object $object, array $tags = []): array
|
||||
{
|
||||
return $this->processTags($object, $tags);
|
||||
}
|
||||
|
||||
public function convert(object $object, array $tags = []): array
|
||||
{
|
||||
$tags = empty($tags) ? ['default'] : $tags;
|
||||
$schema = $this->grabSchema($object, $tags);
|
||||
return $this->convertByMap($object, $schema->map, $tags);
|
||||
}
|
||||
|
||||
public function convertByMap($objectData, Map $map, array $tags = []): array
|
||||
{
|
||||
$data = [];
|
||||
|
||||
$reflectionObject = new \ReflectionObject($object);
|
||||
foreach ($map->properties as $propertyName => $childMap) {
|
||||
$property = new ReflectionProperty($object, $propertyName);
|
||||
$value = $this->getValue($object, $property);
|
||||
if ($childMap && !is_scalar($value)) {
|
||||
if (is_array($value)) {
|
||||
$tmpValue = [];
|
||||
foreach($value as $objectDataItem) {
|
||||
$tmpValue[] = $this->convertByMap($objectDataItem, $childMap, $tags);
|
||||
}
|
||||
$value = $tmpValue;
|
||||
} else {
|
||||
$value = $this->convertByMap($value, $childMap, $tags);
|
||||
}
|
||||
}
|
||||
$this->processTransformers($property, $value, $tags);
|
||||
$dataPath = $this->grabDataPath($property, $tags);
|
||||
$dataPath = $dataPath ?? $propertyName;
|
||||
$data[$dataPath] = $value;
|
||||
}
|
||||
|
||||
foreach ($map->methods as $methodName => $childMap) {
|
||||
$method = new \ReflectionMethod($object, $methodName);
|
||||
$value = $this->getMethodValue($object, $method);
|
||||
if ($childMap) {
|
||||
$value = $this->convertByMap($value, $childMap, $tags);
|
||||
}
|
||||
$this->processMethodTransformers($method, $value, $tags);
|
||||
$dataPath = $this->grabMethodDataPath($method, $tags);
|
||||
$dataPath = $dataPath ?? $propertyName;
|
||||
$data[$dataPath] = $value;
|
||||
}
|
||||
|
||||
$this->processClassTransformers($reflectionObject, $data, $tags);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получаем теги для обработки
|
||||
*/
|
||||
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 getValue(object $object, \ReflectionProperty $property)
|
||||
{
|
||||
if (!$property->isInitialized($object)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$property->isPublic()) {
|
||||
$property->setAccessible(true);
|
||||
}
|
||||
|
||||
$value = $property->getValue($object);
|
||||
|
||||
if (!$property->isPublic()) {
|
||||
$property->setAccessible(false);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function getMethodValue(object $object, \ReflectionMethod $method)
|
||||
{
|
||||
if (!$method->isPublic()) {
|
||||
$method->setAccessible(true);
|
||||
}
|
||||
|
||||
$value = $method->invoke($object);
|
||||
|
||||
if (!$method->isPublic()) {
|
||||
$method->setAccessible(false);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function grabSchema(object $object, array $tags): ?Schema
|
||||
{
|
||||
$classExtractor = new ClassExtractor($object::class);
|
||||
/** @var Schema $schema */
|
||||
while ($schema = $classExtractor->fetch(Schema::class)) {
|
||||
$filteredTags = array_diff($tags, $schema->tags);
|
||||
if (count($filteredTags) === count($tags)) {
|
||||
continue;
|
||||
}
|
||||
return $schema;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
16
src/Resolver/SimpleResolver.php
Normal file
16
src/Resolver/SimpleResolver.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
11
src/Resolver/TransformerResolverInterface.php
Normal file
11
src/Resolver/TransformerResolverInterface.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Resolver;
|
||||
|
||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
||||
use Rinsvent\DTO2Data\Transformer\TransformerInterface;
|
||||
|
||||
interface TransformerResolverInterface
|
||||
{
|
||||
public function resolve(Meta $meta): TransformerInterface;
|
||||
}
|
32
src/Resolver/TransformerResolverStorage.php
Normal file
32
src/Resolver/TransformerResolverStorage.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?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];
|
||||
}
|
||||
}
|
15
src/Transformer/Meta.php
Normal file
15
src/Transformer/Meta.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?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']
|
||||
) {}
|
||||
}
|
8
src/Transformer/TransformerInterface.php
Normal file
8
src/Transformer/TransformerInterface.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Transformer;
|
||||
|
||||
interface TransformerInterface
|
||||
{
|
||||
public function transform(&$data, Meta $meta): void;
|
||||
}
|
14
src/Transformer/Trim.php
Normal file
14
src/Transformer/Trim.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?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());
|
||||
}
|
||||
}
|
18
src/Transformer/TrimTransformer.php
Normal file
18
src/Transformer/TrimTransformer.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
0
tests/_data/.gitkeep
Normal file
0
tests/_data/.gitkeep
Normal file
2
tests/_output/.gitignore
vendored
Normal file
2
tests/_output/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
0
tests/_support/.gitkeep
Normal file
0
tests/_support/.gitkeep
Normal file
10
tests/_support/Helper/Unit.php
Normal file
10
tests/_support/Helper/Unit.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace Rinsvent\DTO2Data\Tests\Helper;
|
||||
|
||||
// here you can define custom actions
|
||||
// all public methods declared in helper class will be available in $I
|
||||
|
||||
class Unit extends \Codeception\Module
|
||||
{
|
||||
|
||||
}
|
27
tests/_support/UnitTester.php
Normal file
27
tests/_support/UnitTester.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests;
|
||||
|
||||
/**
|
||||
* Inherited Methods
|
||||
* @method void wantToTest($text)
|
||||
* @method void wantTo($text)
|
||||
* @method void execute($callable)
|
||||
* @method void expectTo($prediction)
|
||||
* @method void expect($prediction)
|
||||
* @method void amGoingTo($argumentation)
|
||||
* @method void am($role)
|
||||
* @method void lookForwardTo($achieveValue)
|
||||
* @method void comment($description)
|
||||
* @method void pause()
|
||||
*
|
||||
* @SuppressWarnings(PHPMD)
|
||||
*/
|
||||
class UnitTester extends \Codeception\Actor
|
||||
{
|
||||
use _generated\UnitTesterActions;
|
||||
|
||||
/**
|
||||
* Define custom actions here
|
||||
*/
|
||||
}
|
2
tests/_support/_generated/.gitignore
vendored
Normal file
2
tests/_support/_generated/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
10
tests/unit.suite.yml
Normal file
10
tests/unit.suite.yml
Normal file
@ -0,0 +1,10 @@
|
||||
# Codeception Test Suite Configuration
|
||||
#
|
||||
# Suite for unit or integration tests.
|
||||
|
||||
actor: UnitTester
|
||||
modules:
|
||||
enabled:
|
||||
- Asserts
|
||||
- \Rinsvent\DTO2Data\Tests\Helper\Unit
|
||||
step_decorators: ~
|
77
tests/unit/Converter/FillTest.php
Normal file
77
tests/unit/Converter/FillTest.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\Converter;
|
||||
|
||||
use Rinsvent\DTO2Data\Dto2DataConverter;
|
||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Author;
|
||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Bar;
|
||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\BuyRequest;
|
||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\HelloRequest;
|
||||
|
||||
class FillTest extends \Codeception\Test\Unit
|
||||
{
|
||||
/**
|
||||
* @var \UnitTester
|
||||
*/
|
||||
protected $tester;
|
||||
|
||||
protected function _before()
|
||||
{
|
||||
}
|
||||
|
||||
protected function _after()
|
||||
{
|
||||
}
|
||||
|
||||
// tests
|
||||
public function testSuccessFillRequestData()
|
||||
{
|
||||
$dto2DataConverter = new Dto2DataConverter();
|
||||
$helloRequest = new HelloRequest;
|
||||
$helloRequest->surname = ' asdf';
|
||||
$helloRequest->age = 3;
|
||||
$helloRequest->emails =[
|
||||
'sfdgsa',
|
||||
'af234f',
|
||||
'asdf33333'
|
||||
];
|
||||
$author1 = new Author();
|
||||
$author1->name = 'Tolkien';
|
||||
$author2 = new Author();
|
||||
$author2->name = 'Sapkovsky';
|
||||
$helloRequest->authors = [
|
||||
$author1,
|
||||
$author2
|
||||
];
|
||||
$buy = new BuyRequest();
|
||||
$buy->phrase = 'Buy buy!!!';
|
||||
$buy->length = 10;
|
||||
$buy->isFirst = true;
|
||||
$helloRequest->buy = $buy;
|
||||
$bar = new Bar();
|
||||
$bar->barField = 32;
|
||||
$helloRequest->bar = $bar;
|
||||
|
||||
$dto = $dto2DataConverter->convert($helloRequest);
|
||||
|
||||
// $this->assertEquals('asdf', $dto->surname);
|
||||
// $this->assertEquals(3, $dto->age);
|
||||
// $this->assertEquals([
|
||||
// 'sfdgsa',
|
||||
// 'af234f',
|
||||
// 'asdf33333'
|
||||
// ], $dto->emails);
|
||||
// $this->assertInstanceOf(BuyRequest::class, $dto->buy);
|
||||
// $this->assertEquals('Buy buy!!!', $dto->buy->phrase);
|
||||
// $this->assertEquals(10, $dto->buy->length);
|
||||
// $this->assertEquals(true, $dto->buy->isFirst);
|
||||
//
|
||||
// $this->assertCount(2, $dto->authors);
|
||||
// $this->assertEquals('Tolkien', $dto->authors[0]->name);
|
||||
// $this->assertEquals('Sapkovsky', $dto->authors[1]->name);
|
||||
//
|
||||
// $this->assertInstanceOf(Bar::class, $dto->bar);
|
||||
// $this->assertIsFloat($dto->bar->barField);
|
||||
// $this->assertEquals(32.0, $dto->bar->barField);
|
||||
}
|
||||
}
|
39
tests/unit/Converter/TagsTest.php
Normal file
39
tests/unit/Converter/TagsTest.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
8
tests/unit/Converter/fixtures/FillTest/Author.php
Normal file
8
tests/unit/Converter/fixtures/FillTest/Author.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
class Author
|
||||
{
|
||||
public string $name;
|
||||
}
|
8
tests/unit/Converter/fixtures/FillTest/Bar.php
Normal file
8
tests/unit/Converter/fixtures/FillTest/Bar.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
class Bar implements BarInterface
|
||||
{
|
||||
public float $barField;
|
||||
}
|
8
tests/unit/Converter/fixtures/FillTest/BarInterface.php
Normal file
8
tests/unit/Converter/fixtures/FillTest/BarInterface.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
interface BarInterface
|
||||
{
|
||||
|
||||
}
|
10
tests/unit/Converter/fixtures/FillTest/BuyRequest.php
Normal file
10
tests/unit/Converter/fixtures/FillTest/BuyRequest.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
class BuyRequest
|
||||
{
|
||||
public string $phrase;
|
||||
public int $length;
|
||||
public bool $isFirst;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer\ClassData;
|
||||
|
||||
#[ClassData]
|
||||
class HelloClassTransformersRequest
|
||||
{
|
||||
public string $surname;
|
||||
public int $age;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer\ClassObject;
|
||||
|
||||
#[ClassObject]
|
||||
class HelloClassTransformersRequest2
|
||||
{
|
||||
public string $surname;
|
||||
public int $age;
|
||||
}
|
19
tests/unit/Converter/fixtures/FillTest/HelloRequest.php
Normal file
19
tests/unit/Converter/fixtures/FillTest/HelloRequest.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\DTO2Data\Attribute\DataPath;
|
||||
use Rinsvent\DTO2Data\Transformer\Trim;
|
||||
|
||||
#[HelloSchema]
|
||||
class HelloRequest
|
||||
{
|
||||
#[Trim]
|
||||
public string $surname;
|
||||
#[DataPath('fake_age')]
|
||||
public int $age;
|
||||
public array $emails;
|
||||
public array $authors;
|
||||
public BuyRequest $buy;
|
||||
public BarInterface $bar;
|
||||
}
|
38
tests/unit/Converter/fixtures/FillTest/HelloSchema.php
Normal file
38
tests/unit/Converter/fixtures/FillTest/HelloSchema.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\DTO2Data\Attribute\Schema;
|
||||
use Rinsvent\DTO2Data\DTO\Map;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
|
||||
class HelloSchema extends Schema
|
||||
{
|
||||
public function __construct(
|
||||
) {
|
||||
|
||||
parent::__construct(
|
||||
(new Map())
|
||||
->addProperty('surname')
|
||||
->addProperty('age')
|
||||
->addProperty('emails')
|
||||
->addProperty(
|
||||
'authors',
|
||||
(new Map())
|
||||
->addProperty('name')
|
||||
)
|
||||
->addProperty('buy',
|
||||
(new Map)
|
||||
->addProperty('phrase')
|
||||
->addProperty('length')
|
||||
->addProperty('isFirst')
|
||||
)
|
||||
->addProperty('bar',
|
||||
(new Map())
|
||||
->addProperty('barField')
|
||||
)
|
||||
,
|
||||
['default']
|
||||
);
|
||||
}
|
||||
}
|
18
tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php
Normal file
18
tests/unit/Converter/fixtures/FillTest/HelloTagsRequest.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\DTO2Data\Attribute\DataPath;
|
||||
use Rinsvent\DTO2Data\Attribute\HandleTags;
|
||||
|
||||
#[HandleTags(method: 'getTags')]
|
||||
class HelloTagsRequest extends HelloRequest
|
||||
{
|
||||
#[DataPath('fake_age2', tags: ['surname-group'])]
|
||||
public int $age;
|
||||
|
||||
public function getTags(array $tags)
|
||||
{
|
||||
return 'Surname1234' === trim(($this->surname ?? '')) ? ['surname-group'] : $tags;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
||||
|
||||
#[\Attribute]
|
||||
class ClassData extends Meta
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
||||
use Rinsvent\DTO2Data\Transformer\TransformerInterface;
|
||||
|
||||
class ClassDataTransformer implements TransformerInterface
|
||||
{
|
||||
/**
|
||||
* @param array|null $data
|
||||
* @param ClassData $meta
|
||||
*/
|
||||
public function transform(&$data, Meta $meta): void
|
||||
{
|
||||
if ($data === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($data['surname'])) {
|
||||
$data['surname'] = '123454321';
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
||||
|
||||
#[\Attribute]
|
||||
class ClassObject extends Meta
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\DTO2Data\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest2;
|
||||
use Rinsvent\DTO2Data\Transformer\Meta;
|
||||
use Rinsvent\DTO2Data\Transformer\TransformerInterface;
|
||||
|
||||
class ClassObjectTransformer implements TransformerInterface
|
||||
{
|
||||
/**
|
||||
* @param array|null $data
|
||||
* @param ClassData $meta
|
||||
*/
|
||||
public function transform(&$data, Meta $meta): void
|
||||
{
|
||||
if ($data === null) {
|
||||
return;
|
||||
}
|
||||
$object = new HelloClassTransformersRequest2();
|
||||
$object->surname = '98789';
|
||||
$data = $object;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user