Failed Conditions
Push — master ( 8e3417...f52ae2 )
by
unknown
18:26 queued 07:14
created

CellStyleAssessor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 11
eloc 24
c 0
b 0
f 0
dl 0
loc 55
rs 10
ccs 22
cts 23
cp 0.9565

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A matchConditions() 0 13 4
A matchConditionsReturnNullIfNoneMatched() 0 19 6
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 12
    public function __construct(Cell $cell, string $conditionalRange)
18
    {
19 12
        $this->cell = $cell;
20 12
        $this->cellMatcher = new CellMatcher($cell, $conditionalRange);
21 12
        $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 8
    public function matchConditionsReturnNullIfNoneMatched(array $conditionalStyles, string $cellData, bool $stopAtFirstMatch = false): ?Style
46
    {
47 8
        $matched = false;
48 8
        $value = (float) $cellData;
49 8
        foreach ($conditionalStyles as $conditional) {
50 8
            if ($this->cellMatcher->evaluateConditional($conditional) === true) {
51 8
                $matched = true;
52
                // Merging the conditional style into the base style goes in here
53 8
                $this->styleMerger->mergeStyle($conditional->getStyle($value));
54 8
                if ($conditional->getStopIfTrue() === true || $stopAtFirstMatch) {
55 8
                    break;
56
                }
57
            }
58
        }
59 8
        if ($matched) {
60 8
            return $this->styleMerger->getStyle();
61
        }
62
63 4
        return null;
64
    }
65
}
66