Skip to content

Commit 6803ebe

Browse files
committed
update baseEnum, add getRandomValue
1 parent 0106e06 commit 6803ebe

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

Ajax/common/BaseEnum.php

+36-20
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,73 @@
11
<?php
2-
32
namespace Ajax\common;
43

54
/**
65
* Base class for enums
76
* see at http://stackoverflow.com/questions/254514/php-and-enumerations
7+
*
88
* @author jc
99
*
1010
*/
1111
abstract class BaseEnum {
12-
private static $constCacheArray=NULL;
12+
13+
private static $constCacheArray = NULL;
14+
15+
private static $picked = [];
1316

1417
public static function getConstants() {
1518
if (self::$constCacheArray == NULL) {
16-
self::$constCacheArray=[ ];
19+
self::$constCacheArray = [];
1720
}
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();
2225
}
2326
return self::$constCacheArray[$calledClass];
2427
}
2528

26-
public static function getConstantValues($postFix="",$prefixBefore=false) {
29+
public static function getConstantValues($postFix = "", $prefixBefore = false) {
2730
if ($postFix == "")
2831
return \array_values(self::getConstants());
2932
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) {
3235
return $elem . " " . $postFix;
3336
}, \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;
3740
}, \array_values(self::getConstants()));
3841
}
3942
}
4043
}
4144

42-
public static function isValidName($name, $strict=false) {
43-
$constants=self::getConstants();
45+
public static function isValidName($name, $strict = false) {
46+
$constants = self::getConstants();
4447

4548
if ($strict) {
46-
return array_key_exists($name, $constants);
49+
return \array_key_exists($name, $constants);
4750
}
4851

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);
5154
}
5255

5356
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)];
5672
}
5773
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace Ajax\semantic\html\base\constants\icons;
3+
4+
use Ajax\common\BaseEnum;
5+
6+
abstract class Animals extends BaseEnum {
7+
8+
const CAT = 'cat', CROW = 'crow', DOG = 'dog', DOVE = 'dove', DRAGON = 'dragon', FEATHER = 'feather', FEATHER_ALTERNATE = 'feather alternate', FISH = 'fish', FOG = 'frog', HIPPO = 'hippo', HORSE = 'horse', HORSE_HEAD = 'horse head', KIWI_BIRD = 'kiwi bird', OTTER = 'otter', PAW = 'paw', SPIDER = 'spider';
9+
}

0 commit comments

Comments
 (0)