Skip to content

Instantly share code, notes, and snippets.

@kenzouno1
Last active June 13, 2019 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenzouno1/f268debb3b11d552a66b9a972370b481 to your computer and use it in GitHub Desktop.
Save kenzouno1/f268debb3b11d552a66b9a972370b481 to your computer and use it in GitHub Desktop.
<?php
namespace App\Enums;
use BenSampo\Enum\Enum;
class Enums extends Enum
{
public static function toEnums(): array
{
$array = self::toArray();
$selectArray = [];
foreach ($array as $key => $value) {
$std = new \stdClass();
$std->label = static::getLabel($key);
$std->value = $value;
$std->name = $key;
$selectArray[] = $std;
}
return $selectArray;
}
public static function toOptions()
{
$array = self::toArray();
$selectArray = [];
foreach ($array as $key => $value) {
$selectArray[] = [
"name" => static::getKey($value),
"label" => static::getLabel($value),
"value" => $value,
];
}
return $selectArray;
}
public static function getLabel($key)
{
if (isset(static::$labels) && is_array(static::$labels)
&& array_key_exists($key, static::$labels)) {
return static::$labels[$key];
}
return $key;
}
public static function getLabelByValue($value){
$key = static::getKey($value);
return static::getLabel($key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment