tablepress
/
libraries
/
vendor
/
PhpSpreadsheet
/
Style
/
ConditionalFormatting
/
CellStyleAssessor.php
CellStyleAssessor.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Style/ConditionalFormatting/CellStyleAssessor.php
| 1 | <?php |
| 2 | |
| 3 | namespace TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting; |
| 4 | |
| 5 | use TablePress\PhpOffice\PhpSpreadsheet\Cell\Cell; |
| 6 | use TablePress\PhpOffice\PhpSpreadsheet\Style\Conditional; |
| 7 | use TablePress\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 | public function __construct(Cell $cell, string $conditionalRange) |
| 18 | { |
| 19 | $this->cell = $cell; |
| 20 | $this->cellMatcher = new CellMatcher($cell, $conditionalRange); |
| 21 | $this->styleMerger = new StyleMerger($cell->getStyle()); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @param Conditional[] $conditionalStyles |
| 26 | */ |
| 27 | public function matchConditions(array $conditionalStyles = []): Style |
| 28 | { |
| 29 | foreach ($conditionalStyles as $conditional) { |
| 30 | if ($this->cellMatcher->evaluateConditional($conditional) === true) { |
| 31 | // Merging the conditional style into the base style goes in here |
| 32 | $this->styleMerger->mergeStyle($conditional->getStyle($this->cell->getValue())); |
| 33 | if ($conditional->getStopIfTrue() === true) { |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return $this->styleMerger->getStyle(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param Conditional[] $conditionalStyles |
| 44 | */ |
| 45 | public function matchConditionsReturnNullIfNoneMatched(array $conditionalStyles, string $cellData, bool $stopAtFirstMatch = false): ?Style |
| 46 | { |
| 47 | $matched = false; |
| 48 | $value = (float) $cellData; |
| 49 | foreach ($conditionalStyles as $conditional) { |
| 50 | if ($this->cellMatcher->evaluateConditional($conditional) === true) { |
| 51 | $matched = true; |
| 52 | // Merging the conditional style into the base style goes in here |
| 53 | $this->styleMerger->mergeStyle($conditional->getStyle($value)); |
| 54 | if ($conditional->getStopIfTrue() === true || $stopAtFirstMatch) { |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | if ($matched) { |
| 60 | return $this->styleMerger->getStyle(); |
| 61 | } |
| 62 | |
| 63 | return null; |
| 64 | } |
| 65 | } |
| 66 |