tablepress
/
libraries
/
vendor
/
PhpSpreadsheet
/
Style
/
ConditionalFormatting
/
MergedCellStyle.php
MergedCellStyle.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Style/ConditionalFormatting/MergedCellStyle.php
| 1 | <?php |
| 2 | |
| 3 | namespace TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting; |
| 4 | |
| 5 | use TablePress\PhpOffice\PhpSpreadsheet\Style\Fill; |
| 6 | use TablePress\PhpOffice\PhpSpreadsheet\Style\Style; |
| 7 | use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 8 | |
| 9 | class MergedCellStyle |
| 10 | { |
| 11 | private bool $matched = false; |
| 12 | |
| 13 | /** |
| 14 | * Indicate whether the last call to getMergedStyle found |
| 15 | * any conditional or table styles affecting the cell in question. |
| 16 | */ |
| 17 | public function getMatched(): bool |
| 18 | { |
| 19 | return $this->matched; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Return a style that combines the base style for a cell |
| 24 | * with any conditional or table styles applicable to the cell. |
| 25 | * |
| 26 | * @param bool $tableFormats True/false to indicate whether |
| 27 | * custom table styles should be considered. |
| 28 | * Note that builtin table styles are not supported. |
| 29 | * @param bool $conditionals True/false to indicate whether |
| 30 | * conditional styles should be considered. |
| 31 | */ |
| 32 | public function getMergedStyle(Worksheet $worksheet, string $coordinate, bool $tableFormats = true, bool $conditionals = true, ?bool $builtInTableStyles = null): Style |
| 33 | { |
| 34 | $builtInTableStyles ??= $tableFormats; |
| 35 | $this->matched = false; |
| 36 | $styleMerger = new StyleMerger($worksheet->getStyle($coordinate)); |
| 37 | if ($tableFormats) { |
| 38 | $this->assessTables($worksheet, $coordinate, $styleMerger); |
| 39 | } |
| 40 | if ($builtInTableStyles) { |
| 41 | $this->assessBuiltinTables($worksheet, $coordinate, $styleMerger); |
| 42 | } |
| 43 | if ($conditionals) { |
| 44 | $this->assessConditionals($worksheet, $coordinate, $styleMerger); |
| 45 | } |
| 46 | |
| 47 | return $styleMerger->getStyle(); |
| 48 | } |
| 49 | |
| 50 | private function assessTables(Worksheet $worksheet, string $coordinate, StyleMerger $styleMerger): void |
| 51 | { |
| 52 | $tables = $worksheet->getTablesWithStylesForCell($worksheet->getCell($coordinate)); |
| 53 | foreach ($tables as $ts) { |
| 54 | $dxfsTableStyle = $ts->getStyle()->getTableDxfsStyle(); |
| 55 | if ($dxfsTableStyle !== null) { |
| 56 | $tableRow = $ts->getRowNumber($coordinate); |
| 57 | if ($tableRow === 0 && $dxfsTableStyle->getHeaderRowStyle() !== null) { |
| 58 | $styleMerger->mergeStyle( |
| 59 | $dxfsTableStyle->getHeaderRowStyle() |
| 60 | ); |
| 61 | $this->matched = true; |
| 62 | } elseif ($tableRow % 2 === 1 && $dxfsTableStyle->getFirstRowStripeStyle() !== null) { |
| 63 | $styleMerger->mergeStyle( |
| 64 | $dxfsTableStyle->getFirstRowStripeStyle() |
| 65 | ); |
| 66 | $this->matched = true; |
| 67 | } elseif ($tableRow % 2 === 0 && $dxfsTableStyle->getSecondRowStripeStyle() !== null) { |
| 68 | $styleMerger->mergeStyle( |
| 69 | $dxfsTableStyle->getSecondRowStripeStyle() |
| 70 | ); |
| 71 | $this->matched = true; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private static ?Style $headerStyle = null; |
| 78 | |
| 79 | private static ?Style $firstRowStyle = null; |
| 80 | |
| 81 | private function assessBuiltinTables(Worksheet $worksheet, string $coordinate, StyleMerger $styleMerger): void |
| 82 | { |
| 83 | if (self::$headerStyle === null) { |
| 84 | self::$headerStyle = new Style(); |
| 85 | self::$headerStyle->getFill() |
| 86 | ->setFillType(Fill::FILL_SOLID) |
| 87 | ->getEndColor() |
| 88 | ->setArgb('FF000000'); |
| 89 | self::$headerStyle->getFill()->getStartColor() |
| 90 | ->setArgb('FF000000'); |
| 91 | self::$headerStyle->getFont() |
| 92 | ->getColor()->setRgb('FFFFFF'); |
| 93 | } |
| 94 | if (self::$firstRowStyle === null) { |
| 95 | self::$firstRowStyle = new Style(); |
| 96 | self::$firstRowStyle->getFill() |
| 97 | ->setFillType(Fill::FILL_SOLID) |
| 98 | ->getEndColor() |
| 99 | ->setArgb('FFD9D9D9'); |
| 100 | self::$firstRowStyle->getFill()->getStartColor() |
| 101 | ->setArgb('FFD9D9D9'); |
| 102 | } |
| 103 | $tables = $worksheet->getTablesWithoutStylesForCell($worksheet->getCell($coordinate)); |
| 104 | foreach ($tables as $table) { |
| 105 | $tableRow = $table->getRowNumber($coordinate); |
| 106 | if ($tableRow === 0 && $table->getShowHeaderRow()) { |
| 107 | $styleMerger->mergeStyle(self::$headerStyle); |
| 108 | $this->matched = true; |
| 109 | } elseif ($tableRow % 2 === 1) { |
| 110 | $styleMerger->mergeStyle(self::$firstRowStyle); |
| 111 | $this->matched = true; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private function assessConditionals(Worksheet $worksheet, string $coordinate, StyleMerger $styleMerger): void |
| 117 | { |
| 118 | if ($worksheet->getConditionalRange($coordinate) !== null) { |
| 119 | $assessor = new CellStyleAssessor($worksheet->getCell($coordinate), $worksheet->getConditionalRange($coordinate)); |
| 120 | } else { |
| 121 | $assessor = new CellStyleAssessor($worksheet->getCell($coordinate), $coordinate); |
| 122 | } |
| 123 | $matchedStyle = $assessor |
| 124 | ->matchConditionsReturnNullIfNoneMatched( |
| 125 | $worksheet->getConditionalStyles($coordinate), |
| 126 | $worksheet->getCell($coordinate) |
| 127 | ->getCalculatedValueString(), |
| 128 | true |
| 129 | ); |
| 130 | if ($matchedStyle !== null) { |
| 131 | $this->matched = true; |
| 132 | $styleMerger->mergeStyle($matchedStyle); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 |