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
|
|
|
* Trait for add check logic |
12
|
|
|
* |
13
|
|
|
* @author mendel |
14
|
|
|
*/ |
15
|
|
|
trait CheckTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Check if data satisfies the condition |
19
|
|
|
* |
20
|
|
|
* @param array $conditions |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
|
|
public function check(array $conditions) : bool |
24
|
|
|
{ |
25
|
|
|
$args = CompareHelper::tryPrepareSimpleRules($conditions); |
26
|
|
|
$type = array_shift($args); |
27
|
|
|
switch (strtolower($type)) { |
28
|
|
|
case 'and': |
29
|
|
|
return $this->checkAnd($args); |
30
|
|
|
case 'or': |
31
|
|
|
return $this->checkOr($args); |
32
|
|
|
case 'not': |
33
|
|
|
return !$this->check($args[0]); |
34
|
|
|
default: |
35
|
|
|
return $this->checkField($type, $args[0], $args[1]); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check AND condition |
41
|
|
|
* |
42
|
|
|
* @param array $conditions |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
private function checkAnd(array $conditions) : bool |
46
|
|
|
{ |
47
|
|
|
foreach($conditions as $line) { |
48
|
|
|
if(!$this->check($line)) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check OR condition |
57
|
|
|
* |
58
|
|
|
* @param array $conditions |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
private function checkOr(array $conditions) : bool |
62
|
|
|
{ |
63
|
|
|
foreach($conditions as $line) { |
64
|
|
|
if($this->check($line)) { |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check/compare some field by rule and some value |
73
|
|
|
* |
74
|
|
|
* @param string $staredRule |
75
|
|
|
* @param mixed $arg1 |
76
|
|
|
* @param mixed $arg2 |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
private function checkField(string $staredRule, $arg1, $arg2) : bool |
80
|
|
|
{ |
81
|
|
|
[$rulePrefix, $rule] = StrHelper::findPrefix($staredRule, ['*']); |
|
|
|
|
82
|
|
|
$value1 = $this->$arg1; |
83
|
|
|
if($rulePrefix == '*') { |
84
|
|
|
$value2 = $this->$arg2; |
85
|
|
|
} else { |
86
|
|
|
$value2 = $arg2; |
87
|
|
|
} |
88
|
|
|
return CompareHelper::checkRule($rule, $value1, $value2); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.