Skip to content

Commit 8f16aaa

Browse files
authored
Merge pull request #8 from yzen-dev/feature/v3.0.0-dev
Release v3.0.0
2 parents 7d17522 + 59d96ea commit 8f16aaa

File tree

103 files changed

+5957
-1649
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+5957
-1649
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ tests/ export-ignore
22
phpunit.xml export-ignore
33
phpbench.json export-ignore
44
phpstan.xml export-ignore
5+
psalm.xml export-ignore
56
coverage.xml export-ignore
67
phpstan.neon export-ignore

.github/workflows/cs.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Coding Standards
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
jobs:
8+
coding-standards:
9+
name: Coding Standards
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v3
15+
16+
- name: Setup PHP
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php-version: 8.1
20+
21+
- name: Install dependencies
22+
if: steps.composer-cache.outputs.cache-hit != 'true'
23+
run: composer install --prefer-dist --no-progress --no-suggest
24+
25+
- name: Check codestyle
26+
run: composer run-script phpcs

.github/workflows/mutation.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: mutation test
2+
3+
on:
4+
push:
5+
6+
pull_request:
7+
8+
jobs:
9+
mutation:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
# Setup PHP versions, run checks
16+
- name: PHP setup
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php: >-
20+
['8.2']
21+
22+
- name: Cache Composer packages
23+
id: composer-cache
24+
uses: actions/cache@v2
25+
with:
26+
path: vendor
27+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
28+
restore-keys: |
29+
${{ runner.os }}-php-
30+
31+
- name: Install dependencies
32+
if: steps.composer-cache.outputs.cache-hit != 'true'
33+
run: composer install --prefer-dist --no-progress --no-suggest
34+
35+
- name: Run mutation
36+
run: ./vendor/bin/infection
37+
env:
38+
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
name: Code analyze
1+
name: psalm
22

33
on:
44
push:
5-
branches: [ master, dev ]
5+
66
pull_request:
7-
branches: [ master, dev ]
87

98
jobs:
10-
build:
9+
psalm:
1110

1211
runs-on: ubuntu-latest
1312

@@ -18,7 +17,8 @@ jobs:
1817
- name: PHP setup
1918
uses: shivammathur/setup-php@v2
2019
with:
21-
php-version: 8.0
20+
php: >-
21+
['8.0', '8.1', '8.2']
2222
2323
- name: Cache Composer packages
2424
id: composer-cache
@@ -32,9 +32,6 @@ jobs:
3232
if: steps.composer-cache.outputs.cache-hit != 'true'
3333
run: composer install --prefer-dist --no-progress --no-suggest
3434

35-
- name: Run phpstan
36-
run: composer run-script phpstan
37-
38-
- name: Check codestyle
39-
run: composer run-script phpcs
35+
- name: Static analysis.
36+
run: vendor/bin/psalm --shepherd --stats --output-format=github --php-version=${{ matrix.php }}
4037

.github/workflows/tests.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ name: Run tests
22

33
on:
44
push:
5-
branches: [ master, dev, feature/cache-v2-php8.1 ]
5+
66
pull_request:
7-
branches: [ master, dev ]
87

98
jobs:
109
tests:
@@ -13,8 +12,7 @@ jobs:
1312
strategy:
1413
fail-fast: false
1514
matrix:
16-
php: ["8.1", "8.0"]
17-
dependency-version: [prefer-lowest, prefer-stable]
15+
php: ["8.2", "8.1"]
1816
name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
1917

2018
steps:

.gitignore

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
.DS_Store
21
.idea
3-
vendor
4-
.phpunit.result.cache
5-
.phpunit.cache/
2+
.DS_Store
3+
infection.json
4+
5+
/.cache
6+
/.tmp
7+
/vendor

README.RU.md

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
![Packagist Downloads](https://img.shields.io/packagist/dm/yzen.dev/plain-to-class)
88
![Packagist Downloads](https://img.shields.io/packagist/dt/yzen.dev/plain-to-class)
99

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

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

README.md

+27-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
![Packagist Downloads](https://img.shields.io/packagist/dm/yzen.dev/plain-to-class)
88
![Packagist Downloads](https://img.shields.io/packagist/dt/yzen.dev/plain-to-class)
99

10+
[![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)
11+
[![type-coverage](https://shepherd.dev/github/yzen-dev/plain-to-class/coverage.svg)](https://shepherd.dev/github/yzen-dev/plain-to-class)
12+
[![psalm-level](https://shepherd.dev/github/yzen-dev/plain-to-class/level.svg)](https://shepherd.dev/github/yzen-dev/plain-to-class)
13+
14+
1015
> 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 :)
1116
1217
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.
@@ -33,6 +38,7 @@ This is where this package comes to the rescue, which takes care of all the work
3338
- [After Transform](#after-transform)
3439
- [Custom transform](#custom-transform)
3540
- [Comparison](#Comparison)
41+
- [Cache](#Cache)
3642

3743
## **Installation**
3844

@@ -68,7 +74,10 @@ $data = [
6874
'email' => 'test@mail.com',
6975
'balance' => 128.41,
7076
];
71-
$dto = ClassTransformer::transform(CreateUserDTO::class, $data);
77+
78+
$dto = (new Hydrator())->create(CreateUserDTO::class, $data);
79+
// or static init
80+
$dto = Hydrator::init()->create(CreateUserDTO::class, $data);
7281
var_dump($dto);
7382
```
7483

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

8594
```php
86-
$dto = ClassTransformer::transform(CreateUserDTO::class,
95+
$dto = Hydrator::init()->create(CreateUserDTO::class,
8796
email: 'test@mail.com',
8897
balance: 128.41
8998
);
@@ -109,7 +118,7 @@ $data = [
109118
'cost' => 10012.23,
110119
];
111120

112-
$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
121+
$purchaseDTO = Hydrator::init()->create(PurchaseDTO::class, $data);
113122
var_dump($purchaseDTO);
114123
```
115124

@@ -153,7 +162,7 @@ $data = [
153162
['id' => 2, 'name' => 'bread',],
154163
],
155164
];
156-
$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
165+
$purchaseDTO = Hydrator::init()->create(PurchaseDTO::class, $data);
157166
```
158167

159168
### **Anonymous array**
@@ -166,7 +175,7 @@ $data = [
166175
['id' => 1, 'name' => 'phone'],
167176
['id' => 2, 'name' => 'bread'],
168177
];
169-
$products = ClassTransformer::transformCollection(ProductDTO::class, $data);
178+
$products = Hydrator::init()->createCollection(ProductDTO::class, $data);
170179
```
171180

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

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

204213
[$user, $purchase] = $result;
205214
var_dump($user);
@@ -257,7 +266,7 @@ class WritingStyleSnakeCaseDTO
257266
'contactFio' => 'yzen.dev',
258267
'contactEmail' => 'test@mail.com',
259268
];
260-
$model = ClassTransformer::transform(WritingStyleSnakeCaseDTO::class, $data);
269+
$model = Hydrator::init()->create(WritingStyleSnakeCaseDTO::class, $data);
261270
var_dump($model);
262271
```
263272

@@ -340,6 +349,17 @@ class CustomTransformUserDTOArray
340349
}
341350
```
342351

352+
### **Cache**
353+
354+
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.
355+
356+
You can enable caching by passing the config to the hydrator constructor:
357+
358+
```php
359+
(new Hydrator(new HydratorConfig(true)))
360+
->create(PurchaseDto::class, $data);
361+
```
362+
343363
### Comparison
344364
I also made a comparison with current analogues and here are the main disadvantages
345365
- Works only for a specific framework

bin/plain-to-class-clear

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
function deleteDir($dirPath)
5+
{
6+
if (!str_ends_with($dirPath, '/')) {
7+
$dirPath .= '/';
8+
}
9+
$files = glob($dirPath . '*', GLOB_MARK);
10+
11+
foreach ($files as $file) {
12+
echo 'Remove' . $file . PHP_EOL;
13+
if (is_dir($file)) {
14+
deleteDir($file);
15+
} else {
16+
unlink($file);
17+
}
18+
}
19+
rmdir($dirPath);
20+
}
21+
22+
if (file_exists(__DIR__ . '/../.cache')) {
23+
deleteDir(__DIR__ . '/../.cache');
24+
} elseif (file_exists(__DIR__ . '/../vendor/yzen.dev/plain-to-class/.cache')) {
25+
deleteDir(__DIR__ . '/../vendor/yzen.dev/plain-to-class/.cache');
26+
}
27+
exit(0);

bin/plain-to-class-clear.bat

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@echo off
2+
3+
if "%PHP_PEAR_PHP_BIN%" neq "" (
4+
set PHPBIN=%PHP_PEAR_PHP_BIN%
5+
) else set PHPBIN=php
6+
7+
"%PHPBIN%" "%~dp0\plain-to-class-clear" %*

composer.json

+27-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "yzen.dev/plain-to-class",
3-
"version": "2.0",
3+
"version": "3.0",
44
"description": "Class-transformer to transform your dataset into a structured object",
55
"minimum-stability": "dev",
66
"prefer-stable": true,
@@ -17,7 +17,10 @@
1717
"source": "https://github.com/yzen-dev/plain-to-class"
1818
},
1919
"config": {
20-
"sort-packages": true
20+
"sort-packages": true,
21+
"allow-plugins": {
22+
"infection/extension-installer": true
23+
}
2124
},
2225
"autoload": {
2326
"psr-4": {
@@ -30,39 +33,37 @@
3033
}
3134
},
3235
"require": {
33-
"php": "^8.0"
36+
"php": "^8.1"
3437
},
3538
"require-dev": {
3639
"mockery/mockery": "1.5.1",
3740
"phpbench/phpbench": "1.2.8",
38-
"phpstan/phpstan": "1.9.12",
39-
"phpunit/phpunit": "9.5.28",
40-
"squizlabs/php_codesniffer": "3.7.1"
41+
"phpunit/phpunit": "10.2.1",
42+
"roave/infection-static-analysis-plugin": "1.32",
43+
"squizlabs/php_codesniffer": "3.7.1",
44+
"symfony/var-dumper": "6.3",
45+
"vimeo/psalm": "5.12.0"
4146
},
47+
"bin": [
48+
"bin/plain-to-class-clear"
49+
],
4250
"scripts": {
43-
"phpunit": [
44-
"./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml"
45-
],
46-
"phpstan": [
47-
"./vendor/bin/phpstan analyse -c ./phpstan.neon src"
48-
],
49-
"phpcs": [
50-
"./vendor/bin/phpcs --standard=./phpcs.xml -n --no-cache -s"
51-
],
52-
"phpcbf": [
53-
"./vendor/bin/phpcbf --standard=./phpcs.xml -n --no-cache -s"
54-
],
55-
"coverage": [
56-
"./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --coverage-text --colors=never --coverage-clover coverage.xml"
57-
],
58-
"coverage-html": [
59-
"./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --coverage-html public/coverage --coverage-text"
60-
]
51+
"phpunit": "./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml",
52+
"infection": "./vendor/bin/roave-infection-static-analysis-plugin",
53+
"infection-html": "./vendor/bin/roave-infection-static-analysis-plugin --logger-html='.tmp/mutation-report.html'",
54+
"psalm": "./vendor/bin/psalm",
55+
"phpcs": "./vendor/bin/phpcs --standard=./phpcs.xml -n --no-cache -s",
56+
"phpcbf": "./vendor/bin/phpcbf --standard=./phpcs.xml -n --no-cache -s",
57+
"coverage": "./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --coverage-text --colors=never --coverage-clover coverage.xml",
58+
"coverage-html": "./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --coverage-html public/coverage --coverage-text"
6159
},
6260
"scripts-descriptions": {
6361
"phpunit": "Run tests",
62+
"infection": "Run mutation tests",
63+
"infection-html": "Generate mutation tests report",
64+
"psalm": "Run static analyze",
65+
"phpcs": "Checking codestyle",
6466
"coverage": "Checking code coverage",
65-
"phpstan": "Run static analyze",
66-
"phpcs": "Checking codestyle"
67+
"coverage-html": "Generate code coverage report"
6768
}
6869
}

0 commit comments

Comments
 (0)