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
|
|
|
|