[7.x] Add HasCasterClass interface#32129
Conversation
|
I love this! But for https://github.com/spatie/laravel-enum we would need to be able to pass additional information too. MyEnum::class.':nullable'
EnumCaster::class.':'.MyEnum::class.',nullable'So the interface method should accept additional arguments. |
|
@Gummibeer you'll return whatever string you like from I might send a new PR after this one, which would allow to pass pre-configured instances of casters, instead of their class names. But that's after this. |
|
Hmm, thinking about it, that might not be a solution for your problem… |
|
I can return it - but it should also be possible to pass them in. So the method should accept all additional arguments, like the caster constructor. |
|
Gotcha. I will make sure |
|
Yeah I think the additional arguments would definitely be needed in cases. Other than that I don't see any major issues. |
|
I'll make these changes next week, I'm a little swamped right now with client work :) |
|
I found some spare time. Attributes that are given to a |
|
@slavarazum I think it's a good feature, but outside the scope of this PR. I plan on looking it this after this feature is merged |
|
@taylorotwell could you take a look at my final changes? Any more remarks? |
|
Renamed |
|
May want to PR the docs for this. |
|
Docs PR: laravel/docs#5934 |

This PR adds an interface which allows castable types to specify their caster class. This approach has two advantages:
As an example:
The underlying
MyDTOclass would look like this:As you can tell from the example, my use case is with our DTO package. I would prefer our users to specify what type of DTO they want to cast to, instead of having to manually specify
CastToDTO::class . ':' . MyDTO::classfor each cast. ThegetCasterClasswould be done in our abstract DataTransferObject implementation, and the user wouldn't have to worry about it.