Test Failed
Push — master ( 14bb9e...d96886 )
by Jean
04:16
created

BelowOrEqualRule::setMaximum()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
namespace JClaveau\LogicalFilter\Rule;
3
4
/**
5
 * a <= x
6
 */
7
class BelowOrEqualRule extends OrRule
8
{
9
    use Trait_RuleWithField;
10
11
    /** @var string operator */
12
    const operator = '<=';
13
14
    /** @var mixed maximum */
15
    protected $maximum;
16
17
    /**
18
     * @param string $field The field to apply the rule on.
19
     * @param array  $value The value the field can below to.
20
     */
21
    public function __construct($field, $maximum, array $options=[])
22
    {
23
        if (!empty($options)) {
24
            $this->setOptions($options);
25
        }
26
27
        $this->field   = $field;
28
        $this->maximum = $maximum;
29
    }
30
31
    /**
32
     * @return mixed The maximum for the field of this rule
33
     */
34
    public function getMaximum()
35
    {
36
        return $this->maximum;
37
    }
38
39
    /**
40
     * Defines the maximum of the current rule
41
     *
42
     * @param  mixed $maximum
43
     * @return BelowOrEqualRule $this
44
     */
45
    public function setMaximum($maximum)
46
    {
47
        if ($maximum === null) {
48
            throw new \InvalidArgumentException(
49
                "The maximum of a below or equal rule cannot be null"
50
            );
51
        }
52
53
        if ($this->maximum == $maximum)
54
            return $this;
55
56
        $this->maximum = $maximum;
57
        $this->flushCache();
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function isNormalizationAllowed(array $contextual_options=[])
66
    {
67
        return $this->getOption('below_or_equal.normalization', $contextual_options);
68
    }
69
70
    /**
71
     * @return array $operands
72
     */
73
    public function getOperands()
74
    {
75
        if (! empty($this->cache['operands'])) {
76
            return $this->cache['operands'];
77
        }
78
79
        $operands = [
80
            new BelowRule($this->field, $this->maximum),
81
            new EqualRule($this->field, $this->maximum),
82
        ];
83
84
        return $this->cache['operands'] = $operands;
85
    }
86
87
    /**
88
     * Set the maximum and the field of the current instance by giving
89
     * an array of opereands as parameter.
90
     *
91
     * @param  array            $operands
92
     * @return BelowOrEqualRule $this
93
     */
94
    public function setOperands(array $operands)
95
    {
96
        foreach ($operands as $operand) {
97
            if ($operand instanceof EqualRule) {
98
                $equalRuleField = $operand->getField();
99
                $equalRuleValue = $operand->getValue();
100
            }
101
            elseif ($operand instanceof BelowRule) {
102
                $belowRuleField = $operand->getField();
103
                $belowRuleValue = $operand->getUpperLimit();
104
            }
105
        }
106
107
        if (    count($operands) != 2
108
            || !isset($equalRuleValue)
109
            || !isset($belowRuleValue)
110
            || $belowRuleValue != $equalRuleValue
111
            || $belowRuleField != $equalRuleField
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $equalRuleField seems to be defined by a foreach iteration on line 96. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
Comprehensibility Best Practice introduced by
The variable $belowRuleField does not seem to be defined for all execution paths leading up to this point.
Loading history...
112
        ) {
113
            throw new \InvalidArgumentException(
114
                "Operands must be an array of two rules like (field < maximum || field = maximum) instead of:\n"
115
                .var_export($operands, true)
116
            );
117
        }
118
119
        $this->maximum = $belowRuleValue;
120
        $this->field   = $belowRuleField;
121
122
        return $this;
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function getValues()
129
    {
130
        return $this->getMaximum();
131
    }
132
133
    /**
134
     * @param array $options + show_instance=false Display the operator of the rule or its instance id
135
     *
136
     * @return array
137
     */
138
    public function toArray(array $options=[])
139
    {
140
        $default_options = [
141
            'show_instance' => false,
142
        ];
143
        foreach ($default_options as $default_option => &$default_value) {
144
            if (!isset($options[ $default_option ]))
145
                $options[ $default_option ] = $default_value;
146
        }
147
148
        try {
149
            // TODO replace this rule by a simple OrRule?
150
            return [
151
                $this->getField(),
152
                $options['show_instance'] ? $this->getInstanceId() : self::operator,
153
                $this->getValues(),
154
            ];
155
        }
156
        catch (\RuntimeException $e) {
157
            return parent::toArray();
158
        }
159
    }
160
161
    /**
162
     */
163
    public function toString(array $options=[])
164
    {
165
        if (isset($this->cache['string']))
166
            return $this->cache['string'];
167
168
        $operator = self::operator;
169
170
        return $this->cache['string'] = "['{$this->getField()}', '$operator', " . var_export($this->getValues(), true). "]";
171
    }
172
173
    /**/
174
}
175