commit
52cbb03451
@ -0,0 +1,7 @@ |
||||
.idea |
||||
/var/ |
||||
/vendor/ |
||||
###> phpunit/phpunit ### |
||||
/phpunit.xml |
||||
.phpunit.result.cache |
||||
###< phpunit/phpunit ### |
@ -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 |
||||
|
@ -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
|
@ -0,0 +1,133 @@ |
||||
[![pipeline status](https://git.rinsvent.ru/rinsvent/transformer/badges/master/pipeline.svg)](https://git.rinsvent.ru/rinsvent/transformer/-/commits/master) |
||||
[![coverage report](https://git.rinsvent.ru/rinsvent/transformer/badges/master/coverage.svg)](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 |
||||
] |
||||
] |
||||
``` |
||||
|
@ -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 |
@ -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 |
||||
|
@ -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/* |
@ -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/" |
||||
} |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -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 |
@ -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 |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
@ -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() {} |
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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()); |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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'] |
||||
) {} |
||||
} |
@ -0,0 +1,9 @@ |
||||
<?php |
||||
declare(strict_types=1); |
||||
|
||||
namespace Rinsvent\Transformer\Transformer; |
||||
|
||||
interface TransformerInterface |
||||
{ |
||||
public function transform(mixed $data, Meta $meta): mixed; |
||||
} |
@ -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()); |
||||
} |
||||
} |
@ -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,0 +1,2 @@ |
||||
* |
||||
!.gitignore |
@ -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 |
||||
{ |
||||
|
||||
} |
@ -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 |
||||
*/ |
||||
} |
@ -0,0 +1,2 @@ |
||||
* |
||||
!.gitignore |
@ -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