1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PhpFilterer |
4
|
|
|
* |
5
|
|
|
* @package php-logical-filter |
6
|
|
|
* @author Jean Claveau |
7
|
|
|
*/ |
8
|
|
|
namespace JClaveau\LogicalFilter\Filterer; |
9
|
|
|
|
10
|
|
|
use JClaveau\LogicalFilter\LogicalFilter; |
11
|
|
|
use JClaveau\LogicalFilter\Rule\EqualRule; |
12
|
|
|
use JClaveau\LogicalFilter\Rule\BelowRule; |
13
|
|
|
use JClaveau\LogicalFilter\Rule\AboveRule; |
14
|
|
|
use JClaveau\LogicalFilter\Rule\BelowOrEqualRule; |
15
|
|
|
use JClaveau\LogicalFilter\Rule\AboveOrEqualRule; |
16
|
|
|
use JClaveau\LogicalFilter\Rule\NotEqualRule; |
17
|
|
|
use JClaveau\LogicalFilter\Rule\InRule; |
18
|
|
|
use JClaveau\LogicalFilter\Rule\NotInRule; |
19
|
|
|
use JClaveau\LogicalFilter\Rule\RegexpRule; |
20
|
|
|
use JClaveau\LogicalFilter\FilteredKey; |
21
|
|
|
use JClaveau\LogicalFilter\FilteredValue; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
*/ |
25
|
|
|
class PhpFilterer extends Filterer |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
8 |
|
*/ |
29
|
|
|
public function validateRule ($field, $operator, $value, $row, array $path, $all_operands, $options) |
30
|
8 |
|
{ |
31
|
2 |
|
if ($field instanceof FilteredValue) { |
32
|
2 |
|
$value_to_validate = $field( $row ); |
33
|
8 |
|
} |
34
|
3 |
|
elseif ($field instanceof FilteredKey) { |
35
|
3 |
|
$value_to_validate = $field( array_pop($path) ); |
36
|
6 |
|
} |
37
|
1 |
|
elseif (! isset($row[(string) $field])) { |
38
|
1 |
|
$value_to_validate = null; |
39
|
|
|
} |
40
|
6 |
|
else { |
41
|
|
|
$value_to_validate = $row[ $field ]; |
42
|
|
|
} |
43
|
8 |
|
|
44
|
3 |
|
if (EqualRule::operator === $operator) { |
45
|
|
|
if (! isset($value_to_validate)) { |
46
|
|
|
// ['field', '=', null] <=> isset($row['field']) |
47
|
1 |
|
// [row, '=', null] <=> $row !== null |
48
|
1 |
|
$result = null === $value; |
49
|
|
|
} |
50
|
|
|
else { |
51
|
3 |
|
// TODO support strict comparisons |
52
|
|
|
$result = $value_to_validate == $value; |
53
|
3 |
|
} |
54
|
6 |
|
} |
55
|
1 |
View Code Duplication |
elseif (InRule::operator === $operator) { |
|
|
|
|
56
|
|
|
$result = in_array( |
57
|
|
|
isset($value_to_validate) ? $value_to_validate : null, |
58
|
|
|
$value |
59
|
1 |
|
); |
60
|
|
|
} |
61
|
1 |
|
elseif (BelowRule::operator === $operator) { |
62
|
5 |
|
if (! isset($value_to_validate)) { |
63
|
2 |
|
$result = false; |
64
|
|
|
} |
65
|
|
|
else { |
66
|
|
|
$result = $value_to_validate < $value; |
67
|
2 |
|
} |
68
|
|
|
} |
69
|
2 |
|
elseif (AboveRule::operator === $operator) { |
70
|
5 |
|
if (! isset($value_to_validate)) { |
71
|
2 |
|
$result = false; |
72
|
|
|
} |
73
|
|
|
else { |
74
|
|
|
$result = $value_to_validate > $value; |
75
|
2 |
|
} |
76
|
|
|
} |
77
|
2 |
|
elseif (NotEqualRule::operator === $operator) { |
78
|
5 |
|
if (null === $value) { |
79
|
4 |
|
$result = isset($value_to_validate); |
80
|
1 |
|
} |
81
|
1 |
|
else { |
82
|
|
|
$result = $value_to_validate != $value; |
83
|
3 |
|
} |
84
|
|
|
} |
85
|
4 |
View Code Duplication |
elseif (NotInRule::operator === $operator) { |
|
|
|
|
86
|
2 |
|
$result = ! in_array( |
87
|
2 |
|
isset($value_to_validate) ? $value_to_validate : null, |
88
|
1 |
|
$value |
89
|
1 |
|
); |
90
|
|
|
} |
91
|
1 |
|
elseif (AboveOrEqualRule::operator === $operator) { |
92
|
|
|
if (! isset($value_to_validate)) { |
93
|
2 |
|
$result = false; |
94
|
1 |
|
} |
95
|
1 |
|
else { |
96
|
|
|
$result = $value_to_validate >= $value; |
97
|
|
|
} |
98
|
|
|
} |
99
|
1 |
|
elseif (BelowOrEqualRule::operator === $operator) { |
100
|
|
|
if (! isset($value_to_validate)) { |
101
|
1 |
|
$result = false; |
102
|
1 |
|
} |
103
|
1 |
|
else { |
104
|
|
|
$result = $value_to_validate <= $value; |
105
|
|
|
} |
106
|
|
|
} |
107
|
1 |
|
elseif (RegexpRule::operator === $operator) { |
108
|
|
|
if (! isset($value_to_validate)) { |
109
|
1 |
|
$result = false; |
110
|
|
|
} |
111
|
|
View Code Duplication |
else { |
|
|
|
|
112
|
|
|
try { |
113
|
|
|
// TODO support optionnal parameters (offest mainly) ? |
114
|
|
|
$result = preg_match($value, $value_to_validate); |
115
|
|
|
} |
116
|
|
|
catch (\Exception $e) { |
117
|
|
|
// The documentation of preg_match() is wrong and preg_last_error() |
118
|
|
|
// is useless as preg_match returns 0 instead of false |
119
|
|
|
// and then throws an exception with PHP 5.6 |
120
|
|
|
throw new \InvalidArgumentException( |
121
|
|
|
"PCRE error ".var_export($e->getMessage(), true). |
122
|
|
|
" while applying the regexp ".var_export($value, true)." to " |
123
|
8 |
|
.var_export($value_to_validate, true) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$result = (bool) $result; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
else { |
131
|
|
|
throw new \InvalidArgumentException( |
132
|
|
|
"Unhandled operator: " . $operator |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// var_dump( |
137
|
|
|
// "$field, $operator, " . var_export($value, true) |
138
|
|
|
// . ' vs ' . var_export($value_to_validate, true) . ' => ' . var_export($result, true) |
139
|
|
|
// . "\n\n" |
140
|
|
|
// . var_export($row, true) |
141
|
|
|
// ); |
142
|
|
|
// exit; |
143
|
|
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/**/ |
147
|
|
|
} |
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.