Failed Conditions
Pull Request — master (#4314)
by Owen
11:26
created

Conditional::setPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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