1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of twLib |
4
|
|
|
* http://www.thomaswhiston.com |
5
|
|
|
* [email protected] |
6
|
|
|
* Created by PhpStorm. |
7
|
|
|
* User: Thomas Whiston |
8
|
|
|
* Date: 12/12/2015 |
9
|
|
|
* Time: 00:47 |
10
|
|
|
*/ |
11
|
|
|
namespace twhiston\twLib\Enum; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Enum |
15
|
|
|
* PHP Enum implementation |
16
|
|
|
* abstract class DaysOfWeek extends twLib\Enum { |
17
|
|
|
* const Sunday = 0; |
18
|
|
|
* const Monday = 1; |
19
|
|
|
* const Tuesday = 2; |
20
|
|
|
* const Wednesday = 3; |
21
|
|
|
* const Thursday = 4; |
22
|
|
|
* const Friday = 5; |
23
|
|
|
* const Saturday = 6; |
24
|
|
|
* } |
25
|
|
|
* @package twhiston\twLib |
26
|
|
|
*/ |
27
|
|
|
abstract class Enum |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class constants from reflection |
32
|
|
|
* @var null |
33
|
|
|
*/ |
34
|
|
|
private static $constCacheArray = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Enum constructor. |
38
|
|
|
* Private to prevent instantiation |
39
|
|
|
*/ |
40
|
|
|
private function __construct() |
41
|
|
|
{ |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Is this name valid?, accepts string |
46
|
|
|
* @param $name string |
47
|
|
|
* @param bool|FALSE $strict |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
1 |
|
public static function isValidName($name, $strict = false) |
51
|
|
|
{ |
52
|
1 |
|
$constants = self::getConstants(); |
53
|
|
|
|
54
|
1 |
|
if ($strict) { |
55
|
|
|
return array_key_exists($name, $constants); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$keys = array_map('strtolower', array_keys($constants)); |
59
|
|
|
|
60
|
1 |
|
return in_array(strtolower($name), $keys); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* get internal functions |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
3 |
|
private static function getConstants() |
68
|
|
|
{ |
69
|
3 |
|
if (self::$constCacheArray == null) { |
70
|
1 |
|
self::$constCacheArray = []; |
|
|
|
|
71
|
1 |
|
} |
72
|
3 |
|
$calledClass = get_called_class(); |
73
|
3 |
|
if (!array_key_exists($calledClass, self::$constCacheArray)) { |
74
|
1 |
|
$reflect = new \ReflectionClass($calledClass); |
75
|
1 |
|
self::$constCacheArray[$calledClass] = $reflect->getConstants(); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
3 |
|
return self::$constCacheArray[$calledClass]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Is the value valid for our enum, accepts int |
83
|
|
|
* @param $value |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
1 |
|
public static function isValidValue($value) |
87
|
|
|
{ |
88
|
1 |
|
$values = array_values(self::getConstants()); |
89
|
|
|
|
90
|
1 |
|
return in_array($value, $values, $strict = true); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the enum member name as a string from the value |
95
|
|
|
* @param $value |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
1 |
|
public static function getName($value) |
99
|
|
|
{ |
100
|
1 |
|
return array_search($value, self::getConstants()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..