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

Add $hidden property to Enums #331

Closed
TanisukeGoro opened this issue Aug 2, 2023 · 1 comment
Closed

Add $hidden property to Enums #331

TanisukeGoro opened this issue Aug 2, 2023 · 1 comment

Comments

@TanisukeGoro
Copy link

TanisukeGoro commented Aug 2, 2023

Thank you for offering this convenient package.

Problem

In long-term projects, Enums can accumulate values that are no longer used. Although these values can be deleted, sometimes they can't be because it may affect other systems. In particular, in Laravel-Enum, trying to create an instance with an undefined value causes an error.

Proposed Solution

I propose to add a new $hidden property to Laravel-Enum classes to hide unwanted enum values. This property is inspired by the $hidden attribute in Laravel's Eloquent Model.

Example Usage:

<?php

declare(strict_types=1);

namespace App\Enums;

use BenSampo\Enum\Enum;
use BenSampo\Enum\Contracts\LocalizedEnum;

/**
 * @method static static Administrator()
 * @method static static Moderator()
 * @method static static Subscriber()
 * @method static static SuperAdministrator()
 *
 * @extends Enum<string>
 */
final class UserType extends Enum implements LocalizedEnum
{
    public const Administrator = 'administrator';
    public const Moderator = 'moderator';
    public const Subscriber = 'subscriber';
    public const SuperAdministrator = 'super_administrator';

    // add hidden property
    protected $hidden = [
        self::Moderator,
        self::Subscriber
    ];
}
# before
UserType::asSelectArray()
array:4 [
    "administrator" => "administrator description",
    "moderator" => "moderator description",
    "subscriber" => "subscriber description",
    "super_administrator" => "super administrator description",
  ]
(※ ↑ assuming the description is defined in enums.php)

# after
UserType::asSelectArray();
array:4 [
    "administrator" => "administrator description",
    "super_administrator" => "super administrator description",
]

This will allow us to hide values that are no longer needed, making the Enums more manageable within the project. In the future, providing makeHidden and makeVisible methods would allow dynamic toggling of visibility.

Preliminary Implementation

I have implemented a basic version of this feature in my fork of the repository, which can be found here. This implementation is not final, but it demonstrates how the $hidden property can be used.

I would like to discuss this feature and continue the implementation with feedback and guidance from the community.

@spawnia
Copy link
Collaborator

spawnia commented Feb 14, 2024

Closing, as I no longer plan to develop this library further - see #332.

@spawnia spawnia closed this as not planned Won't fix, can't repro, duplicate, stale Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants