phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
BaseEnum.php
Go to the documentation of this file.
1 <?php
2 namespace Ajax\common;
3 
11 abstract class BaseEnum {
12 
13  private static $constCacheArray = NULL;
14 
15  private static $picked = [];
16 
17  public static function getConstants() {
18  if (self::$constCacheArray == NULL) {
19  self::$constCacheArray = [];
20  }
21  $calledClass = get_called_class();
22  if (! \array_key_exists($calledClass, self::$constCacheArray)) {
23  $reflect = new \ReflectionClass($calledClass);
24  self::$constCacheArray[$calledClass] = $reflect->getConstants();
25  }
26  return self::$constCacheArray[$calledClass];
27  }
28 
29  public static function getConstantValues($postFix = "", $prefixBefore = false) {
30  if ($postFix == "")
31  return \array_values(self::getConstants());
32  else {
33  if ($prefixBefore === false) {
34  return \array_map(function ($elem) use ($postFix) {
35  return $elem . " " . $postFix;
36  }, \array_values(self::getConstants()));
37  } else {
38  return \array_map(function ($elem) use ($postFix) {
39  return $postFix . " " . $elem;
40  }, \array_values(self::getConstants()));
41  }
42  }
43  }
44 
45  public static function isValidName($name, $strict = false) {
46  $constants = self::getConstants();
47 
48  if ($strict) {
49  return \array_key_exists($name, $constants);
50  }
51 
52  $keys = \array_map('strtolower', array_keys($constants));
53  return \in_array(\strtolower($name), $keys);
54  }
55 
56  public static function isValidValue($value) {
57  $values = \array_values(self::getConstants());
58  return \in_array($value, $values, true);
59  }
60 
61  public static function getRandomValue(bool $unique = false) {
62  $values = self::getConstantValues();
63  $count = \count($values);
64  $calledClass = \get_called_class();
65  if ($unique && $count > count(self::$picked[$calledClass] ?? [])) {
66  do {
67  $newVal = $values[\rand(0, $count - 1)];
68  } while (isset(self::$picked[$calledClass][$newVal]));
69  self::$picked[$calledClass][$newVal] = true;
70  return $newVal;
71  }
72  return $values[\rand(0, $count - 1)];
73  }
74 }
static getConstants()
Definition: BaseEnum.php:17
static $constCacheArray
Definition: BaseEnum.php:13
static getRandomValue(bool $unique=false)
Definition: BaseEnum.php:61
static isValidName($name, $strict=false)
Definition: BaseEnum.php:45
static getConstantValues($postFix="", $prefixBefore=false)
Definition: BaseEnum.php:29
Base class for enums see at http://stackoverflow.com/questions/254514/php-and-enumerations.
Definition: BaseEnum.php:11
static isValidValue($value)
Definition: BaseEnum.php:56