You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Class-transformer to transform your data into a typed object
3
+
> Alas, I do not speak English, and the documentation was compiled through google translator :(
4
+
> I will be glad if you can help me describe the documentation more correctly :)
5
+
6
+
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?
7
+
8
+
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.
9
+
10
+
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.
11
+
12
+
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.
4
13
5
14
## :scroll:**Installation**
6
15
The package can be installed via composer:
7
16
```
17
+
for PHP ^8.0
8
18
composer require yzen.dev/plain-to-class
19
+
20
+
for PHP ^7.4
21
+
composer require yzen.dev/plain-to-class:php7
9
22
```
10
23
11
24
## :scroll:**Usage**
12
-
> Alas, i do not speak English, and the documentation is compiled via google translate :(
13
-
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.
15
-
Therefore, I am trying to implement all business services through DTO.
25
+
Common use case:
16
26
17
-
This approach makes sure that the service will work with the data it needs,full typing, there will be no need to check the existence of keys if it is an array.
18
-
19
-
Suppose you have a service for authorization. To ensure the reliability of the input data, it would be good to send DTO there:
20
-
Let's say you have an authorization service. To ensure that the input is valid, it would be nice to send a DTO there. But then we will have to initiate and fill in each time ourselves, and you must admit that this is very inconvenient.
21
-
For this, this helper was created, which will allow you to easily create an instance of a DTO, or just any of your objects.
22
27
```php
23
-
class AuthController
28
+
namespace DTO;
29
+
30
+
class CreateUserDTO
24
31
{
25
-
private AuthService $authService;
26
-
27
-
//...
28
-
29
-
public function register(RegisterUserRequest $request)
public string 'password' => string '123456' (length=6)
47
+
object(\LoginDTO)
48
+
'email' => string(13) "test@mail.com"
49
+
'balance' => float(128.41)
52
50
```
53
51
54
-
If you need to implement your own transformation, for example, your input parameters have a different name for attributes, then you can implement the static of the transform method in the class.
If the property is not of a scalar type, but a class of another DTO is allowed, it will also be automatically converted.
57
62
58
-
If you have an array of objects of a specific class, then you must specify the full path to the class in phpdoc `array <\ DTO \ ProductDTO>`.
63
+
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.
59
64
60
-
This is done in order to know exactly which instance you need to create. Since Reflection does not provide out-of-the-box functionality to get the `use *` file. In addition to `use *`, an alias can be specified, and it will be more difficult to trace it.
65
+
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.
61
66
Example:
62
67
```php
63
-
namespace DTO;
64
68
65
69
class ProductDTO
66
70
{
67
71
public int $id;
68
72
public string $name;
69
73
}
70
-
```
71
-
```php
72
-
namespace DTO;
74
+
73
75
74
76
class UserDTO
75
77
{
76
78
public int $id;
77
79
public string $email;
78
80
public string $balance;
79
81
}
80
-
```
81
82
82
-
```php
83
+
83
84
class PurchaseDTO
84
85
{
85
-
/** @var array<\DTO\ProductDTO> $products Product list */
public string 'email' => string 'test@test.com' (length=13)
121
120
public float 'balance' => float 10012.23
122
121
```
122
+
123
+
### :scroll:**Writing style**
124
+
125
+
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:
0 commit comments