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

Need a toArray() fn that returns enum descriptions #21

Closed
rsubr opened this issue Aug 8, 2018 · 5 comments
Closed

Need a toArray() fn that returns enum descriptions #21

rsubr opened this issue Aug 8, 2018 · 5 comments

Comments

@rsubr
Copy link

rsubr commented Aug 8, 2018

@BenSampo thanks a ton for this module and your blog post, using enums has greatly simplified my code.

I'm using enums to populate html select fields and toArray() almost satisfies my requirement. However, populating select field values with enum descriptions is more readable than using enum keys.

eg. Instead of SADMIN, AUTHOR, the select field would show 'Super Administrator', 'Blog Author'.

I'm now repeating the same toArrayDescription() function in all of my Enum classes as shown below. Can we have a toArrayDescription() added to laravel-enum to address this use case?

final class UserType extends Enum
{
    const SADMIN = 0;
    const AUTHOR = 1;


    /**
     * Get the description for an enum value
     *
     * @param $value
     * @return string
     */
    public static function getDescription($value): string
    {
        switch ($value) {
            case self::SADMIN:
                return 'Super admin';
            case self::AUTHOR:
                return 'Blog author';
        }
        return self::getKey($value);
    }

// returns ['Super Admin' => 0, 'Blog Author', => 1]
    public static function toArrayDescription()
    {
        $arr = [];

        foreach (self::getValues() as $v) {
            $arr[self::getDescription($v)] = $v;
        }
        return $arr;
    }
}
@BenSampo
Copy link
Owner

BenSampo commented Aug 9, 2018

@rsubr Yes, this looks good to me. I'll add something like it in when I get a chance.

I was also thinking about making a better default for the getDescription method where it could guess a description for a key name e.g. SuperAdministrator would become Super administrator.

Glad you find the package useful 👍

@BenSampo
Copy link
Owner

@rsubr I've added a toSelectArray method. If you have any other methods that you often add to an enum, I've made the base Enum class macroable so you can extend it as you wish within your application.

https://github.com/BenSampo/laravel-enum#extending-the-enum-base-class

@rsubr
Copy link
Author

rsubr commented Aug 10, 2018

@BenSampo works great! 🙏

Thanks for making it extensible.

You have even updated the project documentation! 😲 🏆

@rsubr
Copy link
Author

rsubr commented Aug 10, 2018

Further testing shows that getDescription method defined in my UserType child class does not get called by toSelectArray. It's calling the method defined in the Enum base class function instead.

In the toSelectArray method, can you please change self::getDescription to static::getDescription so the overridden getDescription from the child class will be called?

There's the code that needs to be changed:

foreach ($array as $key => $value) {
//  $selectArray[$value] = self::getDescription($value);
    $selectArray[$value] = static::getDescription($value);
}

@BenSampo
Copy link
Owner

@rsubr Yeah well spotted - have just pushed a fix for that.

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

No branches or pull requests

2 participants