tablepress
/
libraries
/
vendor
/
PhpSpreadsheet
/
Style
/
ConditionalFormatting
/
Wizard
/
WizardAbstract.php
WizardAbstract.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Style/ConditionalFormatting/Wizard/WizardAbstract.php
| 1 | <?php |
| 2 | |
| 3 | namespace TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard; |
| 4 | |
| 5 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
| 6 | use TablePress\PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
| 7 | use TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard; |
| 8 | use TablePress\PhpOffice\PhpSpreadsheet\Style\Style; |
| 9 | |
| 10 | abstract class WizardAbstract |
| 11 | { |
| 12 | protected ?Style $style = null; |
| 13 | |
| 14 | protected string $expression; |
| 15 | |
| 16 | protected string $cellRange; |
| 17 | |
| 18 | protected string $referenceCell; |
| 19 | |
| 20 | protected int $referenceRow; |
| 21 | |
| 22 | protected bool $stopIfTrue = false; |
| 23 | |
| 24 | protected int $referenceColumn; |
| 25 | |
| 26 | public function __construct(string $cellRange) |
| 27 | { |
| 28 | $this->setCellRange($cellRange); |
| 29 | } |
| 30 | |
| 31 | public function getCellRange(): string |
| 32 | { |
| 33 | return $this->cellRange; |
| 34 | } |
| 35 | |
| 36 | public function setCellRange(string $cellRange): void |
| 37 | { |
| 38 | $this->cellRange = $cellRange; |
| 39 | $this->setReferenceCellForExpressions($cellRange); |
| 40 | } |
| 41 | |
| 42 | protected function setReferenceCellForExpressions(string $conditionalRange): void |
| 43 | { |
| 44 | $conditionalRange = Coordinate::splitRange(str_replace('$', '', strtoupper($conditionalRange))); |
| 45 | [$this->referenceCell] = $conditionalRange[0]; |
| 46 | |
| 47 | [$this->referenceColumn, $this->referenceRow] = Coordinate::indexesFromString($this->referenceCell); |
| 48 | } |
| 49 | |
| 50 | public function getStopIfTrue(): bool |
| 51 | { |
| 52 | return $this->stopIfTrue; |
| 53 | } |
| 54 | |
| 55 | public function setStopIfTrue(bool $stopIfTrue): void |
| 56 | { |
| 57 | $this->stopIfTrue = $stopIfTrue; |
| 58 | } |
| 59 | |
| 60 | public function getStyle(): Style |
| 61 | { |
| 62 | return $this->style ?? new Style(false, true); |
| 63 | } |
| 64 | |
| 65 | public function setStyle(Style $style): void |
| 66 | { |
| 67 | $this->style = $style; |
| 68 | } |
| 69 | |
| 70 | protected function validateOperand(string $operand, string $operandValueType = Wizard::VALUE_TYPE_LITERAL): string |
| 71 | { |
| 72 | if ( |
| 73 | $operandValueType === Wizard::VALUE_TYPE_LITERAL |
| 74 | && str_starts_with($operand, '"') |
| 75 | && str_ends_with($operand, '"') |
| 76 | ) { |
| 77 | $operand = str_replace('""', '"', (string) substr($operand, 1, -1)); |
| 78 | } elseif ($operandValueType === Wizard::VALUE_TYPE_FORMULA && str_starts_with($operand, '=')) { |
| 79 | $operand = (string) substr($operand, 1); |
| 80 | } |
| 81 | |
| 82 | return $operand; |
| 83 | } |
| 84 | |
| 85 | /** @param string[] $matches */ |
| 86 | protected static function reverseCellAdjustment(array $matches, int $referenceColumn, int $referenceRow): string |
| 87 | { |
| 88 | $worksheet = $matches[1]; |
| 89 | $column = $matches[6]; |
| 90 | $row = $matches[7]; |
| 91 | |
| 92 | if (!str_contains($column, '$')) { |
| 93 | $column = Coordinate::columnIndexFromString($column); |
| 94 | $column -= $referenceColumn - 1; |
| 95 | $column = Coordinate::stringFromColumnIndex($column); |
| 96 | } |
| 97 | |
| 98 | if (!str_contains($row, '$')) { |
| 99 | $row = (int) $row - ($referenceRow - 1); |
| 100 | } |
| 101 | |
| 102 | return "{$worksheet}{$column}{$row}"; |
| 103 | } |
| 104 | |
| 105 | public static function reverseAdjustCellRef(string $condition, string $cellRange): string |
| 106 | { |
| 107 | $conditionalRange = Coordinate::splitRange(str_replace('$', '', strtoupper($cellRange))); |
| 108 | [$referenceCell] = $conditionalRange[0]; |
| 109 | [$referenceColumnIndex, $referenceRow] = Coordinate::indexesFromString($referenceCell); |
| 110 | |
| 111 | $splitCondition = explode(Calculation::FORMULA_STRING_QUOTE, $condition); |
| 112 | $i = false; |
| 113 | foreach ($splitCondition as &$value) { |
| 114 | // Only count/replace in alternating array entries (ie. not in quoted strings) |
| 115 | $i = $i === false; |
| 116 | if ($i) { |
| 117 | $value = (string) preg_replace_callback( |
| 118 | '/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/i', |
| 119 | fn ($matches): string => self::reverseCellAdjustment($matches, $referenceColumnIndex, $referenceRow), |
| 120 | $value |
| 121 | ); |
| 122 | } |
| 123 | } |
| 124 | unset($value); |
| 125 | |
| 126 | // Then rebuild the condition string to return it |
| 127 | return implode(Calculation::FORMULA_STRING_QUOTE, $splitCondition); |
| 128 | } |
| 129 | |
| 130 | /** @param string[] $matches */ |
| 131 | protected function conditionCellAdjustment(array $matches): string |
| 132 | { |
| 133 | $worksheet = $matches[1]; |
| 134 | $column = $matches[6]; |
| 135 | $row = $matches[7]; |
| 136 | |
| 137 | if (!str_contains($column, '$')) { |
| 138 | $column = Coordinate::columnIndexFromString($column); |
| 139 | $column += $this->referenceColumn - 1; |
| 140 | $column = Coordinate::stringFromColumnIndex($column); |
| 141 | } |
| 142 | |
| 143 | if (!str_contains($row, '$')) { |
| 144 | $row = (int) $row + ($this->referenceRow - 1); |
| 145 | } |
| 146 | |
| 147 | return "{$worksheet}{$column}{$row}"; |
| 148 | } |
| 149 | |
| 150 | protected function cellConditionCheck(string $condition): string |
| 151 | { |
| 152 | $splitCondition = explode(Calculation::FORMULA_STRING_QUOTE, $condition); |
| 153 | $i = false; |
| 154 | foreach ($splitCondition as &$value) { |
| 155 | // Only count/replace in alternating array entries (ie. not in quoted strings) |
| 156 | $i = $i === false; |
| 157 | if ($i) { |
| 158 | $value = (string) preg_replace_callback( |
| 159 | '/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/i', |
| 160 | [$this, 'conditionCellAdjustment'], |
| 161 | $value |
| 162 | ); |
| 163 | } |
| 164 | } |
| 165 | unset($value); |
| 166 | |
| 167 | // Then rebuild the condition string to return it |
| 168 | return implode(Calculation::FORMULA_STRING_QUOTE, $splitCondition); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param mixed[] $conditions |
| 173 | * |
| 174 | * @return mixed[] |
| 175 | */ |
| 176 | protected function adjustConditionsForCellReferences(array $conditions): array |
| 177 | { |
| 178 | return array_map( |
| 179 | [$this, 'cellConditionCheck'], |
| 180 | $conditions |
| 181 | ); |
| 182 | } |
| 183 | } |
| 184 |