| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* |
| 5 |
* A class to handle logical expression for notification conditions. |
| 6 |
* |
| 7 |
* Please note that we only support scalar data types and lists of these data types (only when the operator operates on lists)! |
| 8 |
*/ |
| 9 |
class Pixcloud_Notification_LogicalExpression { |
| 10 |
|
| 11 |
/** |
| 12 |
* All the supported operators. |
| 13 |
* |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
protected static $all_operators = array( |
| 17 |
'equal', 'not_equal', 'is_empty', 'is_not_empty', |
| 18 |
'less', 'less_or_equal', 'greater', 'greater_or_equal', |
| 19 |
'begins_with', 'not_begins_with', 'contains', 'not_contains', 'ends_with', 'not_ends_with', |
| 20 |
'between', 'not_between', |
| 21 |
'in', 'not_in', 'any', 'all', |
| 22 |
); |
| 23 |
|
| 24 |
/** |
| 25 |
* All the unary operators (i.e. work with only the left operand). |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected static $unary_operators = array( |
| 30 |
'is_empty', 'is_not_empty', |
| 31 |
); |
| 32 |
|
| 33 |
/** |
| 34 |
* All the binary operators (i.e. work with left and a single right operand). |
| 35 |
* |
| 36 |
* @var array |
| 37 |
*/ |
| 38 |
protected static $binary_operators = array( |
| 39 |
'equal', 'not_equal', |
| 40 |
'less', 'less_or_equal', 'greater', 'greater_or_equal', |
| 41 |
'begins_with', 'not_begins_with', 'contains', 'not_contains', 'ends_with', 'not_ends_with', |
| 42 |
); |
| 43 |
|
| 44 |
/** |
| 45 |
* All the ternary operators (i.e. work with a left operand and two right operands). |
| 46 |
* |
| 47 |
* @var array |
| 48 |
*/ |
| 49 |
protected static $ternary_operators = array( |
| 50 |
'between', 'not_between', |
| 51 |
); |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* All the list operators (i.e. work with a (list) left operand and (list) right operand). |
| 56 |
* |
| 57 |
* @var array |
| 58 |
*/ |
| 59 |
protected static $list_operators = array( |
| 60 |
'in', 'not_in', 'any', 'all', |
| 61 |
); |
| 62 |
|
| 63 |
/** |
| 64 |
* Evaluate a logical expression with one or two operands and an operator. |
| 65 |
* |
| 66 |
* @param mixed $left |
| 67 |
* @param string $operator |
| 68 |
* @param mixed $right Optional. |
| 69 |
* |
| 70 |
* @return bool|null The logical expression result or null on invalid data. |
| 71 |
*/ |
| 72 |
public static function evaluate( $left, $operator, $right = null ) { |
| 73 |
// Reject unknown operators. |
| 74 |
if ( ! in_array( $operator, self::$all_operators ) ) { |
| 75 |
return null; |
| 76 |
} |
| 77 |
// Now, check the number of values a operator can handle and reject on wrong number. |
| 78 |
if ( in_array( $operator, self::$binary_operators ) && is_array( $right ) && count( $right ) > 1 ) { |
| 79 |
return null; |
| 80 |
} |
| 81 |
if ( in_array( $operator, self::$ternary_operators ) && ( ! is_array( $right ) || count( $right ) != 2 ) ) { |
| 82 |
return null; |
| 83 |
} |
| 84 |
if ( in_array( $operator, self::$list_operators ) && ! is_array( $right ) ) { |
| 85 |
return null; |
| 86 |
} |
| 87 |
|
| 88 |
// Reject missing operator evaluation method. |
| 89 |
if ( ! method_exists( __CLASS__, 'evaluate_' . $operator ) ) { |
| 90 |
return null; |
| 91 |
} |
| 92 |
|
| 93 |
return (bool) call_user_func( array( __CLASS__, 'evaluate_' . $operator ), $left, $right ); |
| 94 |
} |
| 95 |
|
| 96 |
/* ============================= |
| 97 |
* EVALUATORS FOR EACH OPERATOR. |
| 98 |
*/ |
| 99 |
|
| 100 |
/** |
| 101 |
* Determine if the left operand is equal to the right operand. The comparison is strict type. |
| 102 |
* |
| 103 |
* @param mixed $left |
| 104 |
* @param mixed $right |
| 105 |
* |
| 106 |
* @return bool |
| 107 |
*/ |
| 108 |
public static function evaluate_equal( $left, $right ) { |
| 109 |
// Sanity check. |
| 110 |
if ( is_array( $right ) ) { |
| 111 |
$right = array_shift( $right ); |
| 112 |
} |
| 113 |
|
| 114 |
return $left === $right; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Determine if the left operand is not equal to the right operand. The comparison is strict type. |
| 119 |
* |
| 120 |
* @param mixed $left |
| 121 |
* @param mixed $right |
| 122 |
* |
| 123 |
* @return bool |
| 124 |
*/ |
| 125 |
public static function evaluate_not_equal( $left, $right ) { |
| 126 |
// Sanity check. |
| 127 |
if ( is_array( $right ) ) { |
| 128 |
$right = array_shift( $right ); |
| 129 |
} |
| 130 |
|
| 131 |
return $left !== $right; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Determine if the left operand is empty (the php empty() function is applied). |
| 136 |
* |
| 137 |
* @param mixed $left |
| 138 |
* @param mixed $right Optional. Not used. |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
*/ |
| 142 |
public static function evaluate_is_empty( $left, $right = null ) { |
| 143 |
return empty( $left ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Determine if the left operand is not empty (the php empty() function is applied). |
| 148 |
* |
| 149 |
* @param mixed $left |
| 150 |
* @param mixed $right Optional. Not used. |
| 151 |
* |
| 152 |
* @return bool |
| 153 |
*/ |
| 154 |
public static function evaluate_is_not_empty( $left, $right = null ) { |
| 155 |
return ! empty( $left ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Determine if the left (numeric) operand less than the right (numeric) operand. |
| 160 |
* |
| 161 |
* @param int|double|float $left |
| 162 |
* @param int|double|float $right |
| 163 |
* |
| 164 |
* @return bool |
| 165 |
*/ |
| 166 |
public static function evaluate_less( $left, $right ) { |
| 167 |
// Sanity check. |
| 168 |
if ( is_array( $right ) ) { |
| 169 |
$right = array_shift( $right ); |
| 170 |
} |
| 171 |
|
| 172 |
return $left < $right; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Determine if the left (numeric) operand less or equal than the right (numeric) operand. |
| 177 |
* |
| 178 |
* @param int|double|float $left |
| 179 |
* @param int|double|float $right |
| 180 |
* |
| 181 |
* @return bool |
| 182 |
*/ |
| 183 |
public static function evaluate_less_or_equal( $left, $right ) { |
| 184 |
// Sanity check. |
| 185 |
if ( is_array( $right ) ) { |
| 186 |
$right = array_shift( $right ); |
| 187 |
} |
| 188 |
|
| 189 |
return $left <= $right; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Determine if the left (numeric) operand greater than the right (numeric) operand. |
| 194 |
* |
| 195 |
* @param int|double|float $left |
| 196 |
* @param int|double|float $right |
| 197 |
* |
| 198 |
* @return bool |
| 199 |
*/ |
| 200 |
public static function evaluate_greater( $left, $right ) { |
| 201 |
// Sanity check. |
| 202 |
if ( is_array( $right ) ) { |
| 203 |
$right = array_shift( $right ); |
| 204 |
} |
| 205 |
|
| 206 |
return $left > $right; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Determine if the left (numeric) operand greater or equal than the right (numeric) operand. |
| 211 |
* |
| 212 |
* @param int|double|float $left |
| 213 |
* @param int|double|float $right |
| 214 |
* |
| 215 |
* @return bool |
| 216 |
*/ |
| 217 |
public static function evaluate_greater_or_equal( $left, $right ) { |
| 218 |
// Sanity check. |
| 219 |
if ( is_array( $right ) ) { |
| 220 |
$right = array_shift( $right ); |
| 221 |
} |
| 222 |
|
| 223 |
return $left >= $right; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Determine if the right (string) operand is at start of the left (string) operand. |
| 228 |
* |
| 229 |
* @param string $left |
| 230 |
* @param string $right |
| 231 |
* |
| 232 |
* @return bool |
| 233 |
*/ |
| 234 |
public static function evaluate_begins_with( $left, $right ) { |
| 235 |
// Sanity check. |
| 236 |
if ( is_array( $right ) ) { |
| 237 |
$right = array_shift( $right ); |
| 238 |
} |
| 239 |
|
| 240 |
return 0 === strpos( $left, $right ); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Determine if the right (string) operand is not at start of the left (string) operand. |
| 245 |
* |
| 246 |
* @param string $left |
| 247 |
* @param string $right |
| 248 |
* |
| 249 |
* @return bool |
| 250 |
*/ |
| 251 |
public static function evaluate_not_begins_with( $left, $right ) { |
| 252 |
// Sanity check. |
| 253 |
if ( is_array( $right ) ) { |
| 254 |
$right = array_shift( $right ); |
| 255 |
} |
| 256 |
|
| 257 |
return 0 !== strpos( $left, $right ); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Determine if the right (string) operand is part of the left (string) operand. |
| 262 |
* |
| 263 |
* @param string $left |
| 264 |
* @param string $right |
| 265 |
* |
| 266 |
* @return bool |
| 267 |
*/ |
| 268 |
public static function evaluate_contains( $left, $right ) { |
| 269 |
// Sanity check. |
| 270 |
if ( is_array( $right ) ) { |
| 271 |
$right = array_shift( $right ); |
| 272 |
} |
| 273 |
|
| 274 |
return false !== strpos( $left, $right ); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Determine if the right (string) operand is not part of the left (string) operand. |
| 279 |
* |
| 280 |
* @param string $left |
| 281 |
* @param string $right |
| 282 |
* |
| 283 |
* @return bool |
| 284 |
*/ |
| 285 |
public static function evaluate_not_contains( $left, $right ) { |
| 286 |
// Sanity check. |
| 287 |
if ( is_array( $right ) ) { |
| 288 |
$right = array_shift( $right ); |
| 289 |
} |
| 290 |
|
| 291 |
return false === strpos( $left, $right ); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Determine if the right (string) operand is at end of the left (string) operand. |
| 296 |
* |
| 297 |
* @param string $left |
| 298 |
* @param string $right |
| 299 |
* |
| 300 |
* @return bool |
| 301 |
*/ |
| 302 |
public static function evaluate_ends_with( $left, $right ) { |
| 303 |
// Sanity check. |
| 304 |
if ( is_array( $right ) ) { |
| 305 |
$right = array_shift( $right ); |
| 306 |
} |
| 307 |
|
| 308 |
return ( strlen( $left ) - strlen( $right ) ) === strrpos( $left, $right ); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Determine if the right (string) operand is not at end of the left (string) operand. |
| 313 |
* |
| 314 |
* @param string $left |
| 315 |
* @param string $right |
| 316 |
* |
| 317 |
* @return bool |
| 318 |
*/ |
| 319 |
public static function evaluate_not_ends_with( $left, $right ) { |
| 320 |
// Sanity check. |
| 321 |
if ( is_array( $right ) ) { |
| 322 |
$right = array_shift( $right ); |
| 323 |
} |
| 324 |
|
| 325 |
return ( strlen( $left ) - strlen( $right ) ) !== strrpos( $left, $right ); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Determine if the left operand is between in the two values in right one. |
| 330 |
* |
| 331 |
* @param $left |
| 332 |
* @param array $right |
| 333 |
* |
| 334 |
* @return bool |
| 335 |
*/ |
| 336 |
public static function evaluate_between( $left, $right ) { |
| 337 |
// Get the two values |
| 338 |
$small = array_shift( $right ); |
| 339 |
$big = array_shift( $right ); |
| 340 |
|
| 341 |
return ( $small <= $left ) && ( $left <= $big ); |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Determine if the left operand is not between in the two values in right one. |
| 346 |
* |
| 347 |
* @param $left |
| 348 |
* @param array $right |
| 349 |
* |
| 350 |
* @return bool |
| 351 |
*/ |
| 352 |
public static function evaluate_not_between( $left, $right ) { |
| 353 |
// Get the two values |
| 354 |
$small = array_shift( $right ); |
| 355 |
$big = array_shift( $right ); |
| 356 |
|
| 357 |
return ! ( ( $small <= $left ) && ( $left <= $big ) ); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Determine if the left operand is present in the right (list) one. |
| 362 |
* |
| 363 |
* @param $left |
| 364 |
* @param array $right |
| 365 |
* |
| 366 |
* @return bool |
| 367 |
*/ |
| 368 |
public static function evaluate_in( $left, $right ) { |
| 369 |
// Sanity check. |
| 370 |
if ( ! is_array( $right ) ) { |
| 371 |
$right = array( $right ); |
| 372 |
} |
| 373 |
|
| 374 |
return ( in_array( $left, $right ) ); |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Determine if the left operand is not present in the right (list) one. |
| 379 |
* |
| 380 |
* @param $left |
| 381 |
* @param array $right |
| 382 |
* |
| 383 |
* @return bool |
| 384 |
*/ |
| 385 |
public static function evaluate_not_in( $left, $right ) { |
| 386 |
// Sanity check. |
| 387 |
if ( ! is_array( $right ) ) { |
| 388 |
$right = array( $right ); |
| 389 |
} |
| 390 |
|
| 391 |
return ! ( in_array( $left, $right ) ); |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Determine if any of the values in the left operand are present in the right one. |
| 396 |
* |
| 397 |
* @param array $left |
| 398 |
* @param array $right |
| 399 |
* |
| 400 |
* @return bool |
| 401 |
*/ |
| 402 |
public static function evaluate_any( $left, $right ) { |
| 403 |
// Sanity check. |
| 404 |
if ( ! is_array( $left ) ) { |
| 405 |
$left = array( $left ); |
| 406 |
} |
| 407 |
if ( ! is_array( $right ) ) { |
| 408 |
$right = array( $right ); |
| 409 |
} |
| 410 |
|
| 411 |
$intersect = array_intersect( $left, $right ); |
| 412 |
|
| 413 |
return ! empty( $intersect ); |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Determine if all of the values in the left operand are present in the right one. |
| 418 |
* |
| 419 |
* @param array $left |
| 420 |
* @param array $right |
| 421 |
* |
| 422 |
* @return bool |
| 423 |
*/ |
| 424 |
public static function evaluate_all( $left, $right ) { |
| 425 |
// Sanity check. |
| 426 |
if ( ! is_array( $left ) ) { |
| 427 |
$left = array( $left ); |
| 428 |
} |
| 429 |
if ( ! is_array( $right ) ) { |
| 430 |
$right = array( $right ); |
| 431 |
} |
| 432 |
|
| 433 |
$intersect = array_intersect( $left, $right ); |
| 434 |
|
| 435 |
return count( $intersect ) === count( $left ); |
| 436 |
} |
| 437 |
|
| 438 |
/* ======= |
| 439 |
* HELPERS |
| 440 |
*/ |
| 441 |
} |
| 442 |
|