1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Enum; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Equatable\EquatableInterface; |
15
|
|
|
use Cubiche\Core\Hashable\HashCoder; |
16
|
|
|
use MyCLabs\Enum\Enum as BaseEnum; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Enum class. |
20
|
|
|
* |
21
|
|
|
* @method static static __DEFAULT() |
22
|
|
|
* |
23
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class Enum extends BaseEnum implements EquatableInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected static $defaultCache = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param mixed $value |
34
|
|
|
* |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public function is($value) |
38
|
|
|
{ |
39
|
|
|
return $this->value === $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function equals($other) |
46
|
|
|
{ |
47
|
|
|
return \get_class($this) === \get_class($other) && $this->getValue() === $other->getValue(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function hashCode() |
54
|
|
|
{ |
55
|
|
|
return HashCoder::defaultHashCoder()->hashCode($this->getValue()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Enum $enum |
60
|
|
|
* @param Enum $default |
61
|
|
|
* |
62
|
|
|
* @throws \InvalidArgumentException |
63
|
|
|
* |
64
|
|
|
* @return static |
65
|
|
|
*/ |
66
|
|
|
public static function ensure(Enum $enum = null, Enum $default = null) |
67
|
|
|
{ |
68
|
|
|
if ($enum instanceof static) { |
69
|
|
|
return $enum; |
70
|
|
|
} |
71
|
|
|
if ($enum === null) { |
72
|
|
|
return $default === null ? static::__DEFAULT() : static::ensure($default); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
throw new \InvalidArgumentException(\sprintf('The enum parameter must be a %s instance', static::class)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $name |
80
|
|
|
* @param array $arguments |
81
|
|
|
* |
82
|
|
|
* @return static |
83
|
|
|
* |
84
|
|
|
* @throws \BadMethodCallException |
85
|
|
|
*/ |
86
|
|
|
public static function __callStatic($name, $arguments) |
87
|
|
|
{ |
88
|
|
|
$array = static::toArray(); |
89
|
|
|
if (\strtoupper($name) === '__DEFAULT' && isset(static::$defaultCache[static::class])) { |
90
|
|
|
return new static(static::$defaultCache[static::class]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (isset($array[$name])) { |
94
|
|
|
return new static($array[$name]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
throw new \BadMethodCallException("No static method or enum constant '$name' in class ".static::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public static function toArray() |
104
|
|
|
{ |
105
|
|
|
if (!isset(static::$cache[static::class])) { |
106
|
|
|
static::$cache[static::class] = self::constants(static::class); |
107
|
|
|
if (!isset(static::$defaultCache[static::class]) && !empty(static::$cache[static::class])) { |
108
|
|
|
static::$defaultCache[static::class] = \array_values(static::$cache[static::class])[0]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return static::$cache[static::class]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $class |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
private static function constants($class) |
121
|
|
|
{ |
122
|
|
|
$reflection = new \ReflectionClass($class); |
123
|
|
|
$constants = $reflection->getConstants(); |
124
|
|
|
foreach ($constants as $name => $value) { |
125
|
|
|
if (\strtoupper($name) === '__DEFAULT') { |
126
|
|
|
static::$defaultCache[$class] = $value; |
127
|
|
|
unset($constants[$name]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $constants; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|