Failed Conditions
Pull Request — master (#4412)
by
unknown
22:45 queued 07:48
created

CellStyleAssessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Cell;
6
use PhpOffice\PhpSpreadsheet\Style\Conditional;
7
use PhpOffice\PhpSpreadsheet\Style\Style;
8
9
class CellStyleAssessor
10
{
11
    protected CellMatcher $cellMatcher;
12
13
    protected StyleMerger $styleMerger;
14
15
    protected Cell $cell;
16
17 11
    public function __construct(Cell $cell, string $conditionalRange)
18
    {
19 11
        $this->cell = $cell;
20 11
        $this->cellMatcher = new CellMatcher($cell, $conditionalRange);
21 11
        $this->styleMerger = new StyleMerger($cell->getStyle());
22
    }
23
24
    /**
25
     * @param Conditional[] $conditionalStyles
26
     */
27 4
    public function matchConditions(array $conditionalStyles = []): Style
28
    {
29 4
        foreach ($conditionalStyles as $conditional) {
30 4
            if ($this->cellMatcher->evaluateConditional($conditional) === true) {
31
                // Merging the conditional style into the base style goes in here
32 3
                $this->styleMerger->mergeStyle($conditional->getStyle($this->cell->getValue()));
33 3
                if ($conditional->getStopIfTrue() === true) {
34
                    break;
35
                }
36
            }
37
        }
38
39 4
        return $this->styleMerger->getStyle();
40
    }
41
42
    /**
43
     * @param Conditional[] $conditionalStyles
44
     */
45 7
    public function matchConditionsReturnNullIfNoneMatched(array $conditionalStyles, string $cellData, bool $stopAtFirstMatch = false): ?Style
46
    {
47 7
        $matched = false;
48 7
        $value = (float) $cellData;
49 7
        foreach ($conditionalStyles as $conditional) {
50 7
            if ($this->cellMatcher->evaluateConditional($conditional) === true) {
51 7
                $matched = true;
52
                // Merging the conditional style into the base style goes in here
53 7
                $this->styleMerger->mergeStyle($conditional->getStyle($value));
54 7
                if ($conditional->getStopIfTrue() === true || $stopAtFirstMatch) {
55 7
                    break;
56
                }
57
            }
58
        }
59 7
        if ($matched) {
60 7
            return $this->styleMerger->getStyle();
61
        }
62
63 5
        return null;
64
    }
65
}
66