1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oliverde8\Component\RuleEngine\Rules; |
4
|
|
|
|
5
|
|
|
use Oliverde8\Component\RuleEngine\Exceptions\Condition\UnSupportedOperationException; |
6
|
|
|
use Oliverde8\Component\RuleEngine\Exceptions\RuleException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Condition |
10
|
|
|
* |
11
|
|
|
* @author de Cramer Oliver<[email protected]> |
12
|
|
|
* @copyright 2018 Oliverde8 |
13
|
|
|
* @package Oliverde8\Component\RuleEngine\Rules |
14
|
|
|
*/ |
15
|
|
|
class Condition extends AbstractRule |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @inheritdoc |
19
|
|
|
*/ |
20
|
6 |
|
public function apply($rowData, &$transformedData, $options = []) |
21
|
|
|
{ |
22
|
6 |
|
$valueToCmp = $this->applyRules($rowData, $transformedData, $options['if'], $options); |
|
|
|
|
23
|
6 |
|
$value = $this->applyRules($rowData, $transformedData, $options['value'], $options); |
|
|
|
|
24
|
|
|
|
25
|
6 |
|
$result = $this->compare($valueToCmp, $value, $options['operation'])? $options['then'] : $options['else']; |
26
|
|
|
|
27
|
5 |
|
return $this->applyRules($rowData, $transformedData, $result, $options); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Compare a value. |
32
|
|
|
* |
33
|
|
|
* @param mixed $value The value to compare. |
34
|
|
|
* @param mixed $with The value to compare the value with. |
35
|
|
|
* @param string $operation The operation to use for the comparison. |
36
|
|
|
* |
37
|
|
|
* @throws UnSupportedOperationException |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
6 |
|
protected function compare($value, $with, $operation) |
42
|
|
|
{ |
43
|
|
|
switch ($operation) { |
44
|
6 |
|
case 'eq' : |
45
|
2 |
|
return $value == $with; |
46
|
4 |
|
case 'neq' : |
47
|
1 |
|
return $value != $with; |
48
|
3 |
|
case 'in' : |
49
|
2 |
|
return in_array($value, $with); |
50
|
|
|
default: |
51
|
1 |
|
throw new UnSupportedOperationException("Operation '$operation' is not supported!"); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Apply sub rules. |
57
|
|
|
* |
58
|
|
|
* @param array $rowData Row to apply the transformation on |
59
|
|
|
* @param array $transformedData Data already transformed. |
60
|
|
|
* @param array $rules Rule definition to apply. |
61
|
|
|
* @param array $options Options for the rules. |
62
|
|
|
* |
63
|
|
|
* @return null |
64
|
|
|
* @throws RuleException |
65
|
|
|
*/ |
66
|
6 |
|
protected function applyRules($rowData, $transformedData, $rules, $options) |
67
|
|
|
{ |
68
|
|
|
// Remove fields used by the current rule and not needed by childs. |
69
|
6 |
|
unset($options['if']); |
70
|
6 |
|
unset($options['value']); |
71
|
6 |
|
unset($options['operation']); |
72
|
6 |
|
unset($options['then']); |
73
|
6 |
|
unset($options['else']); |
74
|
|
|
|
75
|
6 |
|
return $this->ruleApplier->apply($rowData, $transformedData, $rules, $options); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
6 |
|
public function validate($options) |
82
|
|
|
{ |
83
|
6 |
|
$this->requireOption('if', $options); |
84
|
5 |
|
$this->requireOption('value', $options); |
85
|
5 |
|
$this->requireOption('operation', $options); |
86
|
5 |
|
$this->requireOption('then', $options); |
87
|
5 |
|
$this->requireOption('else', $options); |
88
|
5 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
2 |
|
public function getRuleCode() |
94
|
|
|
{ |
95
|
2 |
|
return 'condition'; |
96
|
|
|
} |
97
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.