Skip to content

Commit 1917e62

Browse files
Merge pull request #37 from WendellAdriel/feature/new-dtos
ResourceDTO and Wireable Trait
2 parents af2cbc4 + 65a406a commit 1917e62

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+668
-22
lines changed

README.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +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> |
20+
<a href="#more-dtos">More DTOs</a> |
2121
<a href="#credits">Credits</a> |
2222
<a href="#contributing">Contributing</a>
2323
</p>
@@ -37,6 +37,8 @@
3737
- Support casting of **nested data**
3838
- Easily create **custom Type Casters** for your own needs
3939
- Custom Data Mapping
40+
- Use DTOs for wrapping, typing and transforming API responses
41+
- **[Laravel Livewire](https://laravel-livewire.com/)** support
4042

4143
## Installation
4244

@@ -955,8 +957,9 @@ class AttributesDTO extends ValidatedDTO
955957
}
956958
}
957959
```
960+
## More DTOs
958961

959-
## Simple DTOs
962+
### Simple DTOs
960963

961964
If you don't need to validate the data, you can use the `SimpleDTO` class instead of the `ValidatedDTO` class.
962965
The DTOs created with this class will not validate the data, but will still have all the other features of the `ValidatedDTO` class:
@@ -1008,6 +1011,71 @@ To generate a `SimpleDTO` you can use the `--simple` flag:
10081011
php artisan make:dto SimpleUserDTO --simple
10091012
```
10101013

1014+
### Resource DTOs
1015+
1016+
If you want to use DTOs to wrap, type and transform your API responses, you can use the `ResourceDTO` class.
1017+
This class will have the same features as the `SimpleDTO` class and will implement the `Illuminate\Contracts\Support\Responsable` interface:
1018+
1019+
```php
1020+
class UserResourceDTO extends ResourceDTO
1021+
{
1022+
public string $name;
1023+
1024+
public string $email;
1025+
1026+
public int $age;
1027+
1028+
// Your DTO methods...
1029+
}
1030+
```
1031+
1032+
Then you can return your DTOs from your controllers:
1033+
1034+
```php
1035+
class UserController extends Controller
1036+
{
1037+
public function show(int $id)
1038+
{
1039+
return UserResourceDTO::fromModel(User::findOrFail($id));
1040+
}
1041+
}
1042+
```
1043+
1044+
You can also return a collection/list of your DTOs as a response using the `ResourceDTO::collection()` method:
1045+
1046+
```php
1047+
class UserController extends Controller
1048+
{
1049+
public function index()
1050+
{
1051+
return UserResourceDTO::collection(User::all());
1052+
}
1053+
}
1054+
```
1055+
1056+
This way every item in the collection will be converted to a `UserResourceDTO` instance before sending
1057+
the response to the client, using all the typing, casting and mapping features of your DTO class.
1058+
1059+
To generate a `ResourceDTO` you can use the `--resource` flag:
1060+
1061+
```bash
1062+
php artisan make:dto UserResourceDTO --resource
1063+
```
1064+
1065+
### Wireable DTOS
1066+
1067+
If you're using **[Laravel Livewire](https://laravel-livewire.com/)**, you can turn your DTOs into **wireable** DTOs
1068+
by adding the `WendellAdriel\ValidatedDTO\Concerns\Wireable` trait to your DTOs:
1069+
1070+
```php
1071+
class UserDTO extends ValidatedDTO
1072+
{
1073+
use Wireable;
1074+
1075+
// Your DTO code...
1076+
}
1077+
```
1078+
10111079
## Credits
10121080

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

config/dto.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
return [
46
/*
57
|--------------------------------------------------------------------------

pint.json

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
{
22
"preset": "laravel",
33
"rules": {
4+
"array_push": true,
5+
"assign_null_coalescing_to_coalesce_equal": true,
6+
"combine_consecutive_issets": true,
7+
"combine_consecutive_unsets": true,
8+
"concat_space": {
9+
"spacing": "one"
10+
},
11+
"declare_strict_types": true,
12+
"explicit_indirect_variable": true,
13+
"explicit_string_variable": true,
14+
"global_namespace_import": true,
15+
"method_argument_space": {
16+
"on_multiline": "ensure_fully_multiline"
17+
},
18+
"modernize_strpos": true,
19+
"modernize_types_casting": true,
20+
"new_with_braces": true,
21+
"no_superfluous_elseif": true,
22+
"no_useless_else": true,
23+
"nullable_type_declaration_for_default_null_value": true,
424
"ordered_imports": {
525
"sort_algorithm": "alpha"
626
},
@@ -28,6 +48,9 @@
2848
"method_private"
2949
],
3050
"sort_algorithm": "none"
31-
}
51+
},
52+
"strict_comparison": true,
53+
"ternary_to_null_coalescing": true,
54+
"use_arrow_functions": true
3255
}
33-
}
56+
}

src/Casting/ArrayCast.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace WendellAdriel\ValidatedDTO\Casting;
46

57
class ArrayCast implements Castable

src/Casting/BooleanCast.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace WendellAdriel\ValidatedDTO\Casting;
46

57
class BooleanCast implements Castable
@@ -14,6 +16,6 @@ public function cast(string $property, mixed $value): bool
1416
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
1517
}
1618

17-
return boolval($value);
19+
return (bool) $value;
1820
}
1921
}

src/Casting/CarbonCast.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace WendellAdriel\ValidatedDTO\Casting;
46

57
use Carbon\Carbon;

src/Casting/CarbonImmutableCast.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace WendellAdriel\ValidatedDTO\Casting;
46

57
use Carbon\CarbonImmutable;

src/Casting/Castable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace WendellAdriel\ValidatedDTO\Casting;
46

57
interface Castable

src/Casting/CollectionCast.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace WendellAdriel\ValidatedDTO\Casting;
46

57
use Illuminate\Support\Collection;
@@ -16,8 +18,6 @@ public function cast(string $property, mixed $value): Collection
1618
$value = $arrayCast->cast($property, $value);
1719

1820
return Collection::make($value)
19-
->when($this->type, function ($collection, $castable) use ($property) {
20-
return $collection->map(fn ($item) => $castable->cast($property, $item));
21-
});
21+
->when($this->type, fn ($collection, $castable) => $collection->map(fn ($item) => $castable->cast($property, $item)));
2222
}
2323
}

src/Casting/DTOCast.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace WendellAdriel\ValidatedDTO\Casting;
46

57
use Illuminate\Validation\ValidationException;

0 commit comments

Comments
 (0)