Skip to content

Commit 8146920

Browse files
authored
Merge pull request #2 from yzen-dev/dev
Release 1.0
2 parents bb48f3c + dae079b commit 8146920

24 files changed

+783
-266
lines changed

.github/workflows/php.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: PHP Composer
22

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

99
jobs:
1010
build:
@@ -18,7 +18,7 @@ jobs:
1818
- name: PHP setup
1919
uses: shivammathur/setup-php@v2
2020
with:
21-
php-version: 7.4
21+
php-version: 8
2222

2323
- name: Validate composer.json and composer.lock
2424
run: composer validate

README.md

+75-45
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,104 @@
11
## Class-transformer helper
22

3-
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.
413

514
## :scroll: **Installation**
615
The package can be installed via composer:
716
```
17+
for PHP ^8.0
818
composer require yzen.dev/plain-to-class
19+
20+
for PHP ^7.4
21+
composer require yzen.dev/plain-to-class:php7
922
```
1023

1124
## :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:
1626

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.
2227
```php
23-
class AuthController
28+
namespace DTO;
29+
30+
class CreateUserDTO
2431
{
25-
private AuthService $authService;
26-
27-
//...
28-
29-
public function register(RegisterUserRequest $request)
30-
{
31-
$registerDTO = ClassTransformer::transform(RegisterDTO::class, $request->toArray());
32-
$this->authService->register($registerDTO);
33-
}
32+
public string $email;
33+
public float $balance;
34+
}
3435
```
3536

36-
If the data contains arguments that are not in the class, they will simply be skipped.
37-
3837
```php
3938
$data = [
40-
'email' => 'test',
41-
'password' => '123456',
42-
'fakeField' => 'fake',
39+
'email' => 'test@mail.com',
40+
'balance' => 128.41,
4341
];
44-
$dto = ClassTransformer::transform(LoginDTO::class,$data);
42+
$dto = ClassTransformer::transform(CreateUserDTO::class,$data);
4543
var_dump($dto);
4644
```
4745
Result:
4846
```php
49-
object(\LoginDTO)[298]
50-
public string 'email' => string 'test' (length=4)
51-
public string 'password' => string '123456' (length=6)
47+
object(\LoginDTO)
48+
'email' => string(13) "test@mail.com"
49+
'balance' => float(128.41)
5250
```
5351

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.
52+
Also for php 8 you can pass named arguments:
53+
54+
```php
55+
$dto = ClassTransformer::transform(CreateUserDTO::class,
56+
email: 'test@mail.com',
57+
balance: 128.41
58+
);
59+
```
5560

56-
## :scroll: **Recursive casting**
61+
If the property is not of a scalar type, but a class of another DTO is allowed, it will also be automatically converted.
5762

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.
5964

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.
6166
Example:
6267
```php
63-
namespace DTO;
6468

6569
class ProductDTO
6670
{
6771
public int $id;
6872
public string $name;
6973
}
70-
```
71-
```php
72-
namespace DTO;
74+
7375

7476
class UserDTO
7577
{
7678
public int $id;
7779
public string $email;
7880
public string $balance;
7981
}
80-
```
8182

82-
```php
83+
8384
class PurchaseDTO
8485
{
85-
/** @var array<\DTO\ProductDTO> $products Product list */
86+
#[ConvertArray(ProductDTO::class)]
8687
public array $products;
8788

88-
/** @var \DTO\UserDTO $user */
89+
/** @var UserDTO $user */
8990
public UserDTO $user;
9091
}
91-
```
9292

93-
```php
9493
$data = [
9594
'products' => [
9695
['id' => 1, 'name' => 'phone',],
9796
['id' => 2, 'name' => 'bread',],
9897
],
9998
'user' => ['id' => 1, 'email' => 'test@test.com', 'balance' => 10012.23,],
10099
];
101-
$purcheseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
102-
var_dump($purcheseDTO);
100+
$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
101+
var_dump($purchaseDTO);
103102
```
104103

105104
```php
@@ -120,3 +119,34 @@ object(PurchaseDTO)[345]
120119
public string 'email' => string 'test@test.com' (length=13)
121120
public float 'balance' => float 10012.23
122121
```
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:
126+
127+
```php
128+
class WritingStyleSnakeCaseDTO
129+
{
130+
#[WritingStyle(WritingStyle::STYLE_CAMEL_CASE, WritingStyle::STYLE_SNAKE_CASE)]
131+
public string $contact_fio;
132+
133+
#[WritingStyle(WritingStyle::STYLE_CAMEL_CASE)]
134+
public string $contact_email;
135+
}
136+
137+
138+
$data = [
139+
'contactFio' => 'yzen.dev',
140+
'contactEmail' => 'test@mail.com',
141+
];
142+
$model = ClassTransformer::transform(WritingStyleSnakeCaseDTO::class, $data);
143+
var_dump($model);
144+
```
145+
```php
146+
RESULT:
147+
148+
object(WritingStyleSnakeCaseDTO) (2) {
149+
["contact_fio"]=> string(8) "yzen.dev"
150+
["contact_email"]=> string(13) "test@mail.com"
151+
}
152+
```

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "yzen.dev/plain-to-class",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "Class-transformer to transform your data into a typed object",
55
"minimum-stability": "dev",
66
"prefer-stable": true,
77
"license": "MIT",
8-
"keywords": [ "php", "object", "class", "transformer", "class-transformer"],
8+
"keywords": [ "php", "object", "class", "transformer","convert", "class-transformer"],
99
"authors": [
1010
{
1111
"name": "Andey Iatsenko",
@@ -26,7 +26,7 @@
2626
}
2727
},
2828
"require": {
29-
"php": "^7.4 || ^8.0"
29+
"php": "^8.0"
3030
},
3131
"scripts": {
3232
"phpunit": [

0 commit comments

Comments
 (0)