-
Notifications
You must be signed in to change notification settings - Fork 11.3k
[12.x] Adds Cast to Collect into classes or callback #55377
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] Adds Cast to Collect into classes or callback #55377
Conversation
c79449d
to
6052629
Compare
6052629
to
b4ac245
Compare
Maybe it's better to make method AsEncryptedCollectionMap::using([Color::class, 'make']) Edit: Ah I see it's supported. But I'd prefer to enforce this way, because it's better for static analysis. |
I somewhat concur with Anton. Mostly just to simply the code in the public function __construct(array $arguments)
{
$arguments = array_values($arguments);
if (empty($arguments) || empty($arguments[0])) {
throw new InvalidArgumentException('No class or callable has been set to map the Collection.');
}
if (isset($arguments[1]) && ! is_array($arguments[0])) {
$arguments = [$arguments[0], $arguments[1]];
unset($arguments[1]);
}
$this->callable = $arguments[0];
} What is And then |
@AndrewMast I just brainfaterd, so I fixed it. Anyway, what are your thoughts guys? Should be this remain as its own Castable, or extend the base public function casts()
{
return [
'colors_map_into' => AsCollection::into(Color::class),
'colors_collection' => AsCollection::using(ColorsCollection::class, Color::class),
'colors_map' => AsCollection::map([Color::class, 'make']),
]
} The benefit of the latter is allowing the developer to use its own Collection class but additionally mapping each item to a class or callback, which would be great for both scenarios without sacrifices. This would mean the |
Reworked at #55383 |
What?
This PR adds a new
AsCollectionMap
castable that will allow the developer to create a collection mapped into a class, or to a callback. It's an alternative to use a custom Collection class that would map the items at construction time.A developer may use this castable to map items into a given class using the
into()
static method:It's not limited to class names, but also callbacks that transform the item through the
using()
method. These callbacks should be either a string inclass@method
notation, a callable array, or a class and method set separately.Caution
Because the nature of the casts declaration, there is no support to using a Closure. If a Closure is set, a descriptive exception will be thrown. Alternatively, the developer can use
castUsing()
directly with a closure.The whole logic sits after the value decryption, so encryption can work as any monday morning with the
AsEncryptedCollectionMap
class.