1
|
|
|
<?php |
2
|
|
|
namespace JClaveau\LogicalFilter\Rule; |
3
|
|
|
|
4
|
|
|
abstract class AbstractRule implements \JsonSerializable |
5
|
|
|
{ |
6
|
|
|
use Trait_RuleWithOptions; |
7
|
|
|
use Trait_RuleWithCache; |
8
|
|
|
use Trait_ExportableRule; |
9
|
|
|
use Trait_RuleFactory; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Clones the rule with a chained syntax. |
13
|
|
|
* |
14
|
|
|
* @return AbstractRule A copy of the current instance. |
15
|
|
|
*/ |
16
|
135 |
|
public function copy() |
17
|
|
|
{ |
18
|
135 |
|
return clone $this; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @deprecated addMinimalCase |
23
|
|
|
*/ |
24
|
4 |
|
protected function forceLogicalCore() |
25
|
|
|
{ |
26
|
4 |
|
return $this->addMinimalCase(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Forces the two firsts levels of the tree to be an OrRule having |
31
|
|
|
* only AndRules as operands: |
32
|
|
|
* ['field', '=', '1'] <=> ['or', ['and', ['field', '=', '1']]] |
33
|
|
|
* As a simplified ruleTree will alwways be reduced to this structure |
34
|
|
|
* with no suboperands others than atomic ones or a simpler one like: |
35
|
|
|
* ['or', ['field', '=', '1'], ['field2', '>', '3']] |
36
|
|
|
* |
37
|
|
|
* This helpes to ease the result of simplify() |
38
|
|
|
* |
39
|
|
|
* @return OrRule |
40
|
|
|
*/ |
41
|
48 |
|
public function addMinimalCase() |
42
|
|
|
{ |
43
|
|
|
// Simplification step is required to call hasSolution() on the |
44
|
|
|
// returned OrRule value |
45
|
48 |
|
if ($this instanceof AndRule || $this instanceof OrRule) { |
46
|
33 |
|
$simplification_step_to_keep = $this->getSimplificationStep(); |
|
|
|
|
47
|
33 |
|
} |
48
|
19 |
|
elseif ($this->hasSolution()) { |
|
|
|
|
49
|
18 |
|
$simplification_step_to_keep = AbstractOperationRule::simplified; |
50
|
18 |
|
} |
51
|
|
|
else { |
52
|
1 |
|
$simplification_step_to_keep = null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ( $this instanceof AbstractAtomicRule |
56
|
48 |
|
|| $this instanceof NotRule |
57
|
37 |
|
|| $this instanceof InRule |
58
|
33 |
|
|| ! $this->isNormalizationAllowed([]) |
|
|
|
|
59
|
48 |
|
) { |
60
|
23 |
|
$ruleTree = new OrRule([ |
61
|
23 |
|
new AndRule([ |
62
|
23 |
|
$this, |
63
|
23 |
|
]), |
64
|
23 |
|
]); |
65
|
23 |
|
} |
66
|
29 |
|
elseif ($this instanceof AndRule) { |
67
|
9 |
|
$ruleTree = new OrRule([ |
68
|
9 |
|
$this, |
69
|
9 |
|
]); |
70
|
9 |
|
} |
71
|
21 |
|
elseif ($this instanceof OrRule) { |
72
|
21 |
|
foreach ($this->operands as $i => $operand) { |
73
|
21 |
|
if (! $operand instanceof AndRule) { |
74
|
11 |
|
$this->operands[$i] = new AndRule([$operand]); |
|
|
|
|
75
|
11 |
|
} |
76
|
21 |
|
} |
77
|
21 |
|
$ruleTree = $this; |
78
|
21 |
|
} |
79
|
|
|
else { |
80
|
|
|
throw new \LogicException( |
81
|
|
|
"Unhandled type of simplified rules provided for conversion: " |
82
|
|
|
.$this |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
48 |
|
if ($simplification_step_to_keep) { |
87
|
44 |
|
foreach ($operands = $ruleTree->getOperands() as $andOperand) { |
88
|
44 |
|
if (! $andOperand instanceof AndRule) { |
89
|
|
|
throw new \LogicException( |
90
|
|
|
"A rule is intended to be an and case: \n" |
91
|
|
|
.$andOperand |
92
|
|
|
."\nof:\n" |
93
|
|
|
.$ruleTree |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
44 |
|
$andOperand->moveSimplificationStepForward($simplification_step_to_keep, [], true); |
98
|
44 |
|
} |
99
|
44 |
|
$ruleTree->setOperands($operands); |
100
|
44 |
|
$ruleTree->moveSimplificationStepForward($simplification_step_to_keep, [], true); |
101
|
44 |
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
48 |
|
return $ruleTree; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/**/ |
108
|
|
|
} |
109
|
|
|
|
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: