|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* @copyright (c) 2019 Mendel <[email protected]> |
|
5
|
|
|
* @license see license.txt |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace drycart\data; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Helper for simple compare checks |
|
12
|
|
|
* |
|
13
|
|
|
* @author mendel |
|
14
|
|
|
*/ |
|
15
|
|
|
class CompareHelper |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* List of main compare rules |
|
19
|
|
|
*/ |
|
20
|
|
|
const RULES = [ |
|
21
|
|
|
'<=', '>=', '!=', '!like:','!contain:', '!in:', 'like:','contain:', 'in:', '<', '>', '=' |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* list of aliases for rules |
|
26
|
|
|
*/ |
|
27
|
|
|
const RULES_ALIASES = [ |
|
28
|
|
|
'=<' => '<=', |
|
29
|
|
|
'=>' => '>=', |
|
30
|
|
|
'!' => '!=', |
|
31
|
|
|
'<>' => '!=', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Compare two values using orders list in format [field1, field2, !field3] |
|
36
|
|
|
* where ! is reverse ordering |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $orders |
|
39
|
|
|
* @param type $value1 |
|
40
|
|
|
* @param type $value2 |
|
41
|
|
|
* @return int |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function compareByOrders(array $orders, $value1, $value2) : int |
|
44
|
|
|
{ |
|
45
|
|
|
$wrapper1 = new DataWrapper($value1); |
|
46
|
|
|
$wrapper2 = new DataWrapper($value2); |
|
47
|
|
|
foreach($orders as $order) { |
|
48
|
|
|
if($order[0] == '!') { |
|
49
|
|
|
$field = substr($order, 1); |
|
50
|
|
|
$direction = 1; |
|
51
|
|
|
} else { |
|
52
|
|
|
$field = $order; |
|
53
|
|
|
$direction = -1; |
|
54
|
|
|
} |
|
55
|
|
|
if ($wrapper1[$field] != $wrapper2[$field]) { |
|
56
|
|
|
return $direction * ($wrapper1[$field] < $wrapper2[$field] ? -1 : 1); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
return 0; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Do simple checks by rules |
|
64
|
|
|
* rule is one of: '<=', '=<', '>=', '=>', '!like:','!contain:', '!in:', |
|
65
|
|
|
* 'like:','contain:', 'in:', '<', '>', '!', '=' |
|
66
|
|
|
* value1 and value2 is is parameters for compare |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $rule |
|
69
|
|
|
* @param mixed $value1 |
|
70
|
|
|
* @param mixed $value2 |
|
71
|
|
|
* @return bool |
|
72
|
|
|
* @throws \UnexpectedValueException |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function compareByRule(string $rule, $value1, $value2) : bool |
|
75
|
|
|
{ |
|
76
|
|
|
switch (static::tryRuleAliase($rule)) { |
|
77
|
|
|
case '<=': |
|
78
|
|
|
return ($value1 <= $value2); |
|
79
|
|
|
case '>=': |
|
80
|
|
|
return ($value1 >= $value2); |
|
81
|
|
|
case '!like:': |
|
82
|
|
|
return !StrHelper::like($value1, $value2); |
|
83
|
|
|
case '!in:': |
|
84
|
|
|
return !in_array($value1, $value2); |
|
85
|
|
|
case '!contain:': |
|
86
|
|
|
return !in_array($value2, $value1); |
|
87
|
|
|
case 'like:': |
|
88
|
|
|
return StrHelper::like($value1, $value2); |
|
89
|
|
|
case 'contain:': |
|
90
|
|
|
return in_array($value2, $value1); |
|
91
|
|
|
case 'in:': |
|
92
|
|
|
return in_array($value1, $value2); |
|
93
|
|
|
case '<': |
|
94
|
|
|
return ($value1 < $value2); |
|
95
|
|
|
case '>': |
|
96
|
|
|
return ($value1 > $value2); |
|
97
|
|
|
case '!=': |
|
98
|
|
|
return ($value1 != $value2); |
|
99
|
|
|
case '=': |
|
100
|
|
|
return ($value1 == $value2); |
|
101
|
|
|
default: |
|
102
|
|
|
throw new \UnexpectedValueException('Unknown rule '.$rule); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Change rules aliase to main rule if it is aliase |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $rule |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
protected static function tryRuleAliase(string $rule) : string |
|
113
|
|
|
{ |
|
114
|
|
|
if(isset(self::RULES_ALIASES[$rule])) { |
|
115
|
|
|
return self::RULES_ALIASES[$rule]; |
|
116
|
|
|
} else { |
|
117
|
|
|
return $rule; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|