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

BaseEnum   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 35
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConstants() 0 11 3
A getConstantValues() 0 3 1
A isValidName() 0 10 2
A isValidValue() 0 4 1
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
}