Test Failed
Push — master ( 6c792f...176748 )
by Jean
02:26
created

AboveOrEqualRule::getField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace JClaveau\LogicalFilter\Rule;
3
4
/**
5
 * a >= x
6
 */
7
class AboveOrEqualRule extends OrRule
8
{
9
    use Trait_RuleWithField;
10
11
    /** @var string operator */
12
    const operator = '>=';
13
14
    /** @var mixed minimum */
15
    protected $minimum;
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, $minimum, array $options=[])
22
    {
23
        if (!empty($options)) {
24
            $this->setOptions($options);
25
        }
26
27
        $this->field   = $field;
28
        $this->minimum = $minimum;
29
    }
30
31
    /**
32
     * @return mixed The minimum for the field of this rule
33
     */
34
    public function getMinimum()
35
    {
36
        return $this->minimum;
37
    }
38
39
    /**
40
     * Defines the minimum of the current rule
41
     *
42
     * @param  mixed $minimum
43
     * @return BelowOrEqualRule $this
44
     */
45
    public function setMinimum($minimum)
46
    {
47
        if ($minimum === null) {
48
            throw new \InvalidArgumentException(
49
                "The minimum of a below or equal rule cannot be null"
50
            );
51
        }
52
53
        if ($this->minimum == $minimum)
54
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type JClaveau\LogicalFilter\Rule\AboveOrEqualRule which is incompatible with the documented return type JClaveau\LogicalFilter\Rule\BelowOrEqualRule.
Loading history...
55
56
        $this->minimum = $minimum;
57
        $this->flushCache();
58
59
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type JClaveau\LogicalFilter\Rule\AboveOrEqualRule which is incompatible with the documented return type JClaveau\LogicalFilter\Rule\BelowOrEqualRule.
Loading history...
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function isNormalizationAllowed(array $contextual_options=[])
66
    {
67
        return $this->getOption('above_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 AboveRule($this->field, $this->minimum),
81
            new EqualRule($this->field, $this->minimum),
82
        ];
83
84
        return $this->cache['operands'] = $operands;
85
    }
86
87
    /**
88
     * Set the minimum 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 AboveRule) {
102
                $aboveRuleField = $operand->getField();
103
                $aboveRuleValue = $operand->getLowerLimit();
104
            }
105
        }
106
107
        if (    count($operands) != 2
108
            || !isset($equalRuleValue)
109
            || !isset($aboveRuleValue)
110
            || $aboveRuleValue != $equalRuleValue
111
            || $aboveRuleField != $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 $aboveRuleField 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 > minimum || field = minimum) instead of:\n"
115
                .var_export($operands, true)
116
            );
117
        }
118
119
        $this->setMinimum($aboveRuleValue);
120
        $this->setField($aboveRuleField);
121
122
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type JClaveau\LogicalFilter\Rule\AboveOrEqualRule which is incompatible with the documented return type JClaveau\LogicalFilter\Rule\BelowOrEqualRule.
Loading history...
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function getValues()
129
    {
130
        return $this->getMinimum();
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