Passed
Push — master ( d88efc...653645 )
by
unknown
12:43 queued 21s
created

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