Skip to content
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

[8.x] Chainable casts #34963

Closed
wants to merge 6 commits into from
Closed

[8.x] Chainable casts #34963

wants to merge 6 commits into from

Conversation

hotmeteor
Copy link
Contributor

@hotmeteor hotmeteor commented Oct 25, 2020

This PR adds new functionality for "chainable casts", allowing for more reusable and flexible casting of attributes. Chainable casts are defined by using an array of cast types and/or custom cast classes.

For example, an attribute could first be cast as json, and then encrypted, all without needing to add new cast rules or create multiple custom casts. Chainable casts can contain as many casts as necessary, including custom cast classes.

protected $casts = [
    'birth_date' => 'date',
    'password' => [
        'string',
        'encrypted',
    ],
    'number' => [
        'integer',
        'encrypted',
    ],
    'options' => [
        'array',
        'encrypted',
    ],
    'hash' => [
        ReverseCaster::class,
        SlugifyCaster::class,
        Base64Caster::class,
    ],
];

@hotmeteor hotmeteor changed the title Chainable casts [8.x] Chainable casts Oct 25, 2020
@hotmeteor hotmeteor marked this pull request as ready for review October 25, 2020 19:14
@taylorotwell
Copy link
Member

I think I'm going to hold off on this for now. I talked about this with Jason last week and came to the conclusion that I couldn't think of any uses for this outside of encryption, and it introduces a new concept that we would have to maintain / improve / change going forward. Chainable custom casts aren't really necessary IMO because the custom cast already can just implement its own set of options within itself. Encryptable primitives like encrypted integers or booleans don't make much sense either.

@ryangjchandler
Copy link
Contributor

ryangjchandler commented Oct 27, 2020

I think this problem could be solved in userland with a custom caster and slightly different syntax too:

protected $casts = [
    'password' => ChainedCast::class . ':string,encrypted',
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants