| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
stubClassLoader::load('net::stubbles::lang::exceptions::stubIllegalArgumentException'); |
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
abstract class stubEnum extends stubBaseObject |
|---|
| 17 |
{ |
|---|
| 18 |
|
|---|
| 19 |
* name of the enum |
|---|
| 20 |
* |
|---|
| 21 |
* @var string |
|---|
| 22 |
*/ |
|---|
| 23 |
protected $name; |
|---|
| 24 |
|
|---|
| 25 |
* value of enum |
|---|
| 26 |
* |
|---|
| 27 |
* @var int |
|---|
| 28 |
*/ |
|---|
| 29 |
protected $value; |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
* constructor |
|---|
| 33 |
* |
|---|
| 34 |
* @param string $name |
|---|
| 35 |
* @param mixed $value optional |
|---|
| 36 |
*/ |
|---|
| 37 |
protected function __construct($name, $value = null) |
|---|
| 38 |
{ |
|---|
| 39 |
$this->name = $name; |
|---|
| 40 |
$this->value = ((null !== $value) ? ($value) : ($name)); |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
* forbid cloning of enums |
|---|
| 45 |
* |
|---|
| 46 |
* @throws stubRuntimeException |
|---|
| 47 |
*/ |
|---|
| 48 |
public final function __clone() |
|---|
| 49 |
{ |
|---|
| 50 |
throw new stubRuntimeException('Cloning of enums is not allowed.'); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
* returns the name of the enum |
|---|
| 55 |
* |
|---|
| 56 |
* @return string |
|---|
| 57 |
*/ |
|---|
| 58 |
public function name() |
|---|
| 59 |
{ |
|---|
| 60 |
return $this->name; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
* returns the value of the enum |
|---|
| 65 |
* |
|---|
| 66 |
* @return mixed |
|---|
| 67 |
*/ |
|---|
| 68 |
public function value() |
|---|
| 69 |
{ |
|---|
| 70 |
return $this->value; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
* returns the enum instance of given class identified by its name |
|---|
| 75 |
* |
|---|
| 76 |
* @param ReflectionClass $enum |
|---|
| 77 |
* @param string $name |
|---|
| 78 |
* @return stubEnum |
|---|
| 79 |
* @throws stubIllegalArgumentException |
|---|
| 80 |
*/ |
|---|
| 81 |
public static function forName(ReflectionClass $enum, $name) |
|---|
| 82 |
{ |
|---|
| 83 |
if ($enum->isSubclassOf(__CLASS__) === false) { |
|---|
| 84 |
throw new stubIllegalArgumentException('Given class is not an instance of ' . stubClassLoader::getFullQualifiedClassName(__CLASS__)); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
try { |
|---|
| 88 |
return $enum->getStaticPropertyValue($name); |
|---|
| 89 |
} catch (ReflectionException $re) { |
|---|
| 90 |
try { |
|---|
| 91 |
return $enum->getStaticPropertyValue(strtoupper($name)); |
|---|
| 92 |
} catch (ReflectionException $re) { |
|---|
| 93 |
throw new stubIllegalArgumentException($re->getMessage()); |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
* returns the enum instance of given class identified by its value |
|---|
| 100 |
* |
|---|
| 101 |
* @param ReflectionClass $enum |
|---|
| 102 |
* @param string $value |
|---|
| 103 |
* @return stubEnum |
|---|
| 104 |
* @throws stubIllegalArgumentException |
|---|
| 105 |
*/ |
|---|
| 106 |
public static function forValue(ReflectionClass $enum, $value) |
|---|
| 107 |
{ |
|---|
| 108 |
if ($enum->isSubclassOf(__CLASS__) === false) { |
|---|
| 109 |
throw new stubIllegalArgumentException('Given class is not an instance of ' . stubClassLoader::getFullQualifiedClassName(__CLASS__)); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
try { |
|---|
| 113 |
foreach ($enum->getStaticProperties() as $instance) { |
|---|
| 114 |
if ($instance->value() === $value) { |
|---|
| 115 |
return $instance; |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
} catch (ReflectionException $re) { |
|---|
| 119 |
throw new stubIllegalArgumentException($re->getMessage()); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
throw new stubIllegalArgumentException('Enum ' . stubClassLoader::getFullQualifiedClassName($enum->getName()) . ' for value ' . $value . ' does not exist.'); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
* returns a list of all instances for given enum |
|---|
| 127 |
* |
|---|
| 128 |
* @param ReflectionClass $enum |
|---|
| 129 |
* @return array<$enum->getName()> |
|---|
| 130 |
* @throws stubIllegalArgumentException |
|---|
| 131 |
*/ |
|---|
| 132 |
public static function instances(ReflectionClass $enum) |
|---|
| 133 |
{ |
|---|
| 134 |
if ($enum->isSubclassOf(__CLASS__) === false) { |
|---|
| 135 |
throw new stubIllegalArgumentException('Given class is not an instance of ' . stubClassLoader::getFullQualifiedClassName(__CLASS__)); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
return array_values($enum->getStaticProperties()); |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
* returns a list of enum names for given enum |
|---|
| 143 |
* |
|---|
| 144 |
* @param ReflectionClass $enum |
|---|
| 145 |
* @return array<string> |
|---|
| 146 |
* @throws stubIllegalArgumentException |
|---|
| 147 |
*/ |
|---|
| 148 |
public static function namesOf(ReflectionClass $enum) |
|---|
| 149 |
{ |
|---|
| 150 |
if ($enum->isSubclassOf(__CLASS__) === false) { |
|---|
| 151 |
throw new stubIllegalArgumentException('Given class is not an instance of ' . stubClassLoader::getFullQualifiedClassName(__CLASS__)); |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
return array_keys($enum->getStaticProperties()); |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
* returns a list of values for given enum |
|---|
| 159 |
* |
|---|
| 160 |
* @param ReflectionClass $enum |
|---|
| 161 |
* @return array<string,mixed> |
|---|
| 162 |
* @throws stubIllegalArgumentException |
|---|
| 163 |
*/ |
|---|
| 164 |
public static function valuesOf(ReflectionClass $enum) |
|---|
| 165 |
{ |
|---|
| 166 |
if ($enum->isSubclassOf(__CLASS__) === false) { |
|---|
| 167 |
throw new stubIllegalArgumentException('Given class is not an instance of ' . stubClassLoader::getFullQualifiedClassName(__CLASS__)); |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
$values = array(); |
|---|
| 171 |
foreach ($enum->getStaticProperties() as $name => $instance) { |
|---|
| 172 |
$values[$name] = $instance->value; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
return $values; |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
* checks whether a value is equal to the class |
|---|
| 180 |
* |
|---|
| 181 |
* @param mixed $compare |
|---|
| 182 |
* @return bool |
|---|
| 183 |
* @XMLIgnore |
|---|
| 184 |
*/ |
|---|
| 185 |
public function equals($compare) |
|---|
| 186 |
{ |
|---|
| 187 |
if ($compare instanceof stubEnum) { |
|---|
| 188 |
return ($compare->getClassName() === $this->getClassName() && $compare->name() === $this->name); |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
return false; |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
* returns a string representation of the class |
|---|
| 196 |
* |
|---|
| 197 |
* @return string |
|---|
| 198 |
* @XMLIgnore |
|---|
| 199 |
*/ |
|---|
| 200 |
public function __toString() |
|---|
| 201 |
{ |
|---|
| 202 |
$string = $this->getClassName() . " {\n"; |
|---|
| 203 |
$string .= ' ' . $this->name . "\n"; |
|---|
| 204 |
$string .= ' ' . $this->value . "\n"; |
|---|
| 205 |
$string .= "}\n"; |
|---|
| 206 |
return $string; |
|---|
| 207 |
} |
|---|
| 208 |
} |
|---|
| 209 |
?> |
|---|