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
|
|
|
// Dont change order - longer will be first (before other started from same symbols) |
18
|
|
|
// @2DO: when updated StrHepler::findPrefix - refactor for remove aliases |
19
|
|
|
const RULES = [ |
20
|
|
|
'<=', '=<', '>=', '=>','!=','<>', '!like:','!contain:', '!in:', 'like:','contain:', 'in:', '<', '>', '!', '=' |
21
|
|
|
]; |
22
|
|
|
const STARED_RULES = [ |
23
|
|
|
'*<=', '*=<', '*>=', '*=>','*!=','*<>', '*!like:','*!contain:', '*!in:', '*like:','*contain:', '*in:', '*<', '*>', '*!', '*=' |
24
|
|
|
]; |
25
|
|
|
const RULES_ALIASES = [ |
26
|
|
|
'=<' => '<=', |
27
|
|
|
'=>' => '>=', |
28
|
|
|
'!' => '!=', |
29
|
|
|
'<>' => '!=', |
30
|
|
|
|
31
|
|
|
'*=<' => '*<=', |
32
|
|
|
'*=>' => '*>=', |
33
|
|
|
'*!' => '*!=', |
34
|
|
|
'*<>' => '*!=', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Compare two values using orders list in format [field1, field2, !field3] |
39
|
|
|
* where ! is reverse ordering |
40
|
|
|
* @param array $orders |
41
|
|
|
* @param type $value1 |
42
|
|
|
* @param type $value2 |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
|
|
public static function compareByOrders(array $orders, $value1, $value2) : int |
46
|
|
|
{ |
47
|
|
|
$wrapper1 = new DataWrapper($value1); |
48
|
|
|
$wrapper2 = new DataWrapper($value2); |
49
|
|
|
foreach($orders as $order) { |
50
|
|
|
if($order[0] == '!') { |
51
|
|
|
$field = substr($order, 1); |
52
|
|
|
$direction = 1; |
53
|
|
|
} else { |
54
|
|
|
$field = $order; |
55
|
|
|
$direction = -1; |
56
|
|
|
} |
57
|
|
|
if ($wrapper1[$field] != $wrapper2[$field]) { |
58
|
|
|
return $direction * ($wrapper1[$field] < $wrapper2[$field] ? -1 : 1); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
return 0; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Find prefix supported by ruleCheck |
66
|
|
|
* return two value. first is rule, second is last part of string |
67
|
|
|
* |
68
|
|
|
* @param string $str |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public static function findRulePrefix(string $str) : array |
72
|
|
|
{ |
73
|
|
|
$allRules = array_merge(self::STARED_RULES, self::RULES); |
74
|
|
|
[$rule,$field] = StrHelper::findPrefix($str, $allRules, '='); |
|
|
|
|
75
|
|
|
return [static::tryRuleAliase($rule), $field]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* If array of rules is in "simple format" |
80
|
|
|
* convert it to full format |
81
|
|
|
* @param array $rules |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public static function tryPrepareSimpleRules(array $rules) : array |
85
|
|
|
{ |
86
|
|
|
if(empty($rules) or isset($rules[0])) { |
87
|
|
|
return $rules; |
88
|
|
|
} |
89
|
|
|
$result = ['and']; |
90
|
|
|
foreach($rules as $fieldRule=>$arg2) { |
91
|
|
|
[$rule, $arg1] = static::findRulePrefix($fieldRule); |
|
|
|
|
92
|
|
|
$result[] = [$rule, $arg1, $arg2]; |
93
|
|
|
} |
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Do simple checks by rules |
99
|
|
|
* rule is one of: '<=', '=<', '>=', '=>', '!like:','!contain:', '!in:', |
100
|
|
|
* 'like:','contain:', 'in:', '<', '>', '!', '=' |
101
|
|
|
* value1 and value2 is is parameters for compare |
102
|
|
|
* |
103
|
|
|
* @param string $rule |
104
|
|
|
* @param mixed $value1 |
105
|
|
|
* @param mixed $value2 |
106
|
|
|
* @return bool |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
public static function checkRule(string $rule, $value1, $value2) : bool |
110
|
|
|
{ |
111
|
|
|
switch ($rule) { |
112
|
|
|
case '<=': |
113
|
|
|
return ($value1 <= $value2); |
114
|
|
|
case '>=': |
115
|
|
|
return ($value1 >= $value2); |
116
|
|
|
case '!like:': |
117
|
|
|
return !StrHelper::like($value1, $value2); |
118
|
|
|
case '!in:': |
119
|
|
|
return !in_array($value1, $value2); |
120
|
|
|
case '!contain:': |
121
|
|
|
return !in_array($value2, $value1); |
122
|
|
|
case 'like:': |
123
|
|
|
return StrHelper::like($value1, $value2); |
124
|
|
|
case 'contain:': |
125
|
|
|
return in_array($value2, $value1); |
126
|
|
|
case 'in:': |
127
|
|
|
return in_array($value1, $value2); |
128
|
|
|
case '<': |
129
|
|
|
return ($value1 < $value2); |
130
|
|
|
case '>': |
131
|
|
|
return ($value1 > $value2); |
132
|
|
|
case '!=': |
133
|
|
|
return ($value1 != $value2); |
134
|
|
|
case '=': |
135
|
|
|
return ($value1 == $value2); |
136
|
|
|
default: |
137
|
|
|
throw new \RuntimeException('Unknown rule '.$rule); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected static function tryRuleAliase(string $rule) : string |
142
|
|
|
{ |
143
|
|
|
if(isset(self::RULES_ALIASES[$rule])) { |
144
|
|
|
return self::RULES_ALIASES[$rule]; |
145
|
|
|
} else { |
146
|
|
|
return $rule; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.