Completed
Push — master ( cd0fd2...97e05d )
by Jean-Christophe
03:14
created

BaseEnum::getConstantValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ajax\common;
4
5
/**
6
 * Base class for enums
7
 * see at http://stackoverflow.com/questions/254514/php-and-enumerations
8
 * @author jc
9
 *
10
 */
11
abstract class BaseEnum {
12
	private static $constCacheArray=NULL;
13
14
	public static function getConstants() {
15
		if (self::$constCacheArray==NULL) {
16
			self::$constCacheArray=[ ];
17
		}
18
		$calledClass=get_called_class();
19
		if (!array_key_exists($calledClass, self::$constCacheArray)) {
20
			$reflect=new \ReflectionClass($calledClass);
21
			self::$constCacheArray [$calledClass]=$reflect->getConstants();
22
		}
23
		return self::$constCacheArray [$calledClass];
24
	}
25
26
	public static function getConstantValues(){
27
		return \array_values(self::getConstants());
28
	}
29
30
	public static function isValidName($name, $strict=false) {
31
		$constants=self::getConstants();
32
33
		if ($strict) {
34
			return array_key_exists($name, $constants);
35
		}
36
37
		$keys=array_map('strtolower', array_keys($constants));
38
		return in_array(strtolower($name), $keys);
39
	}
40
41
	public static function isValidValue($value) {
42
		$values=array_values(self::getConstants());
43
		return in_array($value, $values, true);
44
	}
45
}