Подготовил базовыю реализацию для трансформеров
Нужно добавить тесты и пофиксить все
This commit is contained in:
commit
52cbb03451
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 transformer_php bash
|
||||||
|
|
||||||
|
auth-root:
|
||||||
|
docker exec -it transformer_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
|
133
Readme.md
Normal file
133
Readme.md
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
[](https://git.rinsvent.ru/rinsvent/transformer/-/commits/master)
|
||||||
|
[](https://git.rinsvent.ru/rinsvent/transformer/-/commits/master)
|
||||||
|
|
||||||
|
Transformer
|
||||||
|
===
|
||||||
|
|
||||||
|
## Установка
|
||||||
|
```php
|
||||||
|
composer require rinsvent/transformer
|
||||||
|
```
|
||||||
|
|
||||||
|
## Пример
|
||||||
|
|
||||||
|
### Описания ДТО
|
||||||
|
```php
|
||||||
|
class BuyRequest
|
||||||
|
{
|
||||||
|
public string $phrase;
|
||||||
|
public int $length;
|
||||||
|
public bool $isFirst;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BarInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Bar implements BarInterface
|
||||||
|
{
|
||||||
|
public float $barField;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[HelloSchema]
|
||||||
|
class HelloRequest
|
||||||
|
{
|
||||||
|
#[Trim]
|
||||||
|
public string $surname;
|
||||||
|
#[DataPath('fake_age')]
|
||||||
|
public int $age;
|
||||||
|
public array $emails;
|
||||||
|
public array $authors;
|
||||||
|
public array $authors2;
|
||||||
|
public array $authors3;
|
||||||
|
public BuyRequest $buy;
|
||||||
|
public BarInterface $bar;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
### Использование
|
||||||
|
```php
|
||||||
|
use Rinsvent\Transformer\Transformer;
|
||||||
|
|
||||||
|
$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
|
||||||
|
];
|
||||||
|
$helloRequest->authors2 = [
|
||||||
|
[
|
||||||
|
"name" => "Tolkien"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"name" => "Sapkovsky"
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$helloRequest->authors3 = [
|
||||||
|
[
|
||||||
|
"name" => "Tolkien"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"name" => "Sapkovsky"
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$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;
|
||||||
|
|
||||||
|
$transformer = new Transformer();
|
||||||
|
$dto = $transformer->convert($helloRequest);
|
||||||
|
```
|
||||||
|
### Результат
|
||||||
|
```php
|
||||||
|
$dto = [
|
||||||
|
"surname" => "asdf",
|
||||||
|
"fake_age" => 3,
|
||||||
|
"emails" => [
|
||||||
|
"sfdgsa",
|
||||||
|
"af234f",
|
||||||
|
"asdf33333"
|
||||||
|
],
|
||||||
|
"authors" => [
|
||||||
|
[
|
||||||
|
"name" => "Tolkien"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"name" => "Sapkovsky"
|
||||||
|
]
|
||||||
|
],
|
||||||
|
"authors2" => [
|
||||||
|
[
|
||||||
|
"name" => "Tolkien"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"name" => "Sapkovsky"
|
||||||
|
]
|
||||||
|
],
|
||||||
|
"authors3" => [],
|
||||||
|
"buy" => [
|
||||||
|
"phrase" => "Buy buy!!!",
|
||||||
|
"length" => 10,
|
||||||
|
"isFirst" => true
|
||||||
|
],
|
||||||
|
"bar" => [
|
||||||
|
"barField" => 32
|
||||||
|
]
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
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 transformer_php composer install -q
|
||||||
|
echo 'composer installed !!'
|
||||||
|
|
||||||
|
docker exec -i transformer_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\Transformer\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/*
|
27
composer.json
Normal file
27
composer.json
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"name": "rinsvent/transformer",
|
||||||
|
"description": "transform data",
|
||||||
|
"license": "MIT",
|
||||||
|
"require": {
|
||||||
|
"php": "^8.0",
|
||||||
|
"ext-ctype": "*",
|
||||||
|
"ext-iconv": "*",
|
||||||
|
"ext-json": "*"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"codeception/codeception": "^4.1",
|
||||||
|
"codeception/module-phpbrowser": "^1.0.0",
|
||||||
|
"codeception/module-asserts": "^1.0.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"tests\\": "tests/",
|
||||||
|
"Rinsvent\\Transformer\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"Rinsvent\\Transformer\\Tests\\": "tests/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
4489
composer.lock
generated
Normal file
4489
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:
|
||||||
|
transformer_php:
|
||||||
|
image: dh.rinsvent.ru/php8dev
|
||||||
|
container_name: transformer_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:
|
||||||
|
transformer_php:
|
||||||
|
image: dh.rinsvent.ru/php8dev
|
||||||
|
container_name: transformer_php
|
||||||
|
volumes:
|
||||||
|
- ./:/app
|
||||||
|
environment:
|
||||||
|
USE_COMPOSER_SCRIPTS: 1
|
||||||
|
PHP_IDE_CONFIG: "serverName=transformer_php"
|
||||||
|
|
||||||
|
networks:
|
||||||
|
default:
|
||||||
|
external:
|
||||||
|
name: full-project
|
17
src/Resolver/SimpleResolver.php
Normal file
17
src/Resolver/SimpleResolver.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rinsvent\Transformer\Resolver;
|
||||||
|
|
||||||
|
use Rinsvent\Transformer\Transformer\Meta;
|
||||||
|
use Rinsvent\Transformer\Transformer\TransformerInterface;
|
||||||
|
|
||||||
|
class SimpleResolver implements TransformerResolverInterface
|
||||||
|
{
|
||||||
|
public function resolve(Meta $meta): TransformerInterface
|
||||||
|
{
|
||||||
|
$metaClass = $meta::class;
|
||||||
|
$transformerClass = $metaClass . 'Transformer';
|
||||||
|
return new $transformerClass;
|
||||||
|
}
|
||||||
|
}
|
12
src/Resolver/TransformerResolverInterface.php
Normal file
12
src/Resolver/TransformerResolverInterface.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rinsvent\Transformer\Resolver;
|
||||||
|
|
||||||
|
use Rinsvent\Transformer\Transformer\Meta;
|
||||||
|
use Rinsvent\Transformer\Transformer\TransformerInterface;
|
||||||
|
|
||||||
|
interface TransformerResolverInterface
|
||||||
|
{
|
||||||
|
public function resolve(Meta $meta): TransformerInterface;
|
||||||
|
}
|
36
src/Resolver/TransformerResolverStorage.php
Normal file
36
src/Resolver/TransformerResolverStorage.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rinsvent\Transformer\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];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function __clone() {}
|
||||||
|
private function __construct() {}
|
||||||
|
}
|
25
src/Transformer.php
Normal file
25
src/Transformer.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rinsvent\Transformer;
|
||||||
|
|
||||||
|
use Rinsvent\Transformer\Resolver\TransformerResolverStorage;
|
||||||
|
use Rinsvent\Transformer\Transformer\Meta;
|
||||||
|
use Rinsvent\Transformer\Transformer\TransformerInterface;
|
||||||
|
|
||||||
|
class Transformer
|
||||||
|
{
|
||||||
|
public function transform(mixed $value, Meta $meta): mixed
|
||||||
|
{
|
||||||
|
$transformer = $this->grabTransformer($meta);
|
||||||
|
$transformer->transform($value, $meta);
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function grabTransformer(Meta $meta): TransformerInterface
|
||||||
|
{
|
||||||
|
$storage = TransformerResolverStorage::getInstance();
|
||||||
|
$resolver = $storage->get($meta::TYPE);
|
||||||
|
return $resolver->resolve($meta);
|
||||||
|
}
|
||||||
|
}
|
15
src/Transformer/DateTimeFormat.php
Normal file
15
src/Transformer/DateTimeFormat.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rinsvent\Transformer\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());
|
||||||
|
}
|
||||||
|
}
|
19
src/Transformer/DateTimeFormatTransformer.php
Normal file
19
src/Transformer/DateTimeFormatTransformer.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rinsvent\Transformer\Transformer;
|
||||||
|
|
||||||
|
class DateTimeFormatTransformer implements TransformerInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param \DateTimeImmutable|\DateTime|null $data
|
||||||
|
* @param DateTimeFormat $meta
|
||||||
|
*/
|
||||||
|
public function transform(mixed $data, Meta $meta): mixed
|
||||||
|
{
|
||||||
|
if ($data === null) {
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
return $data->format($meta->format);
|
||||||
|
}
|
||||||
|
}
|
16
src/Transformer/Meta.php
Normal file
16
src/Transformer/Meta.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rinsvent\Transformer\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']
|
||||||
|
) {}
|
||||||
|
}
|
9
src/Transformer/TransformerInterface.php
Normal file
9
src/Transformer/TransformerInterface.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rinsvent\Transformer\Transformer;
|
||||||
|
|
||||||
|
interface TransformerInterface
|
||||||
|
{
|
||||||
|
public function transform(mixed $data, Meta $meta): mixed;
|
||||||
|
}
|
15
src/Transformer/Trim.php
Normal file
15
src/Transformer/Trim.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rinsvent\Transformer\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());
|
||||||
|
}
|
||||||
|
}
|
19
src/Transformer/TrimTransformer.php
Normal file
19
src/Transformer/TrimTransformer.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rinsvent\Transformer\Transformer;
|
||||||
|
|
||||||
|
class TrimTransformer implements TransformerInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string|null $data
|
||||||
|
* @param Trim $meta
|
||||||
|
*/
|
||||||
|
public function transform(mixed $data, Meta $meta): mixed
|
||||||
|
{
|
||||||
|
if ($data === null) {
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
return 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\Transformer\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\Transformer\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\Transformer\Tests\Helper\Unit
|
||||||
|
step_decorators: ~
|
Loading…
Reference in New Issue
Block a user