-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathConstantHelper.php
61 lines (54 loc) · 1.84 KB
/
ConstantHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace mgcode\helpers;
class ConstantHelper
{
/**
* Checks if a value exists in constants for given class
* @param string $constant Constant value
* @param string|object $class The class name or instance
* @param string $searchPattern Constants prefix. By default returns all constants.
* @static
* @return bool
*/
public static function valueInConstants($constant, $class, $searchPattern = null)
{
$constants = self::getConstantList($class, $searchPattern);
return (bool) array_search($constant, $constants);
}
/**
* Get constants filtered by pattern in a class
* @param string|object $class The class name or instance
* @param string $searchPattern Constants prefix. By default returns all constants.
* @static
* @return array
*/
public static function getConstantList($class, $searchPattern = null)
{
if (is_object($class)) {
$class = get_class($class);
}
$r = new \ReflectionClass($class);
$constants = $r->getConstants();
if ($searchPattern !== null) {
foreach ($constants as $key => $value) {
if (strpos($key, $searchPattern) !== 0) {
unset($constants[$key]);
}
}
}
return $constants;
}
/**
* Checks for a constant existence in a class
* @param string $constant The constant key
* @param string|object $class The class name or instance
* @param string $searchPattern Constants prefix. By default returns all constants.
* @static
* @return bool
*/
public static function keyExists($constant, $class, $searchPattern = null)
{
$constants = self::getConstantList($class, $searchPattern);
return array_key_exists($constant, $constants);
}
}