Skip to content

Commit

Permalink
Merge pull request #8 from yzen-dev/feature/v3.0.0-dev
Browse files Browse the repository at this point in the history
Release v3.0.0
  • Loading branch information
yzen-dev committed Jun 15, 2023
2 parents 7d17522 + 59d96ea commit 8f16aaa
Show file tree
Hide file tree
Showing 103 changed files with 5,957 additions and 1,649 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -2,5 +2,6 @@ tests/ export-ignore
phpunit.xml export-ignore
phpbench.json export-ignore
phpstan.xml export-ignore
psalm.xml export-ignore
coverage.xml export-ignore
phpstan.neon export-ignore
26 changes: 26 additions & 0 deletions .github/workflows/cs.yml
@@ -0,0 +1,26 @@
name: Coding Standards

on:
pull_request:
push:

jobs:
coding-standards:
name: Coding Standards
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Check codestyle
run: composer run-script phpcs
38 changes: 38 additions & 0 deletions .github/workflows/mutation.yml
@@ -0,0 +1,38 @@
name: mutation test

on:
push:

pull_request:

jobs:
mutation:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

# Setup PHP versions, run checks
- name: PHP setup
uses: shivammathur/setup-php@v2
with:
php: >-
['8.2']
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run mutation
run: ./vendor/bin/infection
env:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
17 changes: 7 additions & 10 deletions .github/workflows/code-analyze.yml → .github/workflows/psalm.yml
@@ -1,13 +1,12 @@
name: Code analyze
name: psalm

on:
push:
branches: [ master, dev ]

pull_request:
branches: [ master, dev ]

jobs:
build:
psalm:

runs-on: ubuntu-latest

Expand All @@ -18,7 +17,8 @@ jobs:
- name: PHP setup
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php: >-
['8.0', '8.1', '8.2']
- name: Cache Composer packages
id: composer-cache
Expand All @@ -32,9 +32,6 @@ jobs:
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run phpstan
run: composer run-script phpstan

- name: Check codestyle
run: composer run-script phpcs
- name: Static analysis.
run: vendor/bin/psalm --shepherd --stats --output-format=github --php-version=${{ matrix.php }}

6 changes: 2 additions & 4 deletions .github/workflows/tests.yml
Expand Up @@ -2,9 +2,8 @@ name: Run tests

on:
push:
branches: [ master, dev, feature/cache-v2-php8.1 ]

pull_request:
branches: [ master, dev ]

jobs:
tests:
Expand All @@ -13,8 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ["8.1", "8.0"]
dependency-version: [prefer-lowest, prefer-stable]
php: ["8.2", "8.1"]
name: P${{ matrix.php }} - ${{ matrix.dependency-version }}

steps:
Expand Down
10 changes: 6 additions & 4 deletions .gitignore
@@ -1,5 +1,7 @@
.DS_Store
.idea
vendor
.phpunit.result.cache
.phpunit.cache/
.DS_Store
infection.json

/.cache
/.tmp
/vendor
2 changes: 0 additions & 2 deletions README.RU.md
Expand Up @@ -7,8 +7,6 @@
![Packagist Downloads](https://img.shields.io/packagist/dm/yzen.dev/plain-to-class)
![Packagist Downloads](https://img.shields.io/packagist/dt/yzen.dev/plain-to-class)

> Увы, но я не владею английским, так что документация была составлена с помощью Google Translate :С
> Я буду рад, если вы поможете мне более грамотно описать документацию :)

Эта библиотека позволит вам легко преобразовать любой набор данных в нужный вам объект. От вас не требуется менять структуру классов, наследовать их от внешних модулей и т.д. Никаких танцев с бубнами - только данные и нужный класс.

Expand Down
34 changes: 27 additions & 7 deletions README.md
Expand Up @@ -7,6 +7,11 @@
![Packagist Downloads](https://img.shields.io/packagist/dm/yzen.dev/plain-to-class)
![Packagist Downloads](https://img.shields.io/packagist/dt/yzen.dev/plain-to-class)

[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyzen-dev%2Fplain-to-class%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yzen-dev/plain-to-class/master)
[![type-coverage](https://shepherd.dev/github/yzen-dev/plain-to-class/coverage.svg)](https://shepherd.dev/github/yzen-dev/plain-to-class)
[![psalm-level](https://shepherd.dev/github/yzen-dev/plain-to-class/level.svg)](https://shepherd.dev/github/yzen-dev/plain-to-class)


> Alas, I do not speak English, and the documentation was compiled through google translator :( I will be glad if you can help me describe the documentation more correctly :)
This library will allow you to easily convert any data set into the object you need. You are not required to change the structure of classes, inherit them from external modules, etc. No dancing with tambourines - just data and the right class.
Expand All @@ -33,6 +38,7 @@ This is where this package comes to the rescue, which takes care of all the work
- [After Transform](#after-transform)
- [Custom transform](#custom-transform)
- [Comparison](#Comparison)
- [Cache](#Cache)

## **Installation**

Expand Down Expand Up @@ -68,7 +74,10 @@ $data = [
'email' => 'test@mail.com',
'balance' => 128.41,
];
$dto = ClassTransformer::transform(CreateUserDTO::class, $data);

$dto = (new Hydrator())->create(CreateUserDTO::class, $data);
// or static init
$dto = Hydrator::init()->create(CreateUserDTO::class, $data);
var_dump($dto);
```

Expand All @@ -83,7 +92,7 @@ object(\LoginDTO)
Also for php 8 you can pass named arguments:

```php
$dto = ClassTransformer::transform(CreateUserDTO::class,
$dto = Hydrator::init()->create(CreateUserDTO::class,
email: 'test@mail.com',
balance: 128.41
);
Expand All @@ -109,7 +118,7 @@ $data = [
'cost' => 10012.23,
];

$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
$purchaseDTO = Hydrator::init()->create(PurchaseDTO::class, $data);
var_dump($purchaseDTO);
```

Expand Down Expand Up @@ -153,7 +162,7 @@ $data = [
['id' => 2, 'name' => 'bread',],
],
];
$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
$purchaseDTO = Hydrator::init()->create(PurchaseDTO::class, $data);
```

### **Anonymous array**
Expand All @@ -166,7 +175,7 @@ $data = [
['id' => 1, 'name' => 'phone'],
['id' => 2, 'name' => 'bread'],
];
$products = ClassTransformer::transformCollection(ProductDTO::class, $data);
$products = Hydrator::init()->createCollection(ProductDTO::class, $data);
```

As a result of this execution, you will get an array of ProductDTO objects
Expand Down Expand Up @@ -199,7 +208,7 @@ which can then be easily unpacked.
'user' => ['id' => 3, 'email' => 'fake@mail.com', 'balance' => 10012.23,],
];

$result = ClassTransformer::transformMultiple([UserDTO::class, PurchaseDTO::class], [$userData, $purchaseData]);
$result = Hydrator::init()->createMultiple([UserDTO::class, PurchaseDTO::class], [$userData, $purchaseData]);

[$user, $purchase] = $result;
var_dump($user);
Expand Down Expand Up @@ -257,7 +266,7 @@ class WritingStyleSnakeCaseDTO
'contactFio' => 'yzen.dev',
'contactEmail' => 'test@mail.com',
];
$model = ClassTransformer::transform(WritingStyleSnakeCaseDTO::class, $data);
$model = Hydrator::init()->create(WritingStyleSnakeCaseDTO::class, $data);
var_dump($model);
```

Expand Down Expand Up @@ -340,6 +349,17 @@ class CustomTransformUserDTOArray
}
```

### **Cache**

The package supports a class caching mechanism to avoid the cost of reflection. This functionality is recommended to be used only if you have very voluminous classes, or there is a cyclic transformation of multiple entities. On ordinary lightweight DTO, there will be only 5-10%, and this will be unnecessary access in the file system.

You can enable caching by passing the config to the hydrator constructor:

```php
(new Hydrator(new HydratorConfig(true)))
->create(PurchaseDto::class, $data);
```

### Comparison
I also made a comparison with current analogues and here are the main disadvantages
- Works only for a specific framework
Expand Down
27 changes: 27 additions & 0 deletions bin/plain-to-class-clear
@@ -0,0 +1,27 @@
#!/usr/bin/env php
<?php

function deleteDir($dirPath)
{
if (!str_ends_with($dirPath, '/')) {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);

foreach ($files as $file) {
echo 'Remove' . $file . PHP_EOL;
if (is_dir($file)) {
deleteDir($file);
} else {
unlink($file);
}
}
rmdir($dirPath);
}

if (file_exists(__DIR__ . '/../.cache')) {
deleteDir(__DIR__ . '/../.cache');
} elseif (file_exists(__DIR__ . '/../vendor/yzen.dev/plain-to-class/.cache')) {
deleteDir(__DIR__ . '/../vendor/yzen.dev/plain-to-class/.cache');
}
exit(0);
7 changes: 7 additions & 0 deletions bin/plain-to-class-clear.bat
@@ -0,0 +1,7 @@
@echo off

if "%PHP_PEAR_PHP_BIN%" neq "" (
set PHPBIN=%PHP_PEAR_PHP_BIN%
) else set PHPBIN=php

"%PHPBIN%" "%~dp0\plain-to-class-clear" %*
53 changes: 27 additions & 26 deletions composer.json
@@ -1,6 +1,6 @@
{
"name": "yzen.dev/plain-to-class",
"version": "2.0",
"version": "3.0",
"description": "Class-transformer to transform your dataset into a structured object",
"minimum-stability": "dev",
"prefer-stable": true,
Expand All @@ -17,7 +17,10 @@
"source": "https://github.com/yzen-dev/plain-to-class"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"infection/extension-installer": true
}
},
"autoload": {
"psr-4": {
Expand All @@ -30,39 +33,37 @@
}
},
"require": {
"php": "^8.0"
"php": "^8.1"
},
"require-dev": {
"mockery/mockery": "1.5.1",
"phpbench/phpbench": "1.2.8",
"phpstan/phpstan": "1.9.12",
"phpunit/phpunit": "9.5.28",
"squizlabs/php_codesniffer": "3.7.1"
"phpunit/phpunit": "10.2.1",
"roave/infection-static-analysis-plugin": "1.32",
"squizlabs/php_codesniffer": "3.7.1",
"symfony/var-dumper": "6.3",
"vimeo/psalm": "5.12.0"
},
"bin": [
"bin/plain-to-class-clear"
],
"scripts": {
"phpunit": [
"./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml"
],
"phpstan": [
"./vendor/bin/phpstan analyse -c ./phpstan.neon src"
],
"phpcs": [
"./vendor/bin/phpcs --standard=./phpcs.xml -n --no-cache -s"
],
"phpcbf": [
"./vendor/bin/phpcbf --standard=./phpcs.xml -n --no-cache -s"
],
"coverage": [
"./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --coverage-text --colors=never --coverage-clover coverage.xml"
],
"coverage-html": [
"./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --coverage-html public/coverage --coverage-text"
]
"phpunit": "./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml",
"infection": "./vendor/bin/roave-infection-static-analysis-plugin",
"infection-html": "./vendor/bin/roave-infection-static-analysis-plugin --logger-html='.tmp/mutation-report.html'",
"psalm": "./vendor/bin/psalm",
"phpcs": "./vendor/bin/phpcs --standard=./phpcs.xml -n --no-cache -s",
"phpcbf": "./vendor/bin/phpcbf --standard=./phpcs.xml -n --no-cache -s",
"coverage": "./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --coverage-text --colors=never --coverage-clover coverage.xml",
"coverage-html": "./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --coverage-html public/coverage --coverage-text"
},
"scripts-descriptions": {
"phpunit": "Run tests",
"infection": "Run mutation tests",
"infection-html": "Generate mutation tests report",
"psalm": "Run static analyze",
"phpcs": "Checking codestyle",
"coverage": "Checking code coverage",
"phpstan": "Run static analyze",
"phpcs": "Checking codestyle"
"coverage-html": "Generate code coverage report"
}
}

0 comments on commit 8f16aaa

Please sign in to comment.