Giter Site home page Giter Site logo

Map by name=>value about enum HOT 6 CLOSED

spatie avatar spatie commented on September 7, 2024
Map by name=>value

from enum.

Comments (6)

Gummibeer avatar Gummibeer commented on September 7, 2024

Hey,

you've already posted the solution. Just put this into your own base Enum class and extend this with all your enums.

namespace App\Enums;

abstract class Enum extends \Spatie\Enum\Enum
{
    public static function toArray(): array
    {
        return array_combine(static::getNames(), static::getValues());
    }
}

final class SaveStatus extends \App\Enums\Enum {}

from enum.

dionysiosarvanitis avatar dionysiosarvanitis commented on September 7, 2024

Hi! Thank you for answering.

I tried what you said, but I got error on initialization. Constructor's 3rd argument $index must be an int. Overriding it is not enough because getIndex() must also return int.

public function __construct(?string $name = null, ?string $value = null, ?int $index = null)

self::$cache[$class][$name]['index'] = static::make($name)->getIndex();

from enum.

Gummibeer avatar Gummibeer commented on September 7, 2024

Good point, right now we depend on the toArray() method in resolve logic.
This should be changed. I think that this is changed by @brendt in v3 #56
For the moment you could add your own method to achieve what you need.

from enum.

Gummibeer avatar Gummibeer commented on September 7, 2024

@dionysiosarvanitis I've added a PR #58 that drops the usage of toArray() in the Enum class itself.

from enum.

Gummibeer avatar Gummibeer commented on September 7, 2024

https://github.com/spatie/enum/releases/tag/2.3.7

from enum.

dionysiosarvanitis avatar dionysiosarvanitis commented on September 7, 2024

Works great. Thanks!

from 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.