Skip to content

Commit 6667a96

Browse files
committed
[DTO-17] Export keys in snake case in toArray method
1 parent eb3b8f3 commit 6667a96

File tree

11 files changed

+678
-229
lines changed

11 files changed

+678
-229
lines changed

README.md

Lines changed: 205 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class GenerateTransfer extends Command
116116

117117
```
118118

119-
5. Define the dto through the definition files
119+
5. Define the dto through the definition file
120120

121121
```src/dto-definitions/customer.json```
122122

@@ -156,7 +156,7 @@ class GenerateTransfer extends Command
156156

157157
```
158158

159-
This definition will generate the following transfer classes:
159+
This definition will generate the following transfer class:
160160

161161
```php
162162

@@ -299,7 +299,180 @@ class CustomerTransfer extends AbstractTransfer
299299

300300
```
301301

302-
6. Create definition file(s)
302+
## Usage
303+
304+
### Creating and set data
305+
306+
```php
307+
$customerTransfer = new CustomerTransfer();
308+
$customerTransfer
309+
->setFirstName('John')
310+
->setLastName('Smith')
311+
->setEmail('user@example.com')
312+
->setBirthDate(new DateTime('2000-01-01'))
313+
->setIsActive(true);
314+
```
315+
316+
### Get data
317+
318+
```php
319+
$firstName = $customerTransfer->getFirstName(); // John
320+
$lastName = $customerTransfer->getLastName(); // Smith
321+
```
322+
323+
### Creating from array
324+
325+
When creating from array its possible to use both **camelCase** or **snake_case** as array keys
326+
327+
```php
328+
$data = [
329+
'first_name' => 'John',
330+
'last_name' => 'Smith',
331+
'email' => 'user@example.com',
332+
'birth_date' => new DateTime('2000-01-01'),
333+
'is_active' => true),
334+
];
335+
$customerTransfer = CustomerTransfer::fromArray($data);
336+
337+
$data = [
338+
'streetName' => 'test street name',
339+
'city' => 'test-city',
340+
'zipCode' => '1999',
341+
'isDefaultBillingAddress' => true,
342+
'isDefaultShippingAddress' => false,
343+
];
344+
$addressTransfer = AddressTransfer::fromArray($data);
345+
```
346+
347+
### Export to array
348+
349+
```php
350+
$customerTransfer = new CustomerTransfer();
351+
$customerTransfer
352+
->setFirstName('John')
353+
->setLastName('Smith')
354+
->setEmail('user@example.com')
355+
->setBirthDate(new DateTime('2000-01-01'))
356+
->setIsActive(true);
357+
358+
$data = $customerTransfer->toArray();
359+
```
360+
361+
will return:
362+
363+
```php
364+
[
365+
'firstName' => 'John',
366+
'lastName' => 'Smith',
367+
'email' => 'user@example.com',
368+
'birthDate' => new DateTime('2000-01-01'),
369+
'isActive' => true,
370+
]
371+
```
372+
373+
The ``toArray()`` method has two parameters
374+
375+
```php
376+
public function toArray(bool $isRecursive = false, bool $snakeCaseKeys = false): array
377+
```
378+
379+
- ``isRecursive`` when true will also export child transfer objects to an array
380+
381+
```php
382+
$customerTransfer = new CustomerTransfer();
383+
$customerTransfer
384+
->setEmail('user@example.com')
385+
->setBirthDate(new DateTime('2000-01-01'))
386+
->setFirstName('John')
387+
->setLastName('Smith')
388+
->setIsActive(true);
389+
390+
$orderItemTransfer1 = new OrderItemTransfer();
391+
$orderItemTransfer1
392+
->setName('Chips')
393+
->setPrice(5.99)
394+
->setQuantity(1)
395+
->setId(1);
396+
397+
$orderItemTransfer2 = new OrderItemTransfer();
398+
$orderItemTransfer2
399+
->setName('Juice')
400+
->setPrice(3.45)
401+
->setQuantity(2)
402+
->setId(2);
403+
404+
$orderTransfer = new OrderTransfer();
405+
$orderTransfer
406+
->setId(1)
407+
->setCustomer($customerTransfer)
408+
->setTotal(10.00)
409+
->setOrderItems([
410+
$orderItemTransfer1,
411+
$orderItemTransfer2
412+
])
413+
->setCreatedAt(new DateTime('2023-10-01'));
414+
415+
$data = $orderTransfer->toArray(true);
416+
```
417+
418+
will return:
419+
420+
```php
421+
[
422+
'id' => 1,
423+
'customer' => [
424+
'firstName' => 'John',
425+
'lastName' => 'Smith',
426+
'email' => 'user@example.com',
427+
'birthDate' => new DateTime('2000-01-01'),
428+
'isActive' => true,
429+
],
430+
'total' => 10.0,
431+
'orderItems' => [
432+
[
433+
'id' => 1,
434+
'name' => 'Chips',
435+
'price' => 5.99,
436+
'quantity' => 1,
437+
],
438+
[
439+
'id' => 2,
440+
'name' => 'Juice',
441+
'price' => 3.45,
442+
'quantity' => 2,
443+
],
444+
],
445+
'createdAt' => new DateTime('2023-10-01'),
446+
]
447+
```
448+
449+
- ``snakeCaseKeys`` when true will also export array with *snake_case* keys (by default is *camelCase*)
450+
451+
```php
452+
$customerTransfer = new CustomerTransfer();
453+
$customerTransfer
454+
->setFirstName('John')
455+
->setLastName('Smith')
456+
->setEmail('user@example.com')
457+
->setBirthDate(new DateTime('2000-01-01'))
458+
->setIsActive(true);
459+
460+
$data = $customerTransfer->toArray(false, true);
461+
```
462+
463+
will return:
464+
465+
```php
466+
[
467+
'first_name' => 'John',
468+
'last_name' => 'Smith',
469+
'email' => 'user@example.com',
470+
'birth_date' => new DateTime('2000-01-01'),
471+
'is_active' => true,
472+
]
473+
```
474+
475+
## Create definition file(s)
303476

304477
You can define one or more transfer objects definitions for each json file.
305478
Start by creating a json object that will contain your definitions:
@@ -311,7 +484,7 @@ Start by creating a json object that will contain your definitions:
311484
}
312485
```
313486

314-
and inside the transfers array define your transfer:
487+
and inside the ``transfers`` array define your transfer:
315488

316489
```json
317490
{
@@ -333,108 +506,96 @@ and inside the transfers array define your transfer:
333506
}
334507
```
335508

336-
- transfer class
509+
### Available fields
510+
511+
- Class
337512

338-
| field | type | required | Description |
339-
|------------------------|--------|----------|-----------------------------------------------------------------------------------------------------------------------|
340-
| name | string | yes | The transfer object name. The result class name will be this name concatenated with "Transfer". E.g. CustomerTransfer |
341-
| properties | array | yes | An array of objects with definition of each class property |
342-
| immutable | bool | no | Remove setters from the class. In this case the class name will end with "TransferImmutable" |
343-
| deprecationDescription | string | no | If present and not empty, will add an annotation with @deprecated, to mark this class as deprecated |
513+
| Field | Type | Required | Default | Description |
514+
|------------------------|--------|----------|---------|-----------------------------------------------------------------------------------------------------------------------|
515+
| name | string | yes | -- | The transfer object name. The result class name will be this name concatenated with "Transfer". E.g. CustomerTransfer |
516+
| properties | array | yes | -- | An array of objects with definition of each class property |
517+
| immutable | bool | no | false | Remove setters from the class. In this case the class name will end with "TransferImmutable" |
518+
| deprecationDescription | string | no | "" | If present and not empty, will add an annotation with @deprecated, to mark this class as deprecated |
344519

345-
- class properties
520+
- Class properties
346521

347-
| field | type | required | Description |
348-
|------------------------|--------|----------------------------------|-------------------------------------------------------------------------------------------------------|
349-
| name | string | yes | field name in camelCase |
350-
| type | string | yes | The field type. Can be a native type (string, int, float, bool), or any other class |
351-
| deprecationDescription | string | no | If present and with a text, will add an annotation with @deprecated, to mark this field as deprecated |
352-
| nullable | bool | no | Set if the property can be null |
353-
| namespace | string | yes if the type is another class | Namespace for the class in case the property type is another class |
354-
| singular | string | yes if the type is an array | Singular form of the property if the type is an array |
522+
| Field | Type | Required | Default | Description |
523+
|------------------------|--------|----------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------|
524+
| name | string | yes | -- | field name in camelCase |
525+
| type | string | yes | -- | The field type. Can be a native type (string, int, float, bool), or any other class. If the type ends with [], will mark the property as an array |
526+
| deprecationDescription | string | no | "" | If present and with a text, will add an annotation with @deprecated, to mark this field as deprecated |
527+
| nullable | bool | no | false | Set if the property can be null. Can not be set to true when the type is an array |
528+
| namespace | string | yes if the type is another class | -- | Namespace for the class in case the property type is another class (except another transfer object) |
529+
| singular | string | yes if the type is an array | -- | Singular form of the property if the type is an array |
355530

356-
## Example of property definitions
531+
### Example of property definitions
357532

358533
- integer
359534

360-
```
361-
...
535+
```json lines
362536
{
363537
"name": "id",
364538
"type": "int"
365539
}
366-
...
367540
```
368541

369542
- nullable string
370543

371-
```
372-
...
544+
```json lines
373545
{
374546
"name": "firstName",
375547
"type": "string",
376548
"nullable": true
377549
}
378-
...
379550
```
380551

381552
- another transfer object as property
382553

383-
```
384-
...
554+
```json lines
385555
{
386556
"name": "customer",
387557
"type": "CustomerTransfer"
388558
}
389-
...
390559
```
391560

392561
- DateTime property
393562

394-
```
395-
...
563+
```json lines
396564
{
397565
"name": "createdAt",
398566
"type": "DateTime",
399567
"namespace": "DateTime"
400568
}
401-
...
402569
```
403570

404571
- Array of strings
405572

406-
```
407-
...
573+
```json lines
408574
{
409575
"name": "tags",
410576
"type": "string[]",
411577
"singular": "tag"
412578
}
413-
...
414579
```
415580

416581
- Array of transfer objects
417582

418-
```
419-
...
583+
```json lines
420584
{
421585
"name": "categories",
422586
"type": "CategoryTransfer[]",
423587
"singular": "category"
424-
},
425-
...
588+
}
426589
```
427590

428591
- Symfony Response
429592

430-
```
431-
...
593+
```json lines
432594
{
433595
"name": "response",
434596
"type": "Response",
435597
"namespace": "\\Symfony\\Component\\HttpFoundation\\Response"
436-
},
437-
...
598+
}
438599
```
439600

440601
## License

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
parameters:
2-
level: 6
2+
level: 7
33
paths:
44
- src

0 commit comments

Comments
 (0)