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
> CLI tool for PHP Data Transfer Objects generation.
7
7
8
-
This utility gives an ability to generate PHP 7.4 DTO classes based on json pattern (schema).
8
+
> A CLI tool for PHP Data Transfer Objects generation.
9
+
10
+
This utility gives an ability to generate PHP 8 DTO classes based on json pattern.
11
+
9
12
## Installation
10
-
Install the package via composer:
11
13
12
-
`composer require --dev codebot/phpdto 0.2.*`
14
+
Install the package via composer:
15
+
16
+
`composer require --dev codebot/phpdto 0.3.*`
13
17
14
-
**Please consider that the minimum PHP version required by this package is 7.4.**
18
+
**Please consider that the minimum PHP version required by this package is 8.0.**
15
19
16
20
## Initialization
21
+
17
22
`vendor/bin/phpdto init`
18
23
19
-
The current working directory is the directory from where you are invoking `phpdto` command.
24
+
The current working directory is the directory from where you are invoking `phpdto` command.
20
25
21
-
This command will initialize the phpdto and create **phpdto.json** configuration file and **phpdto_patterns** directory in your current working directory.
26
+
This command will initialize the phpdto and create **phpdto.json** configuration file and **phpdto_patterns** directory
27
+
in your current working directory.
22
28
23
29
## Configuration
24
-
**phpdto.json** configuration file contains following variables:
25
30
26
-
*PHP_DTO_PATTERNS_DIR* - the directory, where you must store json schemas for DTOs.
31
+
**phpdto.json** configuration file contains following variables:
32
+
33
+
*PHP_DTO_PATTERNS_DIR* - the directory, where you must store json patterns for DTOs.
34
+
35
+
*PHP_DTO_NAMESPACE* - the namespace of generated DTO classes.
27
36
28
-
*PHP_DTO_NAMESPACE* - the namespace for DTO classes.
29
-
30
-
*PHP_DTO_CLASS_POSTFIX* - postfix of DTO classes, e.g. Item (without postfix), ItemDto (postfix is "Dto").
37
+
*PHP_DTO_CLASS_POSTFIX* - postfix of DTO classes, e.g. Item (no postfix), ItemDto (the postfix is "Dto").
31
38
32
39
***These variables are stored as environment variables.***
33
40
34
41
## Usage
35
-
To generate DTO class you must create a pattern, which is a json file that contains information about generated class.
36
42
37
-
##### DTO Pattern
43
+
To generate DTO class you must create a pattern, which is a json file that contains information about the generated class.
38
44
39
-
An example of DTO pattern:
45
+
##### DTO JSON Pattern
46
+
47
+
An example of DTO pattern:
40
48
41
49
```json
42
50
{
43
-
"class": "item",
44
-
"namespace_postfix": "",
45
-
"rules": {
46
-
"id": "int",
47
-
"count": "nullable|int",
48
-
"name": "string",
49
-
"description": "nullable|string",
50
-
"is_active": "bool"
51
-
}
51
+
"class": "item",
52
+
"namespace_postfix": "",
53
+
"props": {
54
+
"id": "int",
55
+
"count": "?int",
56
+
"name": "string",
57
+
"description": "?string",
58
+
"is_active": "bool"
59
+
}
52
60
}
53
61
```
54
62
55
-
**class**
63
+
**class**
64
+
65
+
A class name that will be combined with *PHP_DTO_CLASS_POSTFIX* specified in `phpdto.json` config file.
56
66
57
-
The class name that will be combined with *PHP_DTO_CLASS_POSTFIX* specified in phpdto.json config file.
67
+
So, if the class name is `item`, and the class postfix config value is `Dto`, then the generated class name will be `
68
+
ItemDto`.
58
69
59
-
So, if the class name is "item" and the class postfix config value is "Dto", then the generated class name will be "ItemDto".
60
-
61
-
**namespace_postfix**
70
+
**namespace_postfix**
62
71
63
-
The postfix of the generated DTO class namespace that will be combined with *PHP_DTO_NAMESPACE* specified in phpdto.json config file.
72
+
A postfix of the generated DTO class namespace that will be combined with *PHP_DTO_NAMESPACE* specified in `phpdto.json`
73
+
config file.
64
74
65
-
So, if the namespace postfix is **"\User"** and the default DTO namespace is **"App\Dto"**, then the generated class namespace will be `App\Dto\User`.
75
+
So, if the namespace postfix is `\User`, and the default DTO namespace is `App\Dto`, then the namespace of the generated
76
+
class will be `App\Dto\User`.
66
77
67
-
You can leave namespace postfix empty.
78
+
You can leave namespace postfix empty.
68
79
69
-
**rules**
80
+
**props**
70
81
71
-
This object contains information about DTO class properties and methods. Keys will be casted to class properties. Values contain information about getters return types.
82
+
This object contains information about DTO class properties and methods. Keys will be cast to class properties. Values
83
+
contain information about getters return types.
72
84
73
-
`"description" : "nullable|string"` - due to this pair `$_description` property will be added to DTO class with appropriate `private ?string $_description` property and `getDescription(): ?string` method, that expects return type "string" and allows nullable.
85
+
`"description" : "?string"` - due to this pair `$_description` property will be added to DTO class with
86
+
`private ?string $_description` property and `getDescription(): ?string` method, that expects return type
87
+
"string" and allows null.
74
88
75
89
##### Generating DTO
76
90
77
91
Given you have already created pattern as json file named `item.json` in the `phpdto_patterns` folder.
78
92
79
-
Run `vendor/bin/phpdto -f=item` to have your DTO class generated. It will be stored under namespace specified in the `phpdto.json` config file combined with namespace postfix specified in your pattern.
93
+
Run `vendor/bin/phpdto -f=item` to have your DTO class generated. It will be stored under namespace specified in
94
+
the `phpdto.json` config file combined with namespace postfix specified in your pattern.
80
95
81
-
Given your are generating DTO class from the pattern shown in "DTO Pattern" section, then you will have following class generated.
96
+
Given you are generating DTO class from the pattern shown in "DTO JSON Pattern" section, then you will have following class
97
+
generated.
82
98
83
99
```php
84
100
<?php
85
101
86
102
namespace App\Dto;
87
103
88
-
class ItemDto extends \PhpDto\Dto
104
+
use PhpDto\Dto;
105
+
use PhpDto\DtoSerialize;
106
+
use PhpDto\ToArray;
107
+
108
+
class ItemDto extends Dto
89
109
{
90
-
use \PhpDto\DtoSerialize;
110
+
use DtoSerialize, ToArray;
91
111
92
112
private int $_id;
93
113
private ?int $_count;
@@ -97,53 +117,54 @@ class ItemDto extends \PhpDto\Dto
97
117
98
118
public function __construct( array $item )
99
119
{
100
-
$this->_id = $item['id'];
101
-
$this->_count = $item['count'];
102
-
$this->_name = $item['name'];
103
-
$this->_description = $item['description'];
104
-
$this->_isActive = $item['is_active'];
120
+
$this->_id = $item['id'];
121
+
$this->_count = $item['count'];
122
+
$this->_name = $item['name'];
123
+
$this->_description = $item['description'];
124
+
$this->_isActive = $item['is_active'];
105
125
}
106
126
107
127
public function getId(): int
108
128
{
109
-
return $this->_id;
129
+
return $this->_id;
110
130
}
111
131
112
132
public function getCount(): ?int
113
133
{
114
-
return $this->_count;
134
+
return $this->_count;
115
135
}
116
136
117
137
public function getName(): string
118
138
{
119
-
return $this->_name;
139
+
return $this->_name;
120
140
}
121
141
122
142
public function getDescription(): ?string
123
143
{
124
-
return $this->_description;
144
+
return $this->_description;
125
145
}
126
146
127
147
public function getIsActive(): bool
128
148
{
129
-
return $this->_isActive;
149
+
return $this->_isActive;
130
150
}
131
151
}
132
152
```
133
153
134
154
##### How to use
135
155
136
-
There are 2 mapper methods:
156
+
There are 2 mapper methods:
137
157
138
-
`static function mapArray(array $items, bool $shouldSerialize = false): array`
158
+
`static function mapArray(array $items, bool $shouldSerialize = false): array`
139
159
140
-
`static function mapSingle(array $item, bool $shouldSerialize = false): mixed`
160
+
`static function mapSingle(array $item, bool $shouldSerialize = false): Dto|stdClass`
141
161
142
162
Use `mapArray` when you need to map multidimensional array, otherwise use `mapSingle`.
143
163
144
-
Mapping example:
164
+
Mapping example:
165
+
166
+
###### Single
145
167
146
-
Given you have following `$itemData` array that contains *all* data related to item that is not necessary to pass another layer (usually it's a view).
147
168
```php
148
169
$itemData = [
149
170
'id' => 1,
@@ -169,7 +190,7 @@ $item->getCount();
169
190
$item->getIsActive();
170
191
```
171
192
172
-
Due to the mapper above we build our DTO that contains data we need.
193
+
###### Multidimensional
173
194
174
195
```php
175
196
$itemData = [
@@ -197,11 +218,12 @@ foreach( $items as $item )
197
218
}
198
219
```
199
220
200
-
***CONSIDER REFACTORING THE CONSTRUCTOR OF GENERATED DTO CLASS DEPENDING ON THE DATA STRUCTURE OF THE ARRAY DATA YOU WANT TO MAP.***
221
+
***CONSIDER REFACTORING THE CONSTRUCTOR OF GENERATED DTO CLASS DEPENDING ON THE DATA STRUCTURE OF THE ARRAY YOU
222
+
WANT TO MAP.***
201
223
202
-
Sometimes you may want to have DTOs as objects that you could pass in AJAX response or whatever you need for.
224
+
Sometimes you may want to have DTOs as objects that you could pass in AJAX response or whatever you need for.
203
225
204
-
Second parameter of mapper methods is a flag that decides if data should be serialized.
226
+
The second parameter of the mapper methods is a flag that decides if the data should be serialized.
205
227
206
228
`ItemDto::mapSingle( $itemData, true )` - this will return you the DTO as a serialized object:
207
229
@@ -219,13 +241,15 @@ Same is true for `mapArray` method.
219
241
220
242
##### DTO Faker
221
243
222
-
You can generate fake data for your DTOs easily using `PhpDto\Services\DtoFaker` class.
244
+
You can generate fake data for your DTOs easily using `PhpDto\Services\DtoFaker` class.
223
245
224
246
```php
225
247
$fakeData = DtoFaker::fakeSingle( ItemDto::class ); // array that contains fake data for ItemDto
226
248
$item = ItemDto::mapSingle( $fakeData );
227
249
```
250
+
228
251
Now your item looks like this:
252
+
229
253
```
230
254
{
231
255
"id": 993
@@ -235,19 +259,112 @@ Now your item looks like this:
235
259
"isActive": false
236
260
}
237
261
```
238
-
All of the values are randomly generated, even the boolean value for isActive field.
239
262
240
-
You can fake multidimensional array via `DtoFaker::fakeArray` method.
263
+
All the values are randomly generated, even the boolean value for isActive field.
264
+
265
+
You can fake multidimensional array via `DtoFaker::fakeArray` method.
241
266
242
267
In the example below we want to fake data for 10 items.
0 commit comments