Skip to content

Commit 59d96ea

Browse files
committed
update doc
1 parent 40a63de commit 59d96ea

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

README.md

+22-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This is where this package comes to the rescue, which takes care of all the work
3838
- [After Transform](#after-transform)
3939
- [Custom transform](#custom-transform)
4040
- [Comparison](#Comparison)
41+
- [Cache](#Cache)
4142

4243
## **Installation**
4344

@@ -73,7 +74,10 @@ $data = [
7374
'email' => 'test@mail.com',
7475
'balance' => 128.41,
7576
];
76-
$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);
7781
var_dump($dto);
7882
```
7983

@@ -88,7 +92,7 @@ object(\LoginDTO)
8892
Also for php 8 you can pass named arguments:
8993

9094
```php
91-
$dto = ClassTransformer::transform(CreateUserDTO::class,
95+
$dto = Hydrator::init()->create(CreateUserDTO::class,
9296
email: 'test@mail.com',
9397
balance: 128.41
9498
);
@@ -114,7 +118,7 @@ $data = [
114118
'cost' => 10012.23,
115119
];
116120

117-
$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
121+
$purchaseDTO = Hydrator::init()->create(PurchaseDTO::class, $data);
118122
var_dump($purchaseDTO);
119123
```
120124

@@ -158,7 +162,7 @@ $data = [
158162
['id' => 2, 'name' => 'bread',],
159163
],
160164
];
161-
$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
165+
$purchaseDTO = Hydrator::init()->create(PurchaseDTO::class, $data);
162166
```
163167

164168
### **Anonymous array**
@@ -171,7 +175,7 @@ $data = [
171175
['id' => 1, 'name' => 'phone'],
172176
['id' => 2, 'name' => 'bread'],
173177
];
174-
$products = ClassTransformer::transformCollection(ProductDTO::class, $data);
178+
$products = Hydrator::init()->createCollection(ProductDTO::class, $data);
175179
```
176180

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

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

209213
[$user, $purchase] = $result;
210214
var_dump($user);
@@ -262,7 +266,7 @@ class WritingStyleSnakeCaseDTO
262266
'contactFio' => 'yzen.dev',
263267
'contactEmail' => 'test@mail.com',
264268
];
265-
$model = ClassTransformer::transform(WritingStyleSnakeCaseDTO::class, $data);
269+
$model = Hydrator::init()->create(WritingStyleSnakeCaseDTO::class, $data);
266270
var_dump($model);
267271
```
268272

@@ -345,6 +349,17 @@ class CustomTransformUserDTOArray
345349
}
346350
```
347351

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+
348363
### Comparison
349364
I also made a comparison with current analogues and here are the main disadvantages
350365
- Works only for a specific framework

docs/usage.rst

+25-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ Common use case:
2020
'email' => 'test@mail.com',
2121
'balance' => 128.41,
2222
];
23-
$dto = ClassTransformer::transform(CreateUserDTO::class, $data);
23+
$dto = (new Hydrator())->create(CreateUserDTO::class, $data);
24+
25+
// or static init
26+
27+
$dto = Hydrator::init()->create(CreateUserDTO::class, $data);
28+
2429
var_dump($dto);
2530
2631
Output:
@@ -34,7 +39,7 @@ Also for php 8 you can pass named arguments:
3439

3540
.. code-block:: php
3641
37-
$dto = ClassTransformer::transform(CreateUserDTO::class,
42+
$dto = Hydrator::init()->create(CreateUserDTO::class,
3843
email: 'test@mail.com',
3944
balance: 128.41
4045
);
@@ -60,7 +65,7 @@ If the property is not of a scalar type, but a class of another DTO is allowed,
6065
'cost' => 10012.23,
6166
];
6267
63-
$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
68+
$purchaseDTO = Hydrator::init()->create(PurchaseDTO::class, $data);
6469
var_dump($purchaseDTO);
6570
6671
.. code-block:: bash
@@ -101,22 +106,22 @@ Example:
101106
['id' => 2, 'name' => 'bread',],
102107
],
103108
];
104-
$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);
109+
$purchaseDTO = Hydrator::init()->create(PurchaseDTO::class, $data);
105110
106111
107112
Anonymous array
108113
---------------
109114

110115
In case you need to convert an array of data into an array of class objects, you can implement this using
111-
the `transformCollection` method.
116+
the `createCollection` method.
112117

113118
.. code-block:: php
114119
115120
$data = [
116121
['id' => 1, 'name' => 'phone'],
117122
['id' => 2, 'name' => 'bread'],
118123
];
119-
$products = ClassTransformer::transformCollection(ProductDTO::class, $data);
124+
$products = Hydrator::init()->createCollection(ProductDTO::class, $data);
120125
121126
122127
As a result of this execution, you will get an array of ProductDTO objects
@@ -150,7 +155,7 @@ which can then be easily unpacked.
150155
'user' => ['id' => 3, 'email' => 'fake@mail.com', 'balance' => 10012.23,],
151156
];
152157
153-
$result = ClassTransformer::transformMultiple([UserDTO::class, PurchaseDTO::class], [$userData, $purchaseData]);
158+
$result = Hydrator::init()->createMultiple([UserDTO::class, PurchaseDTO::class], [$userData, $purchaseData]);
154159
155160
[$user, $purchase] = $result;
156161
var_dump($user);
@@ -209,7 +214,7 @@ A constant problem with the style of writing, for example, in the database it is
209214
'contactFio' => 'yzen.dev',
210215
'contactEmail' => 'test@mail.com',
211216
];
212-
$model = ClassTransformer::transform(WritingStyleSnakeCaseDTO::class, $data);
217+
$model = Hydrator::init()->create(WritingStyleSnakeCaseDTO::class, $data);
213218
var_dump($model);
214219
215220
Output:
@@ -294,3 +299,15 @@ If you need to completely transform yourself, then you can create a transform me
294299
$this->username = $args['fio'];
295300
}
296301
}
302+
303+
Cache
304+
----------------
305+
306+
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.
307+
308+
You can enable caching by passing the config to the hydrator constructor:
309+
310+
.. code-block:: php
311+
312+
(new Hydrator(new HydratorConfig(true)))
313+
->create(PurchaseDto::class, $data);

0 commit comments

Comments
 (0)