|
1 | 1 | <?php
|
2 |
| - |
3 | 2 | namespace Ajax\common;
|
4 | 3 |
|
5 | 4 | /**
|
6 | 5 | * Base class for enums
|
7 | 6 | * see at http://stackoverflow.com/questions/254514/php-and-enumerations
|
| 7 | + * |
8 | 8 | * @author jc
|
9 | 9 | *
|
10 | 10 | */
|
11 | 11 | abstract class BaseEnum {
|
12 |
| - private static $constCacheArray=NULL; |
| 12 | + |
| 13 | + private static $constCacheArray = NULL; |
| 14 | + |
| 15 | + private static $picked = []; |
13 | 16 |
|
14 | 17 | public static function getConstants() {
|
15 | 18 | if (self::$constCacheArray == NULL) {
|
16 |
| - self::$constCacheArray=[ ]; |
| 19 | + self::$constCacheArray = []; |
17 | 20 | }
|
18 |
| - $calledClass=get_called_class(); |
19 |
| - if (!array_key_exists($calledClass, self::$constCacheArray)) { |
20 |
| - $reflect=new \ReflectionClass($calledClass); |
21 |
| - self::$constCacheArray[$calledClass]=$reflect->getConstants(); |
| 21 | + $calledClass = get_called_class(); |
| 22 | + if (! \array_key_exists($calledClass, self::$constCacheArray)) { |
| 23 | + $reflect = new \ReflectionClass($calledClass); |
| 24 | + self::$constCacheArray[$calledClass] = $reflect->getConstants(); |
22 | 25 | }
|
23 | 26 | return self::$constCacheArray[$calledClass];
|
24 | 27 | }
|
25 | 28 |
|
26 |
| - public static function getConstantValues($postFix="",$prefixBefore=false) { |
| 29 | + public static function getConstantValues($postFix = "", $prefixBefore = false) { |
27 | 30 | if ($postFix == "")
|
28 | 31 | return \array_values(self::getConstants());
|
29 | 32 | else {
|
30 |
| - if($prefixBefore===false){ |
31 |
| - return \array_map(function ($elem) use($postFix) { |
| 33 | + if ($prefixBefore === false) { |
| 34 | + return \array_map(function ($elem) use ($postFix) { |
32 | 35 | return $elem . " " . $postFix;
|
33 | 36 | }, \array_values(self::getConstants()));
|
34 |
| - }else{ |
35 |
| - return \array_map(function ($elem) use($postFix) { |
36 |
| - return $postFix." ".$elem; |
| 37 | + } else { |
| 38 | + return \array_map(function ($elem) use ($postFix) { |
| 39 | + return $postFix . " " . $elem; |
37 | 40 | }, \array_values(self::getConstants()));
|
38 | 41 | }
|
39 | 42 | }
|
40 | 43 | }
|
41 | 44 |
|
42 |
| - public static function isValidName($name, $strict=false) { |
43 |
| - $constants=self::getConstants(); |
| 45 | + public static function isValidName($name, $strict = false) { |
| 46 | + $constants = self::getConstants(); |
44 | 47 |
|
45 | 48 | if ($strict) {
|
46 |
| - return array_key_exists($name, $constants); |
| 49 | + return \array_key_exists($name, $constants); |
47 | 50 | }
|
48 | 51 |
|
49 |
| - $keys=array_map('strtolower', array_keys($constants)); |
50 |
| - return in_array(strtolower($name), $keys); |
| 52 | + $keys = \array_map('strtolower', array_keys($constants)); |
| 53 | + return \in_array(\strtolower($name), $keys); |
51 | 54 | }
|
52 | 55 |
|
53 | 56 | public static function isValidValue($value) {
|
54 |
| - $values=array_values(self::getConstants()); |
55 |
| - return in_array($value, $values, true); |
| 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 | + if ($unique && $count > count(self::$picked)) { |
| 65 | + do { |
| 66 | + $newVal = $values[\rand(0, $count - 1)]; |
| 67 | + } while (isset(self::$picked[$newVal])); |
| 68 | + self::$picked[$newVal] = true; |
| 69 | + return $newVal; |
| 70 | + } |
| 71 | + return $values[\rand(0, $count - 1)]; |
56 | 72 | }
|
57 | 73 | }
|
0 commit comments