1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Workflow library. |
5
|
|
|
* |
6
|
|
|
* @package workflow |
7
|
|
|
* @author David Molineus <[email protected]> |
8
|
|
|
* @copyright 2014-2017 netzmacht David Molineus |
9
|
|
|
* @license LGPL 3.0 https://github.com/netzmacht/workflow |
10
|
|
|
* @filesource |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace Netzmacht\Workflow\Util; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Comparison is an util class to allow value comparison by passing two values. |
19
|
|
|
* |
20
|
|
|
* @package Netzmacht\Workflow\Util |
21
|
|
|
*/ |
22
|
|
|
final class Comparison |
23
|
|
|
{ |
24
|
|
|
const EQUALS = '=='; |
25
|
|
|
const IDENTICAL = '==='; |
26
|
|
|
const NOT_EQUALS = '!='; |
27
|
|
|
const NOT_IDENTICAL = '!=='; |
28
|
|
|
const GREATER_THAN = '>'; |
29
|
|
|
const LESSER_THAN = '<'; |
30
|
|
|
const LESSER_THAN_OR_EQUALS = '<='; |
31
|
|
|
const GREATER_THAN_OR_EQUALS = '>='; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Operation method mapping cache. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private static $operators; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Compare two values. |
42
|
|
|
* |
43
|
|
|
* @param mixed $valueA Value a. |
44
|
|
|
* @param mixed $valueB Value b. |
45
|
|
|
* @param string $operator The operator for the comparison. |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public static function compare($valueA, $valueB, string $operator): bool |
50
|
|
|
{ |
51
|
|
|
$method = self::getOperatorMethod($operator); |
52
|
|
|
|
53
|
|
|
if ($method) { |
54
|
|
|
return call_user_func([get_called_class(), $method], $valueA, $valueB); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Consider if two values equals. |
62
|
|
|
* |
63
|
|
|
* @param mixed $valueA Value a. |
64
|
|
|
* @param mixed $valueB Value b. |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public static function equals($valueA, $valueB): bool |
69
|
|
|
{ |
70
|
|
|
return $valueA == $valueB; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Consider if both values are identical. |
75
|
|
|
* |
76
|
|
|
* It uses the === operator of php. |
77
|
|
|
* |
78
|
|
|
* @param mixed $valueA Value a. |
79
|
|
|
* @param mixed $valueB Value b. |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public static function identical($valueA, $valueB): bool |
84
|
|
|
{ |
85
|
|
|
return $valueA === $valueB; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Consider if two values not equals. |
90
|
|
|
* |
91
|
|
|
* @param mixed $valueA Value a. |
92
|
|
|
* @param mixed $valueB Value b. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public static function notEquals($valueA, $valueB): bool |
97
|
|
|
{ |
98
|
|
|
return !static::equals($valueA, $valueB); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Consider if two values are not identical. |
103
|
|
|
* |
104
|
|
|
* @param mixed $valueA Value a. |
105
|
|
|
* @param mixed $valueB Value b. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public static function notIdentical($valueA, $valueB): bool |
110
|
|
|
{ |
111
|
|
|
return !static::identical($valueA, $valueB); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Consider if value a is greater than value b. |
116
|
|
|
* |
117
|
|
|
* @param mixed $valueA Value a. |
118
|
|
|
* @param mixed $valueB Value b. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public static function greaterThan($valueA, $valueB): bool |
123
|
|
|
{ |
124
|
|
|
return $valueA > $valueB; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Consider if value a is greater than or equals value b. |
129
|
|
|
* |
130
|
|
|
* @param mixed $valueA Value a. |
131
|
|
|
* @param mixed $valueB Value b. |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public static function greaterThanOrEquals($valueA, $valueB): bool |
136
|
|
|
{ |
137
|
|
|
return $valueA >= $valueB; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Consider if value a is lesser than value b. |
142
|
|
|
* |
143
|
|
|
* @param mixed $valueA Value a. |
144
|
|
|
* @param mixed $valueB Value b. |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public static function lesserThan($valueA, $valueB): bool |
149
|
|
|
{ |
150
|
|
|
return $valueA < $valueB; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Consider if value a is lesser than or equals value b. |
155
|
|
|
* |
156
|
|
|
* @param mixed $valueA Value a. |
157
|
|
|
* @param mixed $valueB Value b. |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public static function lesserThanOrEquals($valueA, $valueB): bool |
162
|
|
|
{ |
163
|
|
|
return $valueA <= $valueB; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get operator method. Returns false if metod not set. |
168
|
|
|
* |
169
|
|
|
* @param string $operator The current operator. |
170
|
|
|
* |
171
|
|
|
* @return string|bool |
172
|
|
|
*/ |
173
|
|
|
private static function getOperatorMethod(string $operator) |
174
|
|
|
{ |
175
|
|
|
$operators = self::getOperators(); |
176
|
|
|
|
177
|
|
|
if (isset($operators[$operator])) { |
178
|
|
|
return $operators[$operator]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return false; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get operator method mapping. |
186
|
|
|
* |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
private static function getOperators(): array |
190
|
|
|
{ |
191
|
|
|
if (!is_array(self::$operators)) { |
192
|
|
|
$reflector = new \ReflectionClass(get_called_class()); |
193
|
|
|
$constants = $reflector->getConstants(); |
194
|
|
|
$operators = array(); |
195
|
|
|
|
196
|
|
|
foreach ($constants as $name => $operator) { |
197
|
|
|
$parts = explode('_', $name); |
198
|
|
|
$parts = array_map( |
199
|
|
|
function ($item) { |
200
|
|
|
$item = strtolower($item); |
201
|
|
|
$item = ucfirst($item); |
202
|
|
|
|
203
|
|
|
return $item; |
204
|
|
|
}, |
205
|
|
|
$parts |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
$operators[$operator] = lcfirst(implode('', $parts)); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
self::$operators = $operators; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return self::$operators; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|