Coder Social home page Coder Social logo

Comments (7)

BenjaminMINK avatar BenjaminMINK commented on August 18, 2024

Sorry if I'm unclear, what I would expect by passing a custom value is that the castable class can return correct casting for any passed value

from framework.

driesvints avatar driesvints commented on August 18, 2024

Hi @BenjaminMINK. I don't think this works like in your example. AsEnumCollection is currently documented to work like follow: https://laravel.com/docs/11.x/eloquent-mutators#casting-arrays-of-enums

Can you try that and let me know if it works?

from framework.

BenjaminMINK avatar BenjaminMINK commented on August 18, 2024

Hello,

Thank you for your answer, but what I've showed in my example is for Laravel 10.x.

I've also tested with the new syntax in Laravel 11.x, the same "issue" happens, in fact, the casting classes haven't changed since 10.x, so the same thing happens.

from framework.

BenjaminMINK avatar BenjaminMINK commented on August 18, 2024

Based on AsEnumCollection class:

As today, the get method has:

$data = Json::decode($attributes[$key]);

if (! is_array($data)) {
    return;
}

$enumClass = $this->arguments[0];

return (new Collection($data))->map(function ($value) use ($enumClass) {
    return is_subclass_of($enumClass, BackedEnum::class)
        ? $enumClass::from($value)
        : constant($enumClass.'::'.$value);
});

We might be expect something like this when we pass the $value argument:

$data = Json::decode($value ?? $attributes[$key]);

if (! is_array($data)) {
    return;
}

$enumClass = $this->arguments[0];

return (new Collection($data))->map(function ($value) use ($enumClass) {
    return is_subclass_of($enumClass, BackedEnum::class)
        ? $enumClass::from($value)
        : constant($enumClass.'::'.$value);
});

from framework.

github-actions avatar github-actions commented on August 18, 2024

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

from framework.

Lakshan-Madushanka avatar Lakshan-Madushanka commented on August 18, 2024

castAttrbute() is an internal method since its visibility is protected. However, I'm curious how you managed to call that method from outside without an exception being thrown.

from framework.

driesvints avatar driesvints commented on August 18, 2024

@Lakshan-Madushanka is correct. It doesn't seem we support this usage sorry.

from framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.