| 1 | <?php |
||
| 24 | abstract class Enumerable |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Get label of the enumerable |
||
| 28 | * |
||
| 29 | * @static |
||
| 30 | * |
||
| 31 | * @param $value |
||
| 32 | * |
||
| 33 | * @return mixed |
||
| 34 | */ |
||
| 35 | public static function get($value) |
||
| 36 | { |
||
| 37 | $list = static::data(); |
||
| 38 | |||
| 39 | return isset($list[$value]) ? $list[$value] : $value; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get list of the enumerable |
||
| 44 | * |
||
| 45 | * @static |
||
| 46 | * |
||
| 47 | * @param array $exclude |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | public static function listData(array $exclude = []): array |
||
| 52 | { |
||
| 53 | $list = static::data(); |
||
| 54 | foreach ($exclude as $item) { |
||
| 55 | unset($list[$item]); |
||
| 56 | } |
||
| 57 | |||
| 58 | return $list; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Check if value in into keys |
||
| 63 | * |
||
| 64 | * @param $value |
||
| 65 | * |
||
| 66 | * @return bool |
||
| 67 | */ |
||
| 68 | public static function inKeys($value): bool |
||
| 69 | { |
||
| 70 | return in_array($value, array_keys(static::listData([]))); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Check if value in into values |
||
| 75 | * |
||
| 76 | * @param $value |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public static function inValues($value): bool |
||
| 81 | { |
||
| 82 | return in_array($value, array_keys(static::listData([]))); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get list of the enumerable |
||
| 87 | * |
||
| 88 | * @static |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | abstract protected static function data(): array; |
||
| 93 | } |
||
| 94 |