-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathApbctConstant.php
More file actions
91 lines (79 loc) · 2.05 KB
/
ApbctConstant.php
File metadata and controls
91 lines (79 loc) · 2.05 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Cleantalk\ApbctWP;
class ApbctConstant
{
/**
* Accepted public names for the constants.
*
* @var string[]
*/
public $allowed_public_names = [];
/**
* Description of the constant.
*
* @var string
*/
public $description;
private $type;
private $defined_name = false;
public function __construct(array $allowed_public_names, $type, $description = '')
{
$this->allowed_public_names = $allowed_public_names;
$this->description = $description;
$this->type = $type;
$this->defined_name = $this->getDefinedName();
}
/**
* If defined, returns the name of the first defined constant from the allowed names. Return false if none of the constants are defined.
* @return string|false
*/
private function getDefinedName()
{
foreach ($this->allowed_public_names as $name) {
if (defined($name)) {
return $name;
}
}
return false;
}
/**
* If defined and type is correct.
* @return bool
*/
public function isDefinedAndTypeOK()
{
return $this->defined_name && gettype(constant($this->defined_name)) === $this->type;
}
/**
* Return the fact of definition
* @return bool
* @psalm-suppress PossiblyUnusedMethod
*/
public function isDefined()
{
return (bool)$this->defined_name;
}
/**
* Returns the value of the first defined constant from the allowed names. Return null if none of the constants are defined.
*
* @return mixed|null
*/
public function getValue()
{
if ($this->defined_name) {
return constant($this->defined_name);
}
return null;
}
/**
* @return array
*/
public function getData()
{
return array(
'is_defined' => $this->defined_name,
'value' => $this->getValue(),
'description' => $this->description,
);
}
}