Completed
Push — master ( d08653...6fb3d8 )
by
unknown
27s queued 21s
created

Conditional::isValidConditionType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
use PhpOffice\PhpSpreadsheet\IComparable;
6
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar;
7
8
class Conditional implements IComparable
9
{
10
    // Condition types
11
    const CONDITION_NONE = 'none';
12
    const CONDITION_CELLIS = 'cellIs';
13
    const CONDITION_CONTAINSTEXT = 'containsText';
14
    const CONDITION_EXPRESSION = 'expression';
15
    const CONDITION_CONTAINSBLANKS = 'containsBlanks';
16
    const CONDITION_NOTCONTAINSBLANKS = 'notContainsBlanks';
17
    const CONDITION_DATABAR = 'dataBar';
18
    const CONDITION_NOTCONTAINSTEXT = 'notContainsText';
19
20
    private const CONDITION_TYPES = [
21
        self::CONDITION_CELLIS,
22
        self::CONDITION_CONTAINSBLANKS,
23
        self::CONDITION_CONTAINSTEXT,
24
        self::CONDITION_DATABAR,
25
        self::CONDITION_EXPRESSION,
26
        self::CONDITION_NONE,
27
        self::CONDITION_NOTCONTAINSBLANKS,
28
        self::CONDITION_NOTCONTAINSTEXT,
29
    ];
30
31
    // Operator types
32
    const OPERATOR_NONE = '';
33
    const OPERATOR_BEGINSWITH = 'beginsWith';
34
    const OPERATOR_ENDSWITH = 'endsWith';
35
    const OPERATOR_EQUAL = 'equal';
36
    const OPERATOR_GREATERTHAN = 'greaterThan';
37
    const OPERATOR_GREATERTHANOREQUAL = 'greaterThanOrEqual';
38
    const OPERATOR_LESSTHAN = 'lessThan';
39
    const OPERATOR_LESSTHANOREQUAL = 'lessThanOrEqual';
40
    const OPERATOR_NOTEQUAL = 'notEqual';
41
    const OPERATOR_CONTAINSTEXT = 'containsText';
42
    const OPERATOR_NOTCONTAINS = 'notContains';
43
    const OPERATOR_BETWEEN = 'between';
44
    const OPERATOR_NOTBETWEEN = 'notBetween';
45
46
    /**
47
     * Condition type.
48
     *
49
     * @var string
50
     */
51
    private $conditionType = self::CONDITION_NONE;
52
53
    /**
54
     * Operator type.
55
     *
56
     * @var string
57
     */
58
    private $operatorType = self::OPERATOR_NONE;
59
60
    /**
61
     * Text.
62
     *
63
     * @var string
64
     */
65
    private $text;
66
67
    /**
68
     * Stop on this condition, if it matches.
69
     *
70
     * @var bool
71
     */
72
    private $stopIfTrue = false;
73
74
    /**
75
     * Condition.
76
     *
77
     * @var string[]
78
     */
79
    private $condition = [];
80
81
    /**
82
     * @var ConditionalDataBar
83
     */
84 17
    private $dataBar;
85
86
    /**
87 17
     * Style.
88 17
     *
89
     * @var Style
90
     */
91
    private $style;
92
93
    /**
94
     * Create a new Conditional.
95 13
     */
96
    public function __construct()
97 13
    {
98
        // Initialise values
99
        $this->style = new Style(false, true);
100
    }
101
102
    /**
103
     * Get Condition type.
104
     *
105
     * @return string
106
     */
107 17
    public function getConditionType()
108
    {
109 17
        return $this->conditionType;
110
    }
111 17
112
    /**
113
     * Set Condition type.
114
     *
115
     * @param string $pValue Condition type, see self::CONDITION_*
116
     *
117
     * @return $this
118
     */
119 11
    public function setConditionType($pValue)
120
    {
121 11
        $this->conditionType = $pValue;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Get Operator type.
128
     *
129
     * @return string
130
     */
131 17
    public function getOperatorType()
132
    {
133 17
        return $this->operatorType;
134
    }
135 17
136
    /**
137
     * Set Operator type.
138
     *
139
     * @param string $pValue Conditional operator type, see self::OPERATOR_*
140
     *
141
     * @return $this
142
     */
143 1
    public function setOperatorType($pValue)
144
    {
145 1
        $this->operatorType = $pValue;
146
147
        return $this;
148
    }
149
150
    /**
151
     * Get text.
152
     *
153
     * @return string
154
     */
155 1
    public function getText()
156
    {
157 1
        return $this->text;
158
    }
159 1
160
    /**
161
     * Set text.
162
     *
163
     * @param string $value
164
     *
165
     * @return $this
166
     */
167 9
    public function setText($value)
168
    {
169 9
        $this->text = $value;
170
171
        return $this;
172
    }
173
174
    /**
175
     * Get StopIfTrue.
176
     *
177
     * @return bool
178
     */
179 1
    public function getStopIfTrue()
180
    {
181 1
        return $this->stopIfTrue;
182
    }
183 1
184
    /**
185
     * Set StopIfTrue.
186
     *
187
     * @param bool $value
188
     *
189
     * @return $this
190
     */
191 10
    public function setStopIfTrue($value)
192
    {
193 10
        $this->stopIfTrue = $value;
194
195
        return $this;
196
    }
197
198
    /**
199
     * Get Conditions.
200
     *
201
     * @return string[]
202
     */
203 1
    public function getConditions()
204
    {
205 1
        return $this->condition;
206 1
    }
207
208 1
    /**
209
     * Set Conditions.
210 1
     *
211
     * @param bool|float|int|string|string[] $pValue Condition
212
     *
213
     * @return $this
214
     */
215
    public function setConditions($pValue)
216
    {
217
        if (!is_array($pValue)) {
218
            $pValue = [$pValue];
219
        }
220 17
        $this->condition = $pValue;
221
222 17
        return $this;
223
    }
224 17
225
    /**
226
     * Add Condition.
227
     *
228
     * @param string $pValue Condition
229
     *
230
     * @return $this
231
     */
232 15
    public function addCondition($pValue)
233
    {
234 15
        $this->condition[] = $pValue;
235
236
        return $this;
237
    }
238
239
    /**
240
     * Get Style.
241
     *
242
     * @return Style
243
     */
244 10
    public function getStyle()
245
    {
246 10
        return $this->style;
247
    }
248 10
249
    /**
250
     * Set Style.
251
     *
252
     * @param Style $pValue
253
     *
254
     * @return $this
255
     */
256 10
    public function setStyle(?Style $pValue = null)
257
    {
258 10
        $this->style = $pValue;
259
260
        return $this;
261
    }
262
263
    /**
264
     * get DataBar.
265
     *
266 3
     * @return ConditionalDataBar | null
267
     */
268 3
    public function getDataBar()
269
    {
270 3
        return $this->dataBar;
271
    }
272
273
    /**
274
     * set DataBar.
275
     *
276
     * @return $this
277
     */
278 9
    public function setDataBar(ConditionalDataBar $dataBar)
279
    {
280 9
        $this->dataBar = $dataBar;
281 9
282 9
        return $this;
283 9
    }
284 9
285 9
    /**
286
     * Get hash code.
287
     *
288
     * @return string Hash code
289
     */
290
    public function getHashCode()
291
    {
292 1
        return md5(
293
            $this->conditionType .
294 1
            $this->operatorType .
295 1
            implode(';', $this->condition) .
296 1
            $this->style->getHashCode() .
297 1
            __CLASS__
298
        );
299 1
    }
300
301
    /**
302 1
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
303
     */
304
    public function __clone()
305
    {
306
        $vars = get_object_vars($this);
307
        foreach ($vars as $key => $value) {
308
            if (is_object($value)) {
309
                $this->$key = clone $value;
310
            } else {
311
                $this->$key = $value;
312
            }
313
        }
314
    }
315
316
    /**
317
     * Verify if param is valid condition type.
318
     */
319
    public static function isValidConditionType(string $type): bool
320
    {
321
        return in_array($type, self::CONDITION_TYPES);
322
    }
323
}
324