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 / Calculation / TextData / Text.php

Text.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/TextData/Text.php

258 lines 7.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\Calculation\TextData;
4
5 use TablePress\Composer\Pcre\Preg;
6 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
7 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Calculation;
8 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
9 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions;
10 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue;
11 use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper;
12
13 class Text
14 {
15 use ArrayEnabled;
16
17 /**
18 * LEN.
19 *
20 * @param mixed $value String Value
21 * Or can be an array of values
22 *
23 * @return array<mixed>|int|string If an array of values is passed for the argument, then the returned result
24 * will also be an array with matching dimensions
25 */
26 public static function length($value = '')
27 {
28 if (is_array($value)) {
29 return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
30 }
31
32 try {
33 $value = Helpers::extractString($value, true);
34 } catch (CalcExp $e) {
35 return $e->getMessage();
36 }
37
38 return mb_strlen($value, 'UTF-8');
39 }
40
41 /**
42 * Compares two text strings and returns TRUE if they are exactly the same, FALSE otherwise.
43 * EXACT is case-sensitive but ignores formatting differences.
44 * Use EXACT to test text being entered into a document.
45 *
46 * @param mixed $value1 String Value
47 * Or can be an array of values
48 * @param mixed $value2 String Value
49 * Or can be an array of values
50 *
51 * @return array<mixed>|bool|string If an array of values is passed for either of the arguments, then the returned result
52 * will also be an array with matching dimensions
53 */
54 public static function exact($value1, $value2)
55 {
56 if (is_array($value1) || is_array($value2)) {
57 return self::evaluateArrayArguments([self::class, __FUNCTION__], $value1, $value2);
58 }
59
60 try {
61 $value1 = Helpers::extractString($value1, true);
62 $value2 = Helpers::extractString($value2, true);
63 } catch (CalcExp $e) {
64 return $e->getMessage();
65 }
66
67 return $value2 === $value1;
68 }
69
70 /**
71 * T.
72 *
73 * @param mixed $testValue Value to check
74 * Or can be an array of values
75 *
76 * @return array<mixed>|string If an array of values is passed for the argument, then the returned result
77 * will also be an array with matching dimensions
78 */
79 public static function test($testValue = '')
80 {
81 if (is_array($testValue)) {
82 return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $testValue);
83 }
84
85 if (is_string($testValue)) {
86 return $testValue;
87 }
88
89 return '';
90 }
91
92 /**
93 * TEXTSPLIT.
94 *
95 * @param mixed $text the text that you're searching
96 * @param null|array<string>|string $columnDelimiter The text that marks the point where to spill the text across columns.
97 * Multiple delimiters can be passed as an array of string values
98 * @param null|array<string>|string $rowDelimiter The text that marks the point where to spill the text down rows.
99 * Multiple delimiters can be passed as an array of string values
100 * @param bool $ignoreEmpty Specify FALSE to create an empty cell when two delimiters are consecutive.
101 * true = create empty cells
102 * false = skip empty cells
103 * Defaults to TRUE, which creates an empty cell
104 * @param bool $matchMode Determines whether the match is case-sensitive or not.
105 * true = case-sensitive
106 * false = case-insensitive
107 * By default, a case-sensitive match is done.
108 * @param mixed $padding The value with which to pad the result.
109 * The default is #N/A.
110 *
111 * @return array<mixed>|string the array built from the text, split by the row and column delimiters, or an error string
112 */
113 public static function split($text, $columnDelimiter = null, $rowDelimiter = null, bool $ignoreEmpty = false, bool $matchMode = true, $padding = '#N/A')
114 {
115 $text = Functions::flattenSingleValue($text);
116 if (ErrorValue::isError($text, true)) {
117 return StringHelper::convertToString($text);
118 }
119
120 $flags = self::matchFlags($matchMode);
121
122 if ($rowDelimiter !== null) {
123 $delimiter = self::buildDelimiter($rowDelimiter);
124 $rows = ($delimiter === '()')
125 ? [$text]
126 : Preg::split("/{$delimiter}/{$flags}", StringHelper::convertToString($text));
127 } else {
128 $rows = [$text];
129 }
130
131 if ($ignoreEmpty === true) {
132 $rows = array_values(array_filter(
133 $rows,
134 fn ($row): bool => $row !== ''
135 ));
136 }
137
138 if ($columnDelimiter !== null) {
139 $delimiter = self::buildDelimiter($columnDelimiter);
140 array_walk(
141 $rows,
142 function (&$row) use ($delimiter, $flags, $ignoreEmpty): void {
143 $row = ($delimiter === '()')
144 ? [$row]
145 : Preg::split("/{$delimiter}/{$flags}", StringHelper::convertToString($row));
146 if ($ignoreEmpty === true) {
147 $row = array_values(array_filter(
148 $row,
149 fn ($value): bool => $value !== ''
150 ));
151 }
152 }
153 );
154 if ($ignoreEmpty === true) {
155 $rows = array_values(array_filter(
156 $rows,
157 fn ($row): bool => $row !== [] && $row !== ['']
158 ));
159 }
160 }
161
162 return self::applyPadding($rows, $padding);
163 }
164
165 /**
166 * @param mixed[] $rows
167 *
168 * @return mixed[]
169 * @param mixed $padding
170 */
171 private static function applyPadding(array $rows, $padding): array
172 {
173 $columnCount = array_reduce(
174 $rows,
175 fn (int $counter, array $row): int => max($counter, count($row)), //* @phpstan-ignore argument.type (I don't know)
176 0
177 );
178
179 return array_map(
180 fn (array $row): array => (count($row) < $columnCount) //* @phpstan-ignore argument.type (I don't know)
181 ? array_merge($row, array_fill(0, $columnCount - count($row), $padding))
182 : $row,
183 $rows
184 );
185 }
186
187 /**
188 * @param null|array<string>|string $delimiter the text that marks the point before which you want to split
189 * Multiple delimiters can be passed as an array of string values
190 */
191 private static function buildDelimiter($delimiter): string
192 {
193 $valueSet = Functions::flattenArray($delimiter);
194
195 if (is_array($delimiter) && count($valueSet) > 1) {
196 /** @var array<?string> $valueSet */
197 $quotedDelimiters = array_map(
198 fn (?string $delimiter): string => preg_quote($delimiter ?? '', '/'),
199 $valueSet
200 );
201 $delimiters = implode('|', $quotedDelimiters);
202
203 return '(' . $delimiters . ')';
204 }
205
206 return '(' . preg_quote(StringHelper::convertToString(Functions::flattenSingleValue($delimiter)), '/') . ')';
207 }
208
209 private static function matchFlags(bool $matchMode): string
210 {
211 return ($matchMode === true) ? 'miu' : 'mu';
212 }
213
214 /** @param mixed[][] $array */
215 public static function fromArray(array $array, int $format = 0): string
216 {
217 $result = [];
218 foreach ($array as $row) {
219 $cells = [];
220 foreach ($row as $cellValue) {
221 $value = ($format === 1) ? self::formatValueMode1($cellValue) : self::formatValueMode0($cellValue);
222 $cells[] = $value;
223 }
224 $result[] = implode(($format === 1) ? ',' : ', ', $cells);
225 }
226
227 $result = implode(($format === 1) ? ';' : ', ', $result);
228
229 return ($format === 1) ? '{' . $result . '}' : $result;
230 }
231
232 /**
233 * @param mixed $cellValue
234 */
235 private static function formatValueMode0($cellValue): string
236 {
237 if (is_bool($cellValue)) {
238 return Calculation::getLocaleBoolean($cellValue ? 'TRUE' : 'FALSE');
239 }
240
241 return StringHelper::convertToString($cellValue);
242 }
243
244 /**
245 * @param mixed $cellValue
246 */
247 private static function formatValueMode1($cellValue): string
248 {
249 if (is_string($cellValue) && ErrorValue::isError($cellValue) === false) {
250 return Calculation::FORMULA_STRING_QUOTE . $cellValue . Calculation::FORMULA_STRING_QUOTE;
251 } elseif (is_bool($cellValue)) {
252 return Calculation::getLocaleBoolean($cellValue ? 'TRUE' : 'FALSE');
253 }
254
255 return StringHelper::convertToString($cellValue);
256 }
257 }
258