Skip to content

Commit d6745cd

Browse files
committed
[1.0.0] Первый коммит
0 parents  commit d6745cd

14 files changed

+2077
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# PhpStorm project files
2+
.idea
3+
4+
# Composer vendor dir
5+
/vendor

Enum.php

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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, $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($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 => $value) {
95+
if (is_int($key)) {
96+
$operator = $value[0];
97+
$key = $value[1];
98+
$value = $value[2];
99+
switch ($operator) {
100+
case '=':
101+
if ($item[$key] != $value) {
102+
return false;
103+
}
104+
break;
105+
106+
case '!=':
107+
if ($item[$key] == $value) {
108+
return false;
109+
}
110+
break;
111+
112+
case '>':
113+
if ($item[$key] <= $value) {
114+
return false;
115+
}
116+
break;
117+
118+
case '<':
119+
if ($item[$key] >= $value) {
120+
return false;
121+
}
122+
break;
123+
124+
case '>=':
125+
if ($item[$key] < $value) {
126+
return false;
127+
}
128+
break;
129+
130+
case '<=':
131+
if ($item[$key] > $value) {
132+
return false;
133+
}
134+
break;
135+
}
136+
} else {
137+
if ($item[$key] != $value) {
138+
return false;
139+
}
140+
}
141+
}
142+
return true;
143+
});
144+
return $items;
145+
}
146+
147+
148+
/**
149+
* Все доступные значения в виде массива
150+
*
151+
* @param array $filter
152+
*
153+
* @return array
154+
*/
155+
public static function toValues($filter = [])
156+
{
157+
return array_keys(static::toArray($filter));
158+
}
159+
160+
161+
/**
162+
* Все доступные значение с именами
163+
*
164+
* @param array $filter
165+
*
166+
* @return array enum-значение - ключ, имя - значение
167+
*/
168+
public static function toList($filter = [])
169+
{
170+
$list = [];
171+
foreach (static::toArray($filter) as $id => $data) {
172+
$list[$id] = $data['name'];
173+
}
174+
return $list;
175+
}
176+
177+
178+
/**
179+
* @param $name
180+
*
181+
* @return mixed
182+
*
183+
* @throws \LogicException
184+
*/
185+
public function __get($name)
186+
{
187+
$getter = 'get' . $name;
188+
if (method_exists($this, $getter)) {
189+
return $this->$getter();
190+
} elseif (property_exists($this, $name)) {
191+
return $this->{$name};
192+
}
193+
throw new \LogicException('Getting unknown property: ' . get_class($this) . '::' . $name);
194+
}
195+
196+
197+
/**
198+
* @return string
199+
*/
200+
public function __toString()
201+
{
202+
return (string)$this->value;
203+
}
204+
}

LICENSE.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2017 Sergey Predvoditelev
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
6+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
7+
to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of
10+
the Software.
11+
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
13+
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
14+
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
15+
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)