|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace vjik\enum; |
| 4 | + |
| 5 | +/** |
| 6 | + * Abstract Enum class |
| 7 | + * |
| 8 | + * @property mixed $value |
| 9 | + * @property mixed $name |
| 10 | + */ |
| 11 | +abstract class Enum |
| 12 | +{ |
| 13 | + |
| 14 | + |
| 15 | + /** |
| 16 | + * @var array |
| 17 | + */ |
| 18 | + protected static $_cache = []; |
| 19 | + |
| 20 | + |
| 21 | + /** |
| 22 | + * @var mixed |
| 23 | + */ |
| 24 | + protected $value; |
| 25 | + |
| 26 | + |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $name; |
| 31 | + |
| 32 | + |
| 33 | + /** |
| 34 | + * @param mixed $value |
| 35 | + * |
| 36 | + * @throws \UnexpectedValueException |
| 37 | + */ |
| 38 | + public function __construct($value) |
| 39 | + { |
| 40 | + if (!static::isValid($value)) { |
| 41 | + throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class()); |
| 42 | + } |
| 43 | + foreach (static::toArray()[$value] as $k => $v) { |
| 44 | + $this->$k = $v; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * Проверяет входит ли значение в допустимые |
| 51 | + * |
| 52 | + * @param $value |
| 53 | + * @param $filter |
| 54 | + * |
| 55 | + * @return bool |
| 56 | + */ |
| 57 | + public static function isValid($value, array $filter = []) |
| 58 | + { |
| 59 | + return in_array($value, static::toValues($filter), true); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + /** |
| 64 | + * Все доступные значения в виде массива с данными |
| 65 | + * |
| 66 | + * @param array $filter ['key' => 'value', ['operator', 'key', 'value'], …] |
| 67 | + * |
| 68 | + * @return array enum-значение - ключ, массив с данными - значение |
| 69 | + */ |
| 70 | + public static function toArray(array $filter = []) |
| 71 | + { |
| 72 | + $class = get_called_class(); |
| 73 | + if (!array_key_exists($class, static::$_cache)) { |
| 74 | + if (is_callable([$class, 'items'])) { |
| 75 | + /** @noinspection PhpUndefinedMethodInspection */ |
| 76 | + $items = $class::items(); |
| 77 | + } else { |
| 78 | + $items = []; |
| 79 | + $reflection = new \ReflectionClass($class); |
| 80 | + foreach ($reflection->getConstants() as $constant) { |
| 81 | + $items[$constant] = $constant; |
| 82 | + } |
| 83 | + } |
| 84 | + array_walk($items, function (&$item, $value) { |
| 85 | + $item = is_array($item) ? $item : ['name' => $item]; |
| 86 | + if (!isset($item['name'])) { |
| 87 | + $item['name'] = $value; |
| 88 | + } |
| 89 | + $item['value'] = $value; |
| 90 | + }); |
| 91 | + static::$_cache[$class] = $items; |
| 92 | + } |
| 93 | + $items = array_filter(static::$_cache[$class], function ($item) use ($filter) { |
| 94 | + foreach ($filter as $key => $filterItem) { |
| 95 | + if (is_int($key)) { |
| 96 | + list($operator, $key, $value) = $filterItem; |
| 97 | + switch ($operator) { |
| 98 | + case '=': |
| 99 | + if ($item[$key] != $value) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + break; |
| 103 | + |
| 104 | + case '!=': |
| 105 | + if ($item[$key] == $value) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + break; |
| 109 | + |
| 110 | + case '>': |
| 111 | + if ($item[$key] <= $value) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + break; |
| 115 | + |
| 116 | + case '<': |
| 117 | + if ($item[$key] >= $value) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + break; |
| 121 | + |
| 122 | + case '>=': |
| 123 | + if ($item[$key] < $value) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + break; |
| 127 | + |
| 128 | + case '<=': |
| 129 | + if ($item[$key] > $value) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + break; |
| 133 | + } |
| 134 | + } else { |
| 135 | + $value = $filterItem; |
| 136 | + if ($item[$key] != $value) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return true; |
| 142 | + }); |
| 143 | + return $items; |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + /** |
| 148 | + * Все доступные значения в виде массива |
| 149 | + * |
| 150 | + * @param array $filter |
| 151 | + * |
| 152 | + * @return array |
| 153 | + */ |
| 154 | + public static function toValues(array $filter = []) |
| 155 | + { |
| 156 | + return array_keys(static::toArray($filter)); |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + /** |
| 161 | + * Все доступные значение с именами |
| 162 | + * |
| 163 | + * @param array $filter |
| 164 | + * |
| 165 | + * @return array enum-значение - ключ, имя - значение |
| 166 | + */ |
| 167 | + public static function toList(array $filter = []) |
| 168 | + { |
| 169 | + $list = []; |
| 170 | + foreach (static::toArray($filter) as $id => $data) { |
| 171 | + $list[$id] = $data['name']; |
| 172 | + } |
| 173 | + return $list; |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + /** |
| 178 | + * @param $name |
| 179 | + * |
| 180 | + * @return mixed |
| 181 | + * |
| 182 | + * @throws \LogicException |
| 183 | + */ |
| 184 | + public function __get($name) |
| 185 | + { |
| 186 | + $getter = 'get' . $name; |
| 187 | + if (method_exists($this, $getter)) { |
| 188 | + return $this->$getter(); |
| 189 | + } elseif (property_exists($this, $name)) { |
| 190 | + return $this->{$name}; |
| 191 | + } |
| 192 | + throw new \LogicException('Getting unknown property: ' . get_class($this) . '::' . $name); |
| 193 | + } |
| 194 | + |
| 195 | + |
| 196 | + /** |
| 197 | + * @return string |
| 198 | + */ |
| 199 | + public function __toString() |
| 200 | + { |
| 201 | + return (string)$this->value; |
| 202 | + } |
| 203 | +} |
0 commit comments