Last active
June 13, 2019 10:08
-
-
Save kenzouno1/f268debb3b11d552a66b9a972370b481 to your computer and use it in GitHub Desktop.
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 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