Last active
June 16, 2020 12:22
-
-
Save pelmered/e701e9eaed04772b1176554af11d767b to your computer and use it in GitHub Desktop.
Enum classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Enums; | |
use App\Rules\IsValidEnum; | |
use Illuminate\Support\Str; | |
abstract class BaseEnum | |
{ | |
protected $name; | |
protected $values = array(); | |
abstract public static function options(); | |
public static function optionKeys() | |
{ | |
return array_keys(static::options()); | |
} | |
public static function getFormattedOption($enumKey) | |
{ | |
if (empty($enumKey)) { | |
return [ | |
'key' => null, | |
'name' => null, | |
]; | |
} | |
return [ | |
'key' => $enumKey, | |
'name' => static::optionName($enumKey), | |
]; | |
} | |
public static function getFormattedOptions(array $enumKeys = null) | |
{ | |
if (empty($enumKeys)) { | |
if (is_array($enumKeys)) { | |
return null; | |
} | |
$enumKeys = static::optionKeys(); | |
} | |
return array_map(fn ($enumKey) => static::getFormattedOption($enumKey), $enumKeys); | |
} | |
public static function optionName($enumValue) | |
{ | |
return static::options()[$enumValue]; | |
} | |
public static function optionNames(array $enumValues) | |
{ | |
return array_map(fn ($enumValue) => static::options()[$enumValue], $enumValues); | |
} | |
public static function lowercaseName($enumValue) | |
{ | |
return Str::lower(static::optionName($enumValue)); | |
} | |
public static function all() | |
{ | |
return array_keys(static::options()); | |
} | |
public static function validationRule() | |
{ | |
return new IsValidEnum(new static); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Rules; | |
use App\Enums\BaseEnum; | |
use Illuminate\Contracts\Validation\Rule; | |
class IsValidEnum implements Rule | |
{ | |
private BaseEnum $enum; | |
/** | |
* Create a new rule instance. | |
* | |
* @return void | |
*/ | |
public function __construct(BaseEnum $enum) | |
{ | |
$this->enum = $enum; | |
} | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
return in_array($value, $this->enum::all()); | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
return 'The selected :attribute is invalid. Possible values: "'.implode('", "', $this->enum::all()).'".'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Enums; | |
class UserStatus extends BaseEnum | |
{ | |
public const INCOMPLETE = 'incomplete'; | |
public const INACTIVE = 'inactive'; | |
public const ACTIVE = 'active'; | |
public const SUSPENDED = 'suspended'; | |
public static function options() | |
{ | |
return [ | |
self::INCOMPLETE => __('Incomplete'), | |
self::INACTIVE => __('Inactive'), | |
self::ACTIVE => __('Active'), | |
self::SUSPENDED => __('Suspended'), | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE: