|
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 flexible conditions |
|
12
|
|
|
* |
|
13
|
|
|
* @author mendel |
|
14
|
|
|
*/ |
|
15
|
|
|
class CheckHelper |
|
16
|
|
|
{ |
|
17
|
|
|
protected static $allRules = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Check if data satisfies the condition |
|
21
|
|
|
* |
|
22
|
|
|
* @param mixed $data |
|
23
|
|
|
* @param array $conditions |
|
24
|
|
|
* @return bool |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function check($data, array $conditions) : bool |
|
27
|
|
|
{ |
|
28
|
|
|
$args = self::tryPrepareSimpleRules($conditions); |
|
29
|
|
|
$type = array_shift($args); |
|
30
|
|
|
switch (strtolower($type)) { |
|
31
|
|
|
case 'and': |
|
32
|
|
|
return self::checkAnd($data, $args); |
|
33
|
|
|
case 'or': |
|
34
|
|
|
return self::checkOr($data, $args); |
|
35
|
|
|
case 'not': |
|
36
|
|
|
return !self::check($data, $args[0]); |
|
37
|
|
|
default: |
|
38
|
|
|
return self::checkField($data, $type, $args[0], $args[1]); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* If array of rules is in "simple format" |
|
44
|
|
|
* convert it to full format |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $rules |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected static function tryPrepareSimpleRules(array $rules) : array |
|
50
|
|
|
{ |
|
51
|
|
|
self::initAllRules(); |
|
52
|
|
|
if(empty($rules) or isset($rules[0])) { |
|
53
|
|
|
return $rules; |
|
54
|
|
|
} |
|
55
|
|
|
$result = ['and']; |
|
56
|
|
|
foreach($rules as $fieldRule=>$arg2) { |
|
57
|
|
|
[$rule, $arg1] = StrHelper::findPrefix($fieldRule, static::$allRules, '='); |
|
|
|
|
|
|
58
|
|
|
$result[] = [$rule, $arg1, $arg2]; |
|
59
|
|
|
} |
|
60
|
|
|
return $result; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Init list of rules if not initialized |
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function initAllRules() : void |
|
69
|
|
|
{ |
|
70
|
|
|
if(empty(static::$allRules)) { |
|
71
|
|
|
foreach(CompareHelper::RULES as $rule) { |
|
72
|
|
|
static::$allRules[] = '*'.$rule; |
|
73
|
|
|
static::$allRules[] = $rule; |
|
74
|
|
|
} |
|
75
|
|
|
foreach(array_keys(CompareHelper::RULES_ALIASES) as $rule) { |
|
76
|
|
|
static::$allRules[] = '*'.$rule; |
|
77
|
|
|
static::$allRules[] = $rule; |
|
78
|
|
|
} |
|
79
|
|
|
// Sort by lenght |
|
80
|
|
|
usort(static::$allRules, function(string $a, string $b) : int { |
|
81
|
|
|
return strlen($b) <=> strlen($a); // for reversal result |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Check AND condition |
|
88
|
|
|
* |
|
89
|
|
|
* @param mixed $data |
|
90
|
|
|
* @param array $conditions |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
protected static function checkAnd($data, array $conditions) : bool |
|
94
|
|
|
{ |
|
95
|
|
|
foreach($conditions as $line) { |
|
96
|
|
|
if(!self::check($data,$line)) { |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Check OR condition |
|
105
|
|
|
* |
|
106
|
|
|
* @param mixed $data |
|
107
|
|
|
* @param array $conditions |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
protected static function checkOr($data, array $conditions) : bool |
|
111
|
|
|
{ |
|
112
|
|
|
foreach($conditions as $line) { |
|
113
|
|
|
if(self::check($data,$line)) { |
|
114
|
|
|
return true; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Check/compare some field by rule and some value |
|
122
|
|
|
* |
|
123
|
|
|
* @param mixed $data |
|
124
|
|
|
* @param string $staredRule |
|
125
|
|
|
* @param mixed $arg1 |
|
126
|
|
|
* @param mixed $arg2 |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
|
|
protected static function checkField($data, string $staredRule, $arg1, $arg2) : bool |
|
130
|
|
|
{ |
|
131
|
|
|
[$rulePrefix, $rule] = StrHelper::findPrefix($staredRule, ['*']); |
|
|
|
|
|
|
132
|
|
|
$value1 = $data->$arg1; |
|
133
|
|
|
if($rulePrefix == '*') { |
|
134
|
|
|
$value2 = $data->$arg2; |
|
135
|
|
|
} else { |
|
136
|
|
|
$value2 = $arg2; |
|
137
|
|
|
} |
|
138
|
|
|
return CompareHelper::compareByRule($rule, $value1, $value2); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
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.