|
1
|
|
|
<?php |
|
2
|
|
|
namespace JClaveau\LogicalFilter\Rule; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* a ! in x |
|
6
|
|
|
*/ |
|
7
|
|
|
class NotInRule extends NotRule |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var string operator */ |
|
10
|
|
|
const operator = '!in'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @param string $field The field to apply the rule on. |
|
14
|
|
|
* @param array $value The value the field can equal to. |
|
|
|
|
|
|
15
|
|
|
*/ |
|
16
|
26 |
|
public function __construct( $field, $possibilities ) |
|
17
|
|
|
{ |
|
18
|
26 |
|
$this->addOperand(new InRule($field, $possibilities)); |
|
19
|
26 |
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
*/ |
|
23
|
14 |
|
public function isNormalizationAllowed(array $contextual_options=[]) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
14 |
|
$operand = $this->getOperandAt(0); |
|
26
|
14 |
|
if (! $operand->getPossibilities()) { |
|
27
|
1 |
|
return false; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
13 |
|
return $operand->isNormalizationAllowed($contextual_options); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
*/ |
|
35
|
25 |
|
public function getField() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
25 |
|
if (! $this->getOperandAt(0)) { |
|
38
|
|
|
// TODO a NotRule with no operand should be simplified as |
|
39
|
|
|
// a TrueRule |
|
40
|
1 |
|
throw new \LogicException( |
|
41
|
|
|
"Trying to get the field of an InRule negation missing its operand" |
|
42
|
1 |
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
24 |
|
return $this->getOperandAt(0)->getField(); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function setField($field) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
2 |
|
if (! $this->getOperandAt(0)) { |
|
53
|
|
|
// TODO a NotRule with no operand should be simplified as |
|
54
|
|
|
// a TrueRule |
|
55
|
1 |
|
throw new \LogicException( |
|
56
|
|
|
"Trying to set the field of an InRule negation missing its operand" |
|
57
|
1 |
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return $this->getOperandAt(0)->setField($field); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
23 |
|
public function getPossibilities() |
|
67
|
|
|
{ |
|
68
|
23 |
|
if ($this->getOperandAt(0) instanceof EqualRule) { |
|
69
|
|
|
// In can be simplified in = |
|
70
|
|
|
return [$this->getOperandAt(0)->getValue()]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
23 |
|
return $this->getOperandAt(0)->getPossibilities(); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return $this |
|
|
|
|
|
|
78
|
|
|
*/ |
|
79
|
7 |
|
public function setPossibilities($possibilities) |
|
80
|
|
|
{ |
|
81
|
7 |
|
if ( is_object($possibilities) |
|
82
|
7 |
|
&& method_exists($possibilities, 'toArray') |
|
83
|
7 |
|
) { |
|
84
|
1 |
|
$possibilities = $possibilities->toArray(); |
|
|
|
|
|
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
7 |
|
if ($this->getOperandAt(0) instanceof EqualRule) { |
|
88
|
|
|
// TODO this case should occure anymore while a NotInRule |
|
89
|
|
|
// may not have the same class once simplified |
|
90
|
|
|
$possibilities[] = $this->getOperandAt(0)->getValue(); |
|
91
|
|
|
|
|
92
|
|
|
$operands = [ |
|
93
|
|
|
new InRule( |
|
94
|
|
|
$this->getOperandAt(0)->getField(), |
|
95
|
|
|
array_unique($possibilities) |
|
96
|
|
|
), |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
$this->setOperands($operands); |
|
100
|
|
|
} |
|
101
|
7 |
|
elseif ($this->getOperandAt(0) instanceof InRule) { |
|
102
|
7 |
|
return $this->getOperandAt(0)->setPossibilities($possibilities); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public function getValues() |
|
109
|
|
|
{ |
|
110
|
3 |
|
return $this->getPossibilities(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
*/ |
|
115
|
3 |
|
public function hasSolution(array $contextual_options=[]) |
|
116
|
|
|
{ |
|
117
|
3 |
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param array $options + show_instance=false Display the operator of the rule or its instance id |
|
122
|
|
|
* |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
23 |
|
public function toArray(array $options=[]) |
|
126
|
|
|
{ |
|
127
|
|
|
$default_options = [ |
|
128
|
23 |
|
'show_instance' => false, |
|
129
|
23 |
|
]; |
|
130
|
23 |
|
foreach ($default_options as $default_option => &$default_value) { |
|
131
|
23 |
|
if ( ! isset($options[ $default_option ])) { |
|
132
|
23 |
|
$options[ $default_option ] = $default_value; |
|
133
|
23 |
|
} |
|
134
|
23 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
try { |
|
137
|
|
|
return [ |
|
138
|
23 |
|
$this->getField(), |
|
139
|
23 |
|
$options['show_instance'] ? $this->getInstanceId() : self::operator, |
|
140
|
23 |
|
$this->getPossibilities(), |
|
141
|
23 |
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
catch (\LogicException $e) { |
|
144
|
|
|
return parent::toArray(); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @todo cache support |
|
150
|
|
|
*/ |
|
151
|
1 |
View Code Duplication |
public function toString(array $options=[]) |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
|
|
try { |
|
154
|
1 |
|
$operator = self::operator; |
|
155
|
|
|
|
|
156
|
1 |
|
$stringified_possibilities = '[' . implode(', ', array_map(function($possibility) { |
|
157
|
1 |
|
return var_export($possibility, true); |
|
158
|
1 |
|
}, $this->getPossibilities()) ) .']'; |
|
159
|
|
|
|
|
160
|
1 |
|
return "['{$this->getField()}', '$operator', $stringified_possibilities]"; |
|
161
|
|
|
} |
|
162
|
|
|
catch (\LogicException $e) { |
|
163
|
|
|
return parent::toString(); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/**/ |
|
168
|
|
|
} |
|
169
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.