Giter Club home page Giter Club logo

Comments (6)

Skullbock avatar Skullbock commented on August 25, 2024

Hey @Gummibeer if you want i implemented in one of my project this.
It's still rough, but it allows you to use the cast class directly, without needing a trait:

 /**
     * @var string[]
     */
    protected $casts = [
        'color'           => ColorEnum::class
    ];

The basic idea is:

abstract class Enum extends \Spatie\Enum\Enum implements \Illuminate\Contracts\Database\Eloquent\Castable
{
    public static function castUsing()
    {
        return EnumCast::class;
    }

    protected static function resolveFromStaticMethods(\ReflectionClass $reflection): array
    {
        $methods = parent::resolveFromStaticMethods($reflection);

        unset($methods[array_search('castUsing', $methods)]);

        return $methods;
    }
}

where EnumCast is:

class EnumCast implements \Illuminate\Contracts\Database\Eloquent\CastsAttributes
{
    private ?string $enumClass;

    public function __construct(?string $enumClass = null)
    {
        $this->enumClass = $enumClass;
    }

    public function get($model, $key, $value, $attributes)
    {
        if ($value === null) {
            return $value;
        }

        $class = $this->enumClass ?: $model->getCasts()[$key];
        if (!$value instanceof Enum) {
            $value = $class::make($value);
        }

        return $value;
    }
    public function set($model, $key, $value, $attributes)
    {
        if (!$value instanceof Enum) {
            return $value;
        }

        return (string) $value;
    }
}

from laravel-enum.

Gummibeer avatar Gummibeer commented on August 25, 2024

Hey @Skullbock sorry that I haven't linked the PR.
The casting is already done in #44 right now the dropped $enums property in the trait brings an issue for the scopes.
That's why it's not merged already.
But feel free to test the new casts.
Your implementation made me aware of #39 again. So this will also be a requirement for v2.

from laravel-enum.

Skullbock avatar Skullbock commented on August 25, 2024

Ah sorry, didn't see that.
You PR'ed solution is way more advanced than mine so i'll use that when released.

Just my 2 cents, my solution, while way less flexible than yours, has 1 small advantage in terms of DX: you can see the class being casted directly there, as an autocompletable and IDE navigatable class, instead of a string as it is in your case. It's a small thing, but still i though worth it to point it out.

 /**
     * @var string[]
     */
    protected $casts = [
        'color'           => ColorEnum::class
    ];

Cheers!

from laravel-enum.

Gummibeer avatar Gummibeer commented on August 25, 2024

Hey,

we have introduced multiple casts (4) so we can't deliver a ready to use enum defining the castUsing() method. But we will add an example showing this option to the docs.
I totally love this feature added by @brendt !
What we will prepare is a way to exclude static methods from resolving the enum. Will check which way we will use, but I think that a special doc-comment annotation will be the easiest for all.
Okay, have checked #39 again and we will exclude this special method based on interface implementation.

from laravel-enum.

Skullbock avatar Skullbock commented on August 25, 2024

Gotcha, gonna wait eagerly for that PR to be merged in

from laravel-enum.

Gummibeer avatar Gummibeer commented on August 25, 2024

Me too, but right now we have some big issues with the scopes. We will have to discuss if we drop the scopes at all or if we have to limit the usability to the most common usecases, expect a given input (real enum instances instead of strings and ints and enumerables mixed) or write some pretty complex magic to let them work the same like right now.
I'm thinking about merging the custom cast classes in addition to the trait in the current v1. But will wait some time for response regarding the scopes topic at first.

from laravel-enum.

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.