Skip to content

Releases: WendellAdriel/laravel-validated-dto

v3.2.1

16 Oct 10:06
9e3da20
Compare
Choose a tag to compare

What's Changed

  • Fix how to handle enums and carbon objects when transforming DTO by @WendellAdriel in #59

Full Changelog: v3.2.0...v3.2.1

v3.2.0

10 Oct 11:37
93e7086
Compare
Choose a tag to compare

What's Changed

Full Changelog: v3.1.2...v3.2.0

v3.1.2

06 Oct 15:01
3cfbda2
Compare
Choose a tag to compare

What's Changed

  • Fix issue when transforming DTOs with nested Collections or Models by @WendellAdriel in #54

Full Changelog: v3.1.1...v3.1.2

v3.1.1

06 Oct 11:23
9d05280
Compare
Choose a tag to compare

What's Changed

  • Fix issue when transforming DTOs with nested DTOs into array or json by @WendellAdriel in #53

Full Changelog: v3.1.0...v3.1.1

v3.1.0

04 Oct 10:27
010a519
Compare
Choose a tag to compare

What's Changed

Full Changelog: v3.0.1...v3.1.0

v3.0.1

18 Sep 12:57
9f8d842
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v3.0.0...v3.0.1

v3.0.0

06 Sep 15:44
d497907
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v2.11.1...v3.0.0

v2.11.1

11 Jul 11:41
a1d7eda
Compare
Choose a tag to compare

What's Changed

  • tests :: refactor some tests by @falahatiali in #39
  • Convert Wireable DTO with deeply nested objects to arrays by @bnzo in #41

New Contributors

Full Changelog: v2.11.0...v2.11.1

v2.11.0 - Callable Casting

30 May 16:41
Compare
Choose a tag to compare

Changes

  • Added the support for defining custom casting with callables/callbacks

Extended Docs

You can now create new Castable types for your project by using a callable/callback:

class CustomDTO extends ValidatedDTO
{
    protected function rules(): array
    {
        return [
            'url' => ['required', 'url'],
        ];
    }

    protected function defaults(): array
    {
        return [];
    }

    protected function casts(): array
    {
        return [
            'url' => function (string $property, mixed $value) {
                return new URLWrapper($value);
            },
        ];
    }
}

New Contributors

ResourceDTO and Wireable Trait

22 May 10:29
1917e62
Compare
Choose a tag to compare

Changes

  • Added a new ResourceDTO class that you can use to wrap, type and transform your API responses
  • Added a new Wireable trait that you can use to turn any DTOs into Wireable DTOs to use with Laravel Livewire

Docs for changes

Resource DTOs

If you want to use DTOs to wrap, type and transform your API responses, you can use the ResourceDTO class.
This class will have the same features as the SimpleDTO class and will implement the Illuminate\Contracts\Support\Responsable interface:

class UserResourceDTO extends ResourceDTO
{
    public string $name;

    public string $email;

    public int $age;

    // Your DTO methods...
}

Then you can return your DTOs from your controllers:

class UserController extends Controller
{
    public function show(int $id)
    {
        return UserResourceDTO::fromModel(User::findOrFail($id));
    }
}

You can also return a collection/list of your DTOs as a response using the ResourceDTO::collection() method:

class UserController extends Controller
{
    public function index()
    {
        return UserResourceDTO::collection(User::all());
    }
}

This way every item in the collection will be converted to a UserResourceDTO instance before sending
the response to the client, using all the typing, casting and mapping features of your DTO class.

To generate a ResourceDTO you can use the --resource flag:

php artisan make:dto UserResourceDTO --resource

Wireable DTOs

If you're using Laravel Livewire, you can turn your DTOs into wireable DTOs
by adding the WendellAdriel\ValidatedDTO\Concerns\Wireable trait to your DTOs:

class UserDTO extends ValidatedDTO
{
    use Wireable;

    // Your DTO code...
}