Skip to content

Commit af2cbc4

Browse files
Merge pull request #35 from WendellAdriel/feature/simple-dto
Add SimpleDTO class
2 parents ec88451 + 09a717c commit af2cbc4

17 files changed

+1211
-414
lines changed

README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<a href="#features">Features</a> |
1818
<a href="#installation">Installation</a> |
1919
<a href="#generating-dtos">Generating DTOs</a> |
20+
<a href="#simple-dtos">Simple DTOs</a> |
2021
<a href="#credits">Credits</a> |
2122
<a href="#contributing">Contributing</a>
2223
</p>
@@ -85,7 +86,7 @@ With this package you **define the validation once** and can **reuse it where yo
8586

8687
You can create `DTOs` using the `make:dto` command:
8788

88-
```
89+
```bash
8990
php artisan make:dto UserDTO
9091
```
9192

@@ -955,6 +956,58 @@ class AttributesDTO extends ValidatedDTO
955956
}
956957
```
957958

959+
## Simple DTOs
960+
961+
If you don't need to validate the data, you can use the `SimpleDTO` class instead of the `ValidatedDTO` class.
962+
The DTOs created with this class will not validate the data, but will still have all the other features of the `ValidatedDTO` class:
963+
964+
```php
965+
class SimpleUserDTO extends SimpleDTO
966+
{
967+
public string $name;
968+
969+
public string $email;
970+
971+
public int $age;
972+
973+
protected function defaults(): array
974+
{
975+
return [];
976+
}
977+
978+
protected function casts(): array
979+
{
980+
return [
981+
'name' => new StringCast(),
982+
'email' => new StringCast(),
983+
'age' => new IntegerCast(),
984+
];
985+
}
986+
987+
protected function mapBeforeValidation(): array
988+
{
989+
return [
990+
'username' => 'name',
991+
'user_email' => 'email',
992+
];
993+
}
994+
995+
protected function mapBeforeExport(): array
996+
{
997+
return [
998+
'name' => 'customer_name',
999+
'email' => 'customer_email',
1000+
];
1001+
}
1002+
}
1003+
```
1004+
1005+
To generate a `SimpleDTO` you can use the `--simple` flag:
1006+
1007+
```bash
1008+
php artisan make:dto SimpleUserDTO --simple
1009+
```
1010+
9581011
## Credits
9591012

9601013
- [Wendell Adriel](https://github.com/WendellAdriel)

src/Casting/DTOCast.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Throwable;
77
use WendellAdriel\ValidatedDTO\Exceptions\CastException;
88
use WendellAdriel\ValidatedDTO\Exceptions\CastTargetException;
9-
use WendellAdriel\ValidatedDTO\ValidatedDTO;
9+
use WendellAdriel\ValidatedDTO\SimpleDTO;
1010

1111
class DTOCast implements Castable
1212
{
@@ -17,7 +17,7 @@ public function __construct(private string $dtoClass)
1717
/**
1818
* @throws CastException|CastTargetException|ValidationException
1919
*/
20-
public function cast(string $property, mixed $value): ValidatedDTO
20+
public function cast(string $property, mixed $value): SimpleDTO
2121
{
2222
if (is_string($value)) {
2323
$value = json_decode($value, true);
@@ -35,7 +35,7 @@ public function cast(string $property, mixed $value): ValidatedDTO
3535
throw new CastException($property);
3636
}
3737

38-
if (! ($dto instanceof ValidatedDTO)) {
38+
if (! ($dto instanceof SimpleDTO)) {
3939
throw new CastTargetException($property);
4040
}
4141

src/Console/Commands/MakeDTOCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ protected function getPath($name)
7070
*/
7171
protected function getStub(): string
7272
{
73-
return __DIR__.'/../stubs/dto.stub';
73+
return $this->option('simple')
74+
? __DIR__.'/../stubs/simple_dto.stub'
75+
: __DIR__.'/../stubs/dto.stub';
7476
}
7577

7678
/**
@@ -80,6 +82,7 @@ protected function getOptions(): array
8082
{
8183
return [
8284
['force', null, InputOption::VALUE_NONE, 'Create the class even if the DTO already exists'],
85+
['simple', null, InputOption::VALUE_NONE, 'If the DTO should be a SimpleDTO instead of a ValidatedDTO'],
8386
];
8487
}
8588
}

src/Console/stubs/dto.stub

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class {{ class }} extends ValidatedDTO
88
{
99
/**
1010
* Defines the validation rules for the DTO.
11-
*
12-
* @return array
1311
*/
1412
protected function rules(): array
1513
{
@@ -18,8 +16,6 @@ class {{ class }} extends ValidatedDTO
1816

1917
/**
2018
* Defines the default values for the properties of the DTO.
21-
*
22-
* @return array
2319
*/
2420
protected function defaults(): array
2521
{
@@ -28,18 +24,30 @@ class {{ class }} extends ValidatedDTO
2824

2925
/**
3026
* Defines the type casting for the properties of the DTO.
31-
*
32-
* @return array
3327
*/
3428
protected function casts(): array
3529
{
3630
return [];
3731
}
3832

33+
/**
34+
* Maps the DTO properties before the DTO instantiation.
35+
*/
36+
protected function mapBeforeValidation(): array
37+
{
38+
return [];
39+
}
40+
41+
/**
42+
* Maps the DTO properties before the DTO export.
43+
*/
44+
protected function mapBeforeExport(): array
45+
{
46+
return [];
47+
}
48+
3949
/**
4050
* Defines the custom messages for validator errors.
41-
*
42-
* @return array
4351
*/
4452
public function messages(): array
4553
{
@@ -48,8 +56,6 @@ class {{ class }} extends ValidatedDTO
4856

4957
/**
5058
* Defines the custom attributes for validator errors.
51-
*
52-
* @return array
5359
*/
5460
public function attributes(): array
5561
{

src/Console/stubs/simple_dto.stub

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use WendellAdriel\ValidatedDTO\SimpleDTO;
6+
7+
class {{ class }} extends SimpleDTO
8+
{
9+
/**
10+
* Defines the default values for the properties of the DTO.
11+
*/
12+
protected function defaults(): array
13+
{
14+
return [];
15+
}
16+
17+
/**
18+
* Defines the type casting for the properties of the DTO.
19+
*/
20+
protected function casts(): array
21+
{
22+
return [];
23+
}
24+
25+
/**
26+
* Maps the DTO properties before the DTO instantiation.
27+
*/
28+
protected function mapBeforeValidation(): array
29+
{
30+
return [];
31+
}
32+
33+
/**
34+
* Maps the DTO properties before the DTO export.
35+
*/
36+
protected function mapBeforeExport(): array
37+
{
38+
return [];
39+
}
40+
}

0 commit comments

Comments
 (0)