|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BetweenRule |
|
4
|
|
|
* |
|
5
|
|
|
* @package php-logical-filter |
|
6
|
|
|
* @author Jean Claveau |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace JClaveau\LogicalFilter\Rule; |
|
9
|
|
|
|
|
10
|
|
|
class BetweenRule extends AndRule |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var string operator */ |
|
13
|
|
|
const operator = '><'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
*/ |
|
17
|
16 |
|
public function __construct( $field, array $limits ) |
|
18
|
|
|
{ |
|
19
|
16 |
|
$this->addOperand( new AboveRule($field, $limits[0]) ); |
|
20
|
16 |
|
$this->addOperand( new BelowRule($field, $limits[1]) ); |
|
21
|
16 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return mixed |
|
25
|
|
|
*/ |
|
26
|
22 |
|
public function getMinimum() |
|
27
|
|
|
{ |
|
28
|
22 |
|
return $this->getOperandAt(0)->getMinimum(); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return mixed |
|
33
|
|
|
*/ |
|
34
|
22 |
|
public function getMaximum() |
|
35
|
|
|
{ |
|
36
|
22 |
|
return $this->getOperandAt(1)->getMaximum(); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
14 |
|
public function getValues() |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
14 |
|
$this->getMinimum(), |
|
46
|
14 |
|
$this->getMaximum(), |
|
47
|
14 |
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
*/ |
|
52
|
36 |
|
public function getField() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
36 |
|
$field1 = $this->getOperandAt(0)->getField(); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
36 |
|
if ( ! $this->getOperandAt(1)) { |
|
57
|
3 |
|
return $field1; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
36 |
|
$field2 = $this->getOperandAt(1)->getField(); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
36 |
|
if ($field1 != $field2) { |
|
63
|
|
|
// TODO if this case occures, the current object should be |
|
64
|
|
|
// replaced by a simple OrRule |
|
65
|
1 |
|
throw new \RuntimeException( |
|
66
|
1 |
|
"The field of the AboveRule (".var_export($field1, true).") " |
|
67
|
1 |
|
."and the field of the BelowRule (".var_export($field2, true).") " |
|
68
|
1 |
|
."of an ". __CLASS__ ." do not match anymore" |
|
69
|
1 |
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
35 |
|
return $field1; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Checks if the range is valid. |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function hasSolution(array $contextual_options=[]) |
|
81
|
|
|
{ |
|
82
|
1 |
|
return $this->getMaximum() > $this->getMinimum(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param array $options + show_instance=false Display the operator of the rule or its instance id |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
35 |
|
public function toArray(array $options=[]) |
|
91
|
|
|
{ |
|
92
|
|
|
$default_options = [ |
|
93
|
35 |
|
'show_instance' => false, |
|
94
|
35 |
|
]; |
|
95
|
35 |
|
foreach ($default_options as $default_option => &$default_value) { |
|
96
|
35 |
|
if ( ! isset($options[ $default_option ])) { |
|
97
|
35 |
|
$options[ $default_option ] = $default_value; |
|
98
|
35 |
|
} |
|
99
|
35 |
|
} |
|
100
|
|
|
|
|
101
|
35 |
|
$class = get_class($this); |
|
102
|
|
|
|
|
103
|
|
|
return [ |
|
104
|
35 |
|
$this->getField(), |
|
105
|
35 |
|
$options['show_instance'] ? $this->getInstanceId() : $class::operator, |
|
106
|
35 |
|
$this->getValues(), |
|
107
|
35 |
|
]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
*/ |
|
112
|
1 |
View Code Duplication |
public function toString(array $options=[]) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
try { |
|
115
|
1 |
|
$class = get_called_class(); |
|
116
|
|
|
|
|
117
|
1 |
|
$operator = $class::operator; |
|
118
|
|
|
|
|
119
|
1 |
|
$stringified_limits = '[' . implode(', ', array_map(function($limit) { |
|
120
|
1 |
|
return var_export($limit, true); |
|
121
|
1 |
|
}, $this->getValues()) ) .']'; |
|
122
|
|
|
|
|
123
|
1 |
|
return "['{$this->getField()}', '$operator', $stringified_limits]"; |
|
124
|
|
|
} |
|
125
|
|
|
catch (\LogicException $e) { |
|
126
|
|
|
return parent::toString(); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/**/ |
|
131
|
|
|
} |
|
132
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: