jessarcher / laravel-castable-data-transfer-object

Automatically cast JSON columns to rich PHP objects in Laravel using Spatie's data-transfer-object class
https://jessarcher.com/blog/casting-json-columns-to-value-objects/
MIT License
328 stars 23 forks source link

Support data-transfer-object v3 #6

Closed jrmajor closed 3 years ago

jrmajor commented 3 years ago

I've allowed spatie/data-transfer-object:^3.0 in composer.json and updated test workflow to run tests for both versions.

One test fails because DTOs now throw the native TypeError (and a few other errors) instead of the DataTransferObjectError. Are you OK with different errors being thrown, or do you want to tag a new major release?

Edit: I think it should definitely be a major release.

jessarcher commented 3 years ago

This is fantastic @jrmajor! Thank you!

I'm definitely happy to drop PHP 7.4 and DTO v2 support and tag this as a major release.

Was there any reason this PR is still a draft? I'd be happy to merge and tag this.

jrmajor commented 3 years ago

@jessarcher This PR is ready and can be merged.

Before you tag a new major, I'd like to propose one more breaking change. We can implement Arrayable and Jsonable interfaces in castable DTO. It already has toJson()/toArray() methods, it just doesn't implement those interfaces. It would make toJson()/toArray() collection methods work properly for collections of DTOs and allow returning DTOs from controllers to return a JSON response. Please let me know, if you would accept a PR for that.

jessarcher commented 3 years ago

@jrmajor Thanks again for your work!

I'm happy for you to implement Arrayable and Jsonable. I can't at the moment think why that would be a breaking change, but I'll wait for your PR before tagging 2.0 :slightly_smiling_face:

jrmajor commented 3 years ago

I can't at the moment think why that would be a breaking change

Laravel treats objects with these interfaces differently, for example if you called toArray() on Laravel Collection of DTOs, previously it would return an array of DTOs, and now DTOs would also be converted to arrays.