You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
897 B
27 lines
897 B
<?php
|
|
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Str;
|
|
|
|
if (! function_exists('enum')) {
|
|
function enum($key)
|
|
{
|
|
// add a dot at the end of string to prevent undefined offset
|
|
$key .= Str::of($key)->contains(".") ? "" : ".";
|
|
|
|
// the first parameter of all enum keys are its filename
|
|
[$filename, $key] = explode(".", $key, 2);
|
|
|
|
// because we do not want to load the file every time use require
|
|
$enums = require app_path("Enums/$filename.php");
|
|
|
|
// if the key that user provided not exists then null return
|
|
$enums = Arr::get($enums, $key, null);
|
|
|
|
// if enum null means that key not found
|
|
throw_if($enums === null, 'Exception', "Undefined enum '{$key}'");
|
|
|
|
// if enum value is array its mean that user want to use it as collection
|
|
return is_array($enums) ? collect($enums) : $enums;
|
|
}
|
|
}
|