Skip to content

Commit 07326a2

Browse files
author
Яценко Андрей
committed
Add: Anonymous array
1 parent 54b717c commit 07326a2

12 files changed

+399
-91
lines changed

.editorconfig

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
trim_trailing_whitespace = false
9+
10+
[*.{php, inc}]
11+
indent_size = 4
12+
13+
[*.md]
14+
trim_trailing_whitespace = false
15+
16+
[*.{yml,yaml}]
17+
indent_size = 2

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ vendor
55
.phpunit.cache/code-coverage/
66

77
.phpunit.cache/
8+
9+
.DS_Store

README.md

+113-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## Class-transformer helper
2+
23
![Packagist Version](https://img.shields.io/packagist/v/yzen.dev/plain-to-class?color=blue&label=version)
34
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/yzen-dev/plain-to-class/Run%20tests?label=tests&logo=github)
45
[![Coverage](https://codecov.io/gh/yzen-dev/plain-to-class/branch/master/graph/badge.svg?token=QAO8STLPMI)](https://codecov.io/gh/yzen-dev/plain-to-class)
@@ -9,23 +10,34 @@
910
> Alas, I do not speak English, and the documentation was compiled through google translator :(
1011
> I will be glad if you can help me describe the documentation more correctly :)
1112
12-
This package will help you transform any dataset into a structured object. This is very convenient when values obtained from a query, database, or any other place can be easily cast to the object you need. But what exactly is this convenient?
13+
This package will help you transform any dataset into a structured object. This is very convenient when values obtained
14+
from a query, database, or any other place can be easily cast to the object you need. But what exactly is this
15+
convenient?
1316

14-
When writing code, it is very important to separate logic, adhere to the principle of single responsibility, reduce dependence on other services, and much more.
17+
When writing code, it is very important to separate logic, adhere to the principle of single responsibility, reduce
18+
dependence on other services, and much more.
1519

16-
When creating a new service to create a user, you only need the necessary data set - name, email and phone. Why do you need to check around separately arrays, separately objects, check for the presence of keys through isset. It is much more convenient to make a DTO model with which the service will already work.
20+
When creating a new service to create a user, you only need the necessary data set - name, email and phone. Why do you
21+
need to check around separately arrays, separately objects, check for the presence of keys through isset. It is much
22+
more convenient to make a DTO model with which the service will already work.
1723

18-
This approach guarantees that the service will work with the data it needs, full typing, there is no need to check for the presence of keys if it is an array.
24+
This approach guarantees that the service will work with the data it needs, full typing, there is no need to check for
25+
the presence of keys if it is an array.
1926

2027
## :scroll: **Installation**
28+
2129
The package can be installed via composer:
30+
2231
```
2332
composer require yzen.dev/plain-to-class
2433
```
2534

2635
## :scroll: **Usage**
36+
2737
Common use case:
2838

39+
### :scroll: **Base**
40+
2941
```php
3042
namespace DTO;
3143

@@ -44,7 +56,9 @@ $data = [
4456
$dto = ClassTransformer::transform(CreateUserDTO::class,$data);
4557
var_dump($dto);
4658
```
59+
4760
Result:
61+
4862
```php
4963
object(\LoginDTO)
5064
'email' => string(13) "test@mail.com"
@@ -62,10 +76,16 @@ $dto = ClassTransformer::transform(CreateUserDTO::class,
6276

6377
If the property is not of a scalar type, but a class of another DTO is allowed, it will also be automatically converted.
6478

65-
If you have an array of objects of a certain class, then you must specify the ConvertArray attribute to it, passing it to which class you need to cast the elements.
79+
### :scroll: **Collection**
80+
81+
If you have an array of objects of a certain class, then you must specify the ConvertArray attribute to it, passing it
82+
to which class you need to cast the elements.
83+
84+
It is also possible to specify the class in PHP DOC, but then you need to write the full path to this
85+
class `array <\ DTO \ ProductDTO>`. This is done in order to know exactly which instance you need to create. Since
86+
Reflection does not provide out-of-the-box functions for getting the `use *` file. Besides `use *`, you can specify an
87+
alias, and it will be more difficult to trace it. Example:
6688

67-
It is also possible to specify the class in PHP DOC, but then you need to write the full path to this class `array <\ DTO \ ProductDTO>`. This is done in order to know exactly which instance you need to create. Since Reflection does not provide out-of-the-box functions for getting the `use *` file. Besides `use *`, you can specify an alias, and it will be more difficult to trace it.
68-
Example:
6989
```php
7090

7191
class ProductDTO
@@ -122,9 +142,93 @@ object(PurchaseDTO)[345]
122142
public float 'balance' => float 10012.23
123143
```
124144

145+
### :scroll: **Anonymous array**
146+
147+
In case you need to convert an array of data into an array of class objects, you can implement this through an anonymous
148+
array. To do this, you just need to wrap the class in **[]**
149+
150+
```php
151+
$data = [
152+
['id' => 1, 'name' => 'phone'],
153+
['id' => 2, 'name' => 'bread'],
154+
];
155+
$products = ClassTransformer::transform([ProductDTO::class], $data);
156+
```
157+
158+
The result of this execution you will get an array of objects ProductDTO
159+
160+
```php
161+
array(2) {
162+
[0]=>
163+
object(ProductDTO) {
164+
["id"]=> int(1)
165+
["name"]=> string(5) "phone"
166+
}
167+
[1]=>
168+
object(ProductDTO) {
169+
["id"]=> int(2)
170+
["name"]=> string(5) "bread"
171+
}
172+
}
173+
```
174+
175+
You may also need an element-by-element array transformation. In such a case, you can pass an array of classes, which
176+
can then be easily unpacked
177+
178+
```php
179+
$userData = ['id' => 1, 'email' => 'test@test.com', 'balance' => 10012.23];
180+
$purchaseData = [
181+
'products' => [
182+
['id' => 1, 'name' => 'phone',],
183+
['id' => 2, 'name' => 'bread',],
184+
],
185+
'user' => ['id' => 3, 'email' => 'fake@mail.com', 'balance' => 10012.23,],
186+
];
187+
188+
$result = ClassTransformer::transform([UserDTO::class, PurchaseDTO::class], [$userData, $purchaseData]);
189+
190+
[$user, $purchase] = $result;
191+
var_dump($user);
192+
var_dump($purchase);
193+
```
194+
195+
Result:
196+
197+
```php
198+
object(UserDTO) (3) {
199+
["id"] => int(1)
200+
["email"]=> string(13) "test@test.com"
201+
["balance"]=> float(10012.23)
202+
}
203+
204+
object(PurchaseDTO) (2) {
205+
["products"]=>
206+
array(2) {
207+
[0]=>
208+
object(ProductDTO)#349 (3) {
209+
["id"]=> int(1)
210+
["name"]=> string(5) "phone"
211+
}
212+
[1]=>
213+
object(ProductDTO)#348 (3) {
214+
["id"]=> int(2)
215+
["name"]=> string(5) "bread"
216+
}
217+
}
218+
["user"]=>
219+
object(UserDTO)#332 (3) {
220+
["id"]=> int(3)
221+
["email"]=> string(13) "fake@mail.com"
222+
["balance"]=> float(10012.23)
223+
}
224+
}
225+
```
226+
125227
### :scroll: **Writing style**
126228

127-
A constant problem with the style of writing, for example, in the database it is snake_case, and in the camelCase code. And they constantly need to be transformed somehow. The package takes care of this, you just need to specify the WritingStyle attribute on the property:
229+
A constant problem with the style of writing, for example, in the database it is snake_case, and in the camelCase code.
230+
And they constantly need to be transformed somehow. The package takes care of this, you just need to specify the
231+
WritingStyle attribute on the property:
128232

129233
```php
130234
class WritingStyleSnakeCaseDTO
@@ -144,6 +248,7 @@ class WritingStyleSnakeCaseDTO
144248
$model = ClassTransformer::transform(WritingStyleSnakeCaseDTO::class, $data);
145249
var_dump($model);
146250
```
251+
147252
```php
148253
RESULT:
149254

composer.json

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "yzen.dev/plain-to-class",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Class-transformer to transform your dataset into a structured object",
55
"minimum-stability": "dev",
66
"prefer-stable": true,
@@ -12,6 +12,10 @@
1212
"email": "yzen.dev@gmail.com"
1313
}
1414
],
15+
"support": {
16+
"issues": "https://github.com/yzen-dev/plain-to-class/issues",
17+
"source": "https://github.com/yzen-dev/plain-to-class"
18+
},
1519
"config": {
1620
"sort-packages": true
1721
},
@@ -28,6 +32,12 @@
2832
"require": {
2933
"php": "^8.0"
3034
},
35+
"require-dev": {
36+
"phpstan/phpstan": "^0.12.82",
37+
"mockery/mockery": "^1.0",
38+
"phpunit/phpunit": "^9.3",
39+
"squizlabs/php_codesniffer": "*"
40+
},
3141
"scripts": {
3242
"phpunit": [
3343
"./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml"
@@ -45,14 +55,10 @@
4555
"./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --coverage-html public/coverage --coverage-text"
4656
]
4757
},
48-
"require-dev": {
49-
"phpstan/phpstan": "^0.12.82",
50-
"mockery/mockery": "^1.0",
51-
"phpunit/phpunit": "^9.3",
52-
"squizlabs/php_codesniffer": "*"
53-
},
54-
"support": {
55-
"issues": "https://github.com/yzen-dev/plain-to-class/issues",
56-
"source": "https://github.com/yzen-dev/plain-to-class"
58+
"scripts-descriptions": {
59+
"phpunit": "Run tests",
60+
"coverage": "Checking code coverage",
61+
"phpstan": "Run static analyze",
62+
"phpcs": "Checking codestyle"
5763
}
5864
}

0 commit comments

Comments
 (0)