-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enum.php
169 lines (158 loc) · 5.6 KB
/
Enum.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* Copyright (c) 2012 Szurovecz János
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace precore\lang;
use InvalidArgumentException;
use ReflectionProperty;
/**
* Extending this class will simulate an enum known in several languages.
* The following rules must be followed:
* - use public static variables
* - call init() method right after the class definition
*
* All public static variables will be an instance of your class.
* The instances stores the given name as a string
* which can be obtained through name() method.
*
* For example:
*
* <pre>
* class Color extends Enum
* {
* public static $RED;
* public static $BLUE;
* }
* Color::init();
* </pre>
*
* In this case Color::$RED will be a Color instance and its name() method will return 'RED'.
*
* All public static variables declared in your class will be used as a possible enum constant!
*
* Initializing objects can be done in constructor, which is recommended to defined as private or protected.
* Constructor parameters (if any) can be defined by constructorArgs() static method.
*
* @author Szurovecz János <szjani@szjani.hu>
*/
class Enum extends Object
{
private static $cache = array();
private $name;
/**
* Override if you want to pass any arguments to the constructor defined in your class.
*
* @return array Keys must be the names, values are arrays storing constructor parameters
*
* For example:
*
* class Color extends Enum
* {
* public static $RED;
*
* private $hexValue;
*
* protected function __construct($hexValue)
* {
* $this->hexValue = $hexValue;
* }
*
* protected static function constructorArgs()
* {
* return array('RED' => array('#ff0000'));
* }
* }
*/
protected static function constructorArgs()
{
return array();
}
private static function newInstance($name, array $constructorArgs)
{
$reflectionClass = static::objectClass();
$obj = $reflectionClass->newInstanceWithoutConstructor();
$obj->name = $name;
$constructor = $reflectionClass->getConstructor();
if ($constructor !== null) {
$constructor->setAccessible(true);
$numOfParams = $constructor->getNumberOfParameters();
if ($numOfParams == 0) {
$constructor->invoke($obj);
} else {
if (!array_key_exists($name, $constructorArgs) || !is_array($constructorArgs[$name])
|| $numOfParams !== count($constructorArgs[$name])) {
throw new InvalidArgumentException(
sprintf('Invalid arguments are provided for constructor in %s:$%s', static::className(), $name)
);
}
$constructor->invokeArgs($obj, $constructorArgs[$name]);
}
}
return $obj;
}
/**
* Must be called after your class definition!
*/
public static function init()
{
$className = static::className();
self::$cache[$className] = array();
$reflectionClass = static::objectClass();
$constructorParams = static::constructorArgs();
foreach ($reflectionClass->getStaticProperties() as $name => $value) {
$property = new ReflectionProperty($className, $name);
if ($property->isPublic()) {
$instance = static::newInstance($name, $constructorParams);
static::objectClass()->setStaticPropertyValue($name, $instance);
self::$cache[$className][$name] = $instance;
}
}
}
/**
* You can obtain the appropriate instance by their name.
*
* @param string $name
* @return Enum
* @throws InvalidArgumentException
*/
public static function valueOf($name)
{
$className = static::className();
if (!array_key_exists($className, self::$cache) || !array_key_exists($name, self::$cache[$className])) {
throw new InvalidArgumentException("The enum '$className' type has no constant with name '$name'");
}
return self::$cache[$className][$name];
}
public static function values()
{
return array_key_exists(static::className(), self::$cache)
? self::$cache[static::className()]
: array();
}
public function equals(ObjectInterface $object = null)
{
return $object instanceof static && $object->name === $this->name;
}
public function name()
{
return $this->name;
}
}