-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[12.x] Ensures casts objects can be transformed into strings #56687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[12.x] Ensures casts objects can be transformed into strings #56687
Conversation
|
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
|
Now, you have named arguments, but the position might not immediately clear. I can definitely see your point, but I'm not sure whether that's the ideal implementation (yet). |
Same as with route middleware declarations with stringables at #39439. The main idea is to allow fluent cast declaration when there is more than one parameter. This would enable a built-in use Illuminate\Database\Eloquent\Cast;
public function casts()
{
return [
'retired_at' => Cast::asDatetime()->fromTimestamp()->tz('America/NewYork'),
];
} |
|
I don't follow the CastsAttributes example at all tbh. |
|
I think the So I gave it a thought and I made it much simpler: if the object is That could suffice since it gives total control to the developer on the cast configuration and declaration, much like happen with middleware builders. |
What?
This adds the ability for a Model to cast an attribute using a Stringable object rather than a string. This allows for expressive casting instead of static methods trying to shove in all arguments possible without hints (and no warranties over consistent argument names between versions).
The idea is to let these complex Casters to call an auxiliary class or methods that handles the configuration, a Castable builder of sorts. It can even be the cast class itself.
The way it works is relatively simple: if we're passing an object that implements
Stringable, we will use its string representation as a cast, which most of the time will be manually done by the developer.The magic behind all of this new match arm in
ensureCastsAreStringValues(), that works when thecast()method of the Model is called. It detects if the cast is an object and proceeds as previously explained. In a nutshell: