PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Style / ConditionalFormatting / Wizard / TextValue.php

TextValue.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Style/ConditionalFormatting/Wizard/TextValue.php

178 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6 use TablePress\PhpOffice\PhpSpreadsheet\Exception;
7 use TablePress\PhpOffice\PhpSpreadsheet\Style\Conditional;
8 use TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
9
10 /**
11 * @method TextValue contains(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
12 * @method TextValue doesNotContain(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
13 * @method TextValue doesntContain(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
14 * @method TextValue beginsWith(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
15 * @method TextValue startsWith(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
16 * @method TextValue endsWith(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
17 */
18 class TextValue extends WizardAbstract implements WizardInterface
19 {
20 protected const MAGIC_OPERATIONS = [
21 'contains' => Conditional::OPERATOR_CONTAINSTEXT,
22 'doesntContain' => Conditional::OPERATOR_NOTCONTAINS,
23 'doesNotContain' => Conditional::OPERATOR_NOTCONTAINS,
24 'beginsWith' => Conditional::OPERATOR_BEGINSWITH,
25 'startsWith' => Conditional::OPERATOR_BEGINSWITH,
26 'endsWith' => Conditional::OPERATOR_ENDSWITH,
27 ];
28
29 protected const OPERATORS = [
30 Conditional::OPERATOR_CONTAINSTEXT => Conditional::CONDITION_CONTAINSTEXT,
31 Conditional::OPERATOR_NOTCONTAINS => Conditional::CONDITION_NOTCONTAINSTEXT,
32 Conditional::OPERATOR_BEGINSWITH => Conditional::CONDITION_BEGINSWITH,
33 Conditional::OPERATOR_ENDSWITH => Conditional::CONDITION_ENDSWITH,
34 ];
35
36 protected const EXPRESSIONS = [
37 Conditional::OPERATOR_CONTAINSTEXT => 'NOT(ISERROR(SEARCH(%s,%s)))',
38 Conditional::OPERATOR_NOTCONTAINS => 'ISERROR(SEARCH(%s,%s))',
39 Conditional::OPERATOR_BEGINSWITH => 'LEFT(%s,LEN(%s))=%s',
40 Conditional::OPERATOR_ENDSWITH => 'RIGHT(%s,LEN(%s))=%s',
41 ];
42
43 protected string $operator;
44
45 protected string $operand;
46
47 protected string $operandValueType;
48
49 public function __construct(string $cellRange)
50 {
51 parent::__construct($cellRange);
52 }
53
54 protected function operator(string $operator): void
55 {
56 if (!isset(self::OPERATORS[$operator])) {
57 // should not happen - compareKeys confirms
58 throw new Exception('Invalid Operator for Text Value CF Rule Wizard'); // @codeCoverageIgnore
59 }
60
61 $this->operator = $operator;
62 }
63
64 protected function operand(string $operand, string $operandValueType = Wizard::VALUE_TYPE_LITERAL): void
65 {
66 $operand = $this->validateOperand($operand, $operandValueType);
67
68 $this->operand = $operand;
69 $this->operandValueType = $operandValueType;
70 }
71
72 protected function wrapValue(string $value): string
73 {
74 return '"' . $value . '"';
75 }
76
77 protected function setExpression(): void
78 {
79 $operand = $this->operandValueType === Wizard::VALUE_TYPE_LITERAL
80 ? $this->wrapValue(str_replace('"', '""', $this->operand))
81 : $this->cellConditionCheck($this->operand);
82
83 if (
84 $this->operator === Conditional::OPERATOR_CONTAINSTEXT
85 || $this->operator === Conditional::OPERATOR_NOTCONTAINS
86 ) {
87 $this->expression = sprintf(self::EXPRESSIONS[$this->operator], $operand, $this->referenceCell);
88 } else {
89 $this->expression = sprintf(self::EXPRESSIONS[$this->operator], $this->referenceCell, $operand, $operand);
90 }
91 }
92
93 public function getConditional(): Conditional
94 {
95 $this->setExpression();
96
97 $conditional = new Conditional();
98 $conditional->setConditionType(self::OPERATORS[$this->operator]);
99 $conditional->setOperatorType($this->operator);
100 $conditional->setText(
101 $this->operandValueType !== Wizard::VALUE_TYPE_LITERAL
102 ? $this->cellConditionCheck($this->operand)
103 : $this->operand
104 );
105 $conditional->setConditions([$this->expression]);
106 $conditional->setStyle($this->getStyle());
107 $conditional->setStopIfTrue($this->getStopIfTrue());
108
109 return $conditional;
110 }
111
112 public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
113 {
114 if (!in_array($conditional->getConditionType(), self::OPERATORS, true)) {
115 throw new Exception('Conditional is not a Text Value CF Rule conditional');
116 }
117
118 $wizard = new self($cellRange);
119 $wizard->operator = (string) array_search($conditional->getConditionType(), self::OPERATORS, true);
120 $wizard->style = $conditional->getStyle();
121 $wizard->stopIfTrue = $conditional->getStopIfTrue();
122
123 // Best-guess to try and identify if the text is a string literal, a cell reference or a formula?
124 $wizard->operandValueType = Wizard::VALUE_TYPE_LITERAL;
125 $condition = $conditional->getText();
126 if (preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '$/i', $condition)) {
127 $wizard->operandValueType = Wizard::VALUE_TYPE_CELL;
128 $condition = self::reverseAdjustCellRef($condition, $cellRange);
129 } elseif (
130 preg_match('/\(\)/', $condition)
131 || preg_match('/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/i', $condition)
132 ) {
133 $wizard->operandValueType = Wizard::VALUE_TYPE_FORMULA;
134 }
135 $wizard->operand = $condition;
136
137 return $wizard;
138 }
139
140 /**
141 * @param mixed[] $arguments
142 */
143 public function __call(string $methodName, array $arguments): self
144 {
145 if (!isset(self::MAGIC_OPERATIONS[$methodName])) {
146 throw new Exception('Invalid Operation for Text Value CF Rule Wizard');
147 }
148
149 $this->operator(self::MAGIC_OPERATIONS[$methodName]);
150 //$this->operand(...$arguments);
151 if (count($arguments) < 2) {
152 /** @var string */
153 $arg0 = $arguments[0];
154 $this->operand($arg0);
155 } else {
156 /** @var string */
157 $arg0 = $arguments[0];
158 /** @var string */
159 $arg1 = $arguments[1];
160 $this->operand($arg0, $arg1);
161 }
162
163 return $this;
164 }
165
166 /** @internal */
167 public static function compareKeys(): bool
168 {
169 $retVal = true;
170 $array = array_keys(self::OPERATORS);
171 foreach ($array as $value) {
172 $retVal = $retVal && in_array($value, self::MAGIC_OPERATIONS, true);
173 }
174
175 return $retVal;
176 }
177 }
178