/usr/share/php/MabeEnum/Enum.php is in php-enum 3.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | <?php
namespace MabeEnum;
use ReflectionClass;
use InvalidArgumentException;
use LogicException;
/**
* Class to implement enumerations for PHP 5 (without SplEnum)
*
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
* @copyright Copyright (c) 2017 Marc Bennewitz
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
*/
abstract class Enum
{
/**
* The selected enumerator value
*
* @var null|bool|int|float|string
*/
private $value;
/**
* The ordinal number of the enumerator
*
* @var null|int
*/
private $ordinal;
/**
* A map of enumerator names and values by enumeration class
*
* @var array ["$class" => ["$name" => $value, ...], ...]
*/
private static $constants = [];
/**
* A List of available enumerator names by enumeration class
*
* @var array ["$class" => ["$name0", ...], ...]
*/
private static $names = [];
/**
* Already instantiated enumerators
*
* @var array ["$class" => ["$name" => $instance, ...], ...]
*/
private static $instances = [];
/**
* Constructor
*
* @param null|bool|int|float|string $value The value of the enumerator
* @param int|null $ordinal The ordinal number of the enumerator
*/
final private function __construct($value, $ordinal = null)
{
$this->value = $value;
$this->ordinal = $ordinal;
}
/**
* Get the name of the enumerator
*
* @return string
* @see getName()
*/
public function __toString()
{
return $this->getName();
}
/**
* @throws LogicException Enums are not cloneable
* because instances are implemented as singletons
*/
final private function __clone()
{
throw new LogicException('Enums are not cloneable');
}
/**
* @throws LogicException Enums are not serializable
* because instances are implemented as singletons
*/
final public function __sleep()
{
throw new LogicException('Enums are not serializable');
}
/**
* @throws LogicException Enums are not serializable
* because instances are implemented as singletons
*/
final public function __wakeup()
{
throw new LogicException('Enums are not serializable');
}
/**
* Get the value of the enumerator
*
* @return null|bool|int|float|string
*/
final public function getValue()
{
return $this->value;
}
/**
* Get the name of the enumerator
*
* @return string
*/
final public function getName()
{
$ordinal = $this->ordinal !== null ? $this->ordinal : $this->getOrdinal();
return self::$names[static::class][$ordinal];
}
/**
* Get the ordinal number of the enumerator
*
* @return int
*/
final public function getOrdinal()
{
if ($this->ordinal === null) {
$ordinal = 0;
$value = $this->value;
foreach (self::detectConstants(static::class) as $constValue) {
if ($value === $constValue) {
break;
}
++$ordinal;
}
$this->ordinal = $ordinal;
}
return $this->ordinal;
}
/**
* Compare this enumerator against another and check if it's the same.
*
* @param mixed $enumerator
* @return bool
*/
final public function is($enumerator)
{
return $this === $enumerator || $this->value === $enumerator
// The following additional conditions are required only because of the issue of serializable singletons
|| ($enumerator instanceof static
&& \get_class($enumerator) === static::class
&& $enumerator->value === $this->value
);
}
/**
* Get an enumerator instance of the given enumerator value or instance
*
* @param static|null|bool|int|float|string $enumerator
* @return static
* @throws InvalidArgumentException On an unknwon or invalid value
* @throws LogicException On ambiguous constant values
*/
final public static function get($enumerator)
{
if ($enumerator instanceof static && \get_class($enumerator) === static::class) {
return $enumerator;
}
return static::byValue($enumerator);
}
/**
* Get an enumerator instance by the given value
*
* @param mixed $value
* @return static
* @throws InvalidArgumentException On an unknwon or invalid value
* @throws LogicException On ambiguous constant values
*/
final public static function byValue($value)
{
$constants = self::detectConstants(static::class);
$name = \array_search($value, $constants, true);
if ($name === false) {
throw new InvalidArgumentException(sprintf(
'Unknown value %s for enumeration %s',
\is_scalar($value)
? \var_export($value, true)
: 'of type ' . (\is_object($value) ? \get_class($value) : \gettype($value)),
static::class
));
}
if (!isset(self::$instances[static::class][$name])) {
self::$instances[static::class][$name] = new static($constants[$name]);
}
return self::$instances[static::class][$name];
}
/**
* Get an enumerator instance by the given name
*
* @param string $name The name of the enumerator
* @return static
* @throws InvalidArgumentException On an invalid or unknown name
* @throws LogicException On ambiguous values
*/
final public static function byName($name)
{
$name = (string) $name;
if (isset(self::$instances[static::class][$name])) {
return self::$instances[static::class][$name];
}
$const = static::class . '::' . $name;
if (!\defined($const)) {
throw new InvalidArgumentException($const . ' not defined');
}
return self::$instances[static::class][$name] = new static(\constant($const));
}
/**
* Get an enumeration instance by the given ordinal number
*
* @param int $ordinal The ordinal number or the enumerator
* @return static
* @throws InvalidArgumentException On an invalid ordinal number
* @throws LogicException On ambiguous values
*/
final public static function byOrdinal($ordinal)
{
$ordinal = (int) $ordinal;
if (!isset(self::$names[static::class])) {
self::detectConstants(static::class);
}
if (!isset(self::$names[static::class][$ordinal])) {
throw new InvalidArgumentException(\sprintf(
'Invalid ordinal number, must between 0 and %s',
\count(self::$names[static::class]) - 1
));
}
$name = self::$names[static::class][$ordinal];
if (isset(self::$instances[static::class][$name])) {
return self::$instances[static::class][$name];
}
$const = static::class . '::' . $name;
return self::$instances[static::class][$name] = new static(\constant($const), $ordinal);
}
/**
* Get a list of enumerator instances ordered by ordinal number
*
* @return static[]
*/
final public static function getEnumerators()
{
if (!isset(self::$names[static::class])) {
self::detectConstants(static::class);
}
return \array_map([static::class, 'byName'], self::$names[static::class]);
}
/**
* Get a list of enumerator values ordered by ordinal number
*
* @return mixed[]
*/
final public static function getValues()
{
return \array_values(self::detectConstants(static::class));
}
/**
* Get a list of enumerator names ordered by ordinal number
*
* @return string[]
*/
final public static function getNames()
{
if (!isset(self::$names[static::class])) {
self::detectConstants(static::class);
}
return self::$names[static::class];
}
/*
* Get a list of enumerator ordinal numbers
*
* @return int[]
*/
final public static function getOrdinals()
{
$count = \count(self::detectConstants(static::class));
return $count === 0 ? [] : \range(0, $count - 1);
}
/**
* Get all available constants of the called class
*
* @return array
* @throws LogicException On ambiguous constant values
*/
final public static function getConstants()
{
return self::detectConstants(static::class);
}
/**
* Is the given enumerator part of this enumeration
*
* @param static|null|bool|int|float|string $value
* @return bool
*/
final public static function has($value)
{
if ($value instanceof static && \get_class($value) === static::class) {
return true;
}
$constants = self::detectConstants(static::class);
return \in_array($value, $constants, true);
}
/**
* Detect all public available constants of given enumeration class
*
* @param string $class
* @return array
*/
private static function detectConstants($class)
{
if (!isset(self::$constants[$class])) {
$reflection = new ReflectionClass($class);
$publicConstants = [];
do {
$scopeConstants = [];
if (\PHP_VERSION_ID >= 70100) {
// Since PHP-7.1 visibility modifiers are allowed for class constants
// for enumerations we are only interested in public once.
foreach ($reflection->getReflectionConstants() as $reflConstant) {
if ($reflConstant->isPublic()) {
$scopeConstants[ $reflConstant->getName() ] = $reflConstant->getValue();
}
}
} else {
// In PHP < 7.1 all class constants were public by definition
$scopeConstants = $reflection->getConstants();
}
$publicConstants = $scopeConstants + $publicConstants;
} while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__);
assert(self::noAmbiguousValues($publicConstants));
self::$constants[$class] = $publicConstants;
self::$names[$class] = \array_keys($publicConstants);
}
return self::$constants[$class];
}
/**
* Assert that the given enumeration class doesn't define ambiguous enumerator values
* @param array $constants
* @return bool
*/
private static function noAmbiguousValues(array $constants)
{
foreach ($constants as $value) {
$names = \array_keys($constants, $value, true);
if (\count($names) > 1) {
return false;
}
}
return true;
}
/**
* Get an enumerator instance by the given name.
*
* This will be called automatically on calling a method
* with the same name of a defined enumerator.
*
* @param string $method The name of the enumeraotr (called as method)
* @param array $args There should be no arguments
* @return static
* @throws InvalidArgumentException On an invalid or unknown name
* @throws LogicException On ambiguous constant values
*/
final public static function __callStatic($method, array $args)
{
return self::byName($method);
}
}
|