1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EloquentJs\Query\Guard\Policy; |
4
|
|
|
|
5
|
|
|
use EloquentJs\Query\MethodCall; |
6
|
|
|
|
7
|
|
|
class Rule |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $method; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $arguments; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $sugarMap = [ |
23
|
|
|
'select' => [ |
24
|
|
|
'select', 'addSelect' |
25
|
|
|
], |
26
|
|
|
'where' => [ |
27
|
|
|
'where', 'orWhere', |
28
|
|
|
'whereBetween', 'orWhereBetween', 'whereNotBetween', 'orWhereNotBetween', |
29
|
|
|
'whereExists', 'orWhereExists', 'whereNotExists', 'orWhereNotExists', |
30
|
|
|
'whereIn', 'orWhereIn', 'whereNotIn', 'orWhereNotIn', |
31
|
|
|
'whereNull', 'orWhereNull', 'whereNotNull', 'orWhereNotNull', |
32
|
|
|
'whereDate', 'whereDay', 'whereMonth', 'whereYear', |
33
|
|
|
], |
34
|
|
|
'having' => [ |
35
|
|
|
'having', 'orHaving', |
36
|
|
|
], |
37
|
|
|
'orderBy' => [ |
38
|
|
|
'orderBy', 'latest', 'oldest', |
39
|
|
|
], |
40
|
|
|
'offset' => [ |
41
|
|
|
'offset', 'skip', |
42
|
|
|
], |
43
|
|
|
'limit' => [ |
44
|
|
|
'limit', 'take', 'forPage', |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new rule instance. |
50
|
|
|
* |
51
|
|
|
* @param string $method |
52
|
|
|
* @param array $arguments |
53
|
|
|
*/ |
54
|
|
|
public function __construct($method, array $arguments = []) |
55
|
|
|
{ |
56
|
|
|
$this->method = $method; |
57
|
|
|
$this->arguments = $arguments; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Test if the method call passes this rule. |
62
|
|
|
* |
63
|
|
|
* @param MethodCall $call |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function test(MethodCall $call) |
67
|
|
|
{ |
68
|
|
|
return $this->methodMatches($call->method) |
69
|
|
|
and $this->argumentListMatches($call->arguments); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Test if the given value matches the required method pattern. |
74
|
|
|
* |
75
|
|
|
* @param string $value |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
protected function methodMatches($value) |
79
|
|
|
{ |
80
|
|
|
return $this->comparePattern($this->method, $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Test if the given arguments match the required arguments signature. |
85
|
|
|
* |
86
|
|
|
* @param array $arguments |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
protected function argumentListMatches(array $arguments) |
90
|
|
|
{ |
91
|
|
|
foreach ($arguments as $index => $argument) { |
92
|
|
|
|
93
|
|
|
if (empty($this->arguments[$index])) { |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ( ! $this->comparePattern($this->arguments[$index], $argument)) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Compare the given value to the given pattern. |
107
|
|
|
* |
108
|
|
|
* @param string $pattern |
109
|
|
|
* @param string $value |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
protected function comparePattern($pattern, $value) |
113
|
|
|
{ |
114
|
|
|
if (is_callable($pattern)) { |
115
|
|
|
return $pattern($value); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$alternates = explode('|', $pattern); |
119
|
|
|
|
120
|
|
|
return ! ! array_first($alternates, function($index, $pattern) use ($value) { |
121
|
|
|
|
122
|
|
|
$firstCharacter = substr($pattern, 0, 1); |
123
|
|
|
$remainder = substr($pattern, 1); |
124
|
|
|
|
125
|
|
|
switch ($firstCharacter) { |
126
|
|
|
case '>': |
127
|
|
|
return $remainder < $value; |
128
|
|
|
case '<': |
129
|
|
|
return $remainder > $value; |
130
|
|
|
case '!': |
131
|
|
|
return ! str_is($remainder, $value); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return str_is($pattern, $value); |
135
|
|
|
}); |
136
|
|
|
} |
137
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.