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 / Alignment.php

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

549 lines 14.3 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;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
7 class Alignment extends Supervisor
8 {
9 // Horizontal alignment styles
10 const HORIZONTAL_GENERAL = 'general';
11 const HORIZONTAL_LEFT = 'left';
12 const HORIZONTAL_RIGHT = 'right';
13 const HORIZONTAL_CENTER = 'center';
14 const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
15 const HORIZONTAL_JUSTIFY = 'justify';
16 const HORIZONTAL_FILL = 'fill';
17 const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only
18 private const HORIZONTAL_CENTER_CONTINUOUS_LC = 'centercontinuous';
19 // Mapping for horizontal alignment
20 const HORIZONTAL_ALIGNMENT_FOR_XLSX = [
21 self::HORIZONTAL_LEFT => self::HORIZONTAL_LEFT,
22 self::HORIZONTAL_RIGHT => self::HORIZONTAL_RIGHT,
23 self::HORIZONTAL_CENTER => self::HORIZONTAL_CENTER,
24 self::HORIZONTAL_CENTER_CONTINUOUS => self::HORIZONTAL_CENTER_CONTINUOUS,
25 self::HORIZONTAL_JUSTIFY => self::HORIZONTAL_JUSTIFY,
26 self::HORIZONTAL_FILL => self::HORIZONTAL_FILL,
27 self::HORIZONTAL_DISTRIBUTED => self::HORIZONTAL_DISTRIBUTED,
28 ];
29 // Mapping for horizontal alignment CSS
30 const HORIZONTAL_ALIGNMENT_FOR_HTML = [
31 self::HORIZONTAL_LEFT => self::HORIZONTAL_LEFT,
32 self::HORIZONTAL_RIGHT => self::HORIZONTAL_RIGHT,
33 self::HORIZONTAL_CENTER => self::HORIZONTAL_CENTER,
34 self::HORIZONTAL_CENTER_CONTINUOUS => self::HORIZONTAL_CENTER,
35 self::HORIZONTAL_JUSTIFY => self::HORIZONTAL_JUSTIFY,
36 //self::HORIZONTAL_FILL => self::HORIZONTAL_FILL, // no reasonable equivalent for fill
37 self::HORIZONTAL_DISTRIBUTED => self::HORIZONTAL_JUSTIFY,
38 ];
39
40 // Vertical alignment styles
41 const VERTICAL_BOTTOM = 'bottom';
42 const VERTICAL_TOP = 'top';
43 const VERTICAL_CENTER = 'center';
44 const VERTICAL_JUSTIFY = 'justify';
45 const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only
46 // Vertical alignment CSS
47 private const VERTICAL_BASELINE = 'baseline';
48 private const VERTICAL_MIDDLE = 'middle';
49 private const VERTICAL_SUB = 'sub';
50 private const VERTICAL_SUPER = 'super';
51 private const VERTICAL_TEXT_BOTTOM = 'text-bottom';
52 private const VERTICAL_TEXT_TOP = 'text-top';
53
54 // Mapping for vertical alignment
55 const VERTICAL_ALIGNMENT_FOR_XLSX = [
56 self::VERTICAL_BOTTOM => self::VERTICAL_BOTTOM,
57 self::VERTICAL_TOP => self::VERTICAL_TOP,
58 self::VERTICAL_CENTER => self::VERTICAL_CENTER,
59 self::VERTICAL_JUSTIFY => self::VERTICAL_JUSTIFY,
60 self::VERTICAL_DISTRIBUTED => self::VERTICAL_DISTRIBUTED,
61 // css settings that aren't in sync with Excel
62 self::VERTICAL_BASELINE => self::VERTICAL_BOTTOM,
63 self::VERTICAL_MIDDLE => self::VERTICAL_CENTER,
64 self::VERTICAL_SUB => self::VERTICAL_BOTTOM,
65 self::VERTICAL_SUPER => self::VERTICAL_TOP,
66 self::VERTICAL_TEXT_BOTTOM => self::VERTICAL_BOTTOM,
67 self::VERTICAL_TEXT_TOP => self::VERTICAL_TOP,
68 ];
69
70 // Mapping for vertical alignment for Html
71 const VERTICAL_ALIGNMENT_FOR_HTML = [
72 self::VERTICAL_BOTTOM => self::VERTICAL_BOTTOM,
73 self::VERTICAL_TOP => self::VERTICAL_TOP,
74 self::VERTICAL_CENTER => self::VERTICAL_MIDDLE,
75 self::VERTICAL_JUSTIFY => self::VERTICAL_MIDDLE,
76 self::VERTICAL_DISTRIBUTED => self::VERTICAL_MIDDLE,
77 // css settings that aren't in sync with Excel
78 self::VERTICAL_BASELINE => self::VERTICAL_BASELINE,
79 self::VERTICAL_MIDDLE => self::VERTICAL_MIDDLE,
80 self::VERTICAL_SUB => self::VERTICAL_SUB,
81 self::VERTICAL_SUPER => self::VERTICAL_SUPER,
82 self::VERTICAL_TEXT_BOTTOM => self::VERTICAL_TEXT_BOTTOM,
83 self::VERTICAL_TEXT_TOP => self::VERTICAL_TEXT_TOP,
84 ];
85
86 // Read order
87 const READORDER_CONTEXT = 0;
88 const READORDER_LTR = 1;
89 const READORDER_RTL = 2;
90
91 // Special value for Text Rotation
92 const TEXTROTATION_STACK_EXCEL = 255;
93 const TEXTROTATION_STACK_PHPSPREADSHEET = -165; // 90 - 255
94
95 public const INDENT_UNITS_TO_PIXELS = 9;
96
97 /**
98 * Horizontal alignment.
99 */
100 protected ?string $horizontal = self::HORIZONTAL_GENERAL;
101
102 /**
103 * Justify Last Line alignment.
104 */
105 protected ?bool $justifyLastLine = null;
106
107 /**
108 * Vertical alignment.
109 */
110 protected ?string $vertical = self::VERTICAL_BOTTOM;
111
112 /**
113 * Text rotation.
114 */
115 protected ?int $textRotation = 0;
116
117 /**
118 * Wrap text.
119 */
120 protected bool $wrapText = false;
121
122 /**
123 * Shrink to fit.
124 */
125 protected bool $shrinkToFit = false;
126
127 /**
128 * Indent - only possible with horizontal alignment left and right.
129 */
130 protected int $indent = 0;
131
132 /**
133 * Read order.
134 */
135 protected int $readOrder = 0;
136
137 /**
138 * Create a new Alignment.
139 *
140 * @param bool $isSupervisor Flag indicating if this is a supervisor or not
141 * Leave this value at default unless you understand exactly what
142 * its ramifications are
143 * @param bool $isConditional Flag indicating if this is a conditional style or not
144 * Leave this value at default unless you understand exactly what
145 * its ramifications are
146 */
147 public function __construct(bool $isSupervisor = false, bool $isConditional = false)
148 {
149 // Supervisor?
150 parent::__construct($isSupervisor);
151
152 if ($isConditional) {
153 $this->horizontal = null;
154 $this->vertical = null;
155 $this->textRotation = null;
156 }
157 }
158
159 /**
160 * Get the shared style component for the currently active cell in currently active sheet.
161 * Only used for style supervisor.
162 */
163 public function getSharedComponent(): self
164 {
165 /** @var Style $parent */
166 $parent = $this->parent;
167
168 return $parent->getSharedComponent()->getAlignment();
169 }
170
171 /**
172 * Build style array from subcomponents.
173 *
174 * @param mixed[] $array
175 *
176 * @return array{alignment: mixed[]}
177 */
178 public function getStyleArray(array $array): array
179 {
180 return ['alignment' => $array];
181 }
182
183 /**
184 * Apply styles from array.
185 *
186 * <code>
187 * $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
188 * [
189 * 'horizontal' => \TablePress\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
190 * 'vertical' => \TablePress\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
191 * 'textRotation' => 0,
192 * 'wrapText' => TRUE
193 * ]
194 * );
195 * </code>
196 *
197 * @param mixed[] $styleArray Array containing style information
198 *
199 * @return $this
200 */
201 public function applyFromArray(array $styleArray)
202 {
203 if ($this->isSupervisor) {
204 $this->getActiveSheet()->getStyle($this->getSelectedCells())
205 ->applyFromArray($this->getStyleArray($styleArray));
206 } else {
207 /** @var array{horizontal?: string, vertical?: string, justifyLastLine?: bool, textRotation?: int, wrapText?: bool, shrinkToFit?: bool, readOrder?: int, indent?: int} $styleArray */
208 if (isset($styleArray['horizontal'])) {
209 $this->setHorizontal($styleArray['horizontal']);
210 }
211 if (isset($styleArray['justifyLastLine'])) {
212 $this->setJustifyLastLine($styleArray['justifyLastLine']);
213 }
214 if (isset($styleArray['vertical'])) {
215 $this->setVertical($styleArray['vertical']);
216 }
217 if (isset($styleArray['textRotation'])) {
218 $this->setTextRotation($styleArray['textRotation']);
219 }
220 if (isset($styleArray['wrapText'])) {
221 $this->setWrapText($styleArray['wrapText']);
222 }
223 if (isset($styleArray['shrinkToFit'])) {
224 $this->setShrinkToFit($styleArray['shrinkToFit']);
225 }
226 if (isset($styleArray['indent'])) {
227 $this->setIndent($styleArray['indent']);
228 }
229 if (isset($styleArray['readOrder'])) {
230 $this->setReadOrder($styleArray['readOrder']);
231 }
232 }
233
234 return $this;
235 }
236
237 /**
238 * Get Horizontal.
239 */
240 public function getHorizontal(): ?string
241 {
242 if ($this->isSupervisor) {
243 return $this->getSharedComponent()->getHorizontal();
244 }
245
246 return $this->horizontal;
247 }
248
249 /**
250 * Set Horizontal.
251 *
252 * @param string $horizontalAlignment see self::HORIZONTAL_*
253 *
254 * @return $this
255 */
256 public function setHorizontal(string $horizontalAlignment)
257 {
258 $horizontalAlignment = strtolower($horizontalAlignment);
259 if ($horizontalAlignment === self::HORIZONTAL_CENTER_CONTINUOUS_LC) {
260 $horizontalAlignment = self::HORIZONTAL_CENTER_CONTINUOUS;
261 }
262
263 if ($this->isSupervisor) {
264 $styleArray = $this->getStyleArray(['horizontal' => $horizontalAlignment]);
265 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
266 } else {
267 $this->horizontal = $horizontalAlignment;
268 }
269
270 return $this;
271 }
272
273 /**
274 * Get Justify Last Line.
275 */
276 public function getJustifyLastLine(): ?bool
277 {
278 if ($this->isSupervisor) {
279 return $this->getSharedComponent()->getJustifyLastLine();
280 }
281
282 return $this->justifyLastLine;
283 }
284
285 /**
286 * Set Justify Last Line.
287 *
288 * @return $this
289 */
290 public function setJustifyLastLine(bool $justifyLastLine)
291 {
292 if ($this->isSupervisor) {
293 $styleArray = $this->getStyleArray(['justifyLastLine' => $justifyLastLine]);
294 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
295 } else {
296 $this->justifyLastLine = $justifyLastLine;
297 }
298
299 return $this;
300 }
301
302 /**
303 * Get Vertical.
304 */
305 public function getVertical(): ?string
306 {
307 if ($this->isSupervisor) {
308 return $this->getSharedComponent()->getVertical();
309 }
310
311 return $this->vertical;
312 }
313
314 /**
315 * Set Vertical.
316 *
317 * @param string $verticalAlignment see self::VERTICAL_*
318 *
319 * @return $this
320 */
321 public function setVertical(string $verticalAlignment)
322 {
323 $verticalAlignment = strtolower($verticalAlignment);
324
325 if ($this->isSupervisor) {
326 $styleArray = $this->getStyleArray(['vertical' => $verticalAlignment]);
327 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
328 } else {
329 $this->vertical = $verticalAlignment;
330 }
331
332 return $this;
333 }
334
335 /**
336 * Get TextRotation.
337 */
338 public function getTextRotation(): ?int
339 {
340 if ($this->isSupervisor) {
341 return $this->getSharedComponent()->getTextRotation();
342 }
343
344 return $this->textRotation;
345 }
346
347 /**
348 * Set TextRotation.
349 *
350 * @return $this
351 */
352 public function setTextRotation(int $angleInDegrees)
353 {
354 // Excel2007 value 255 => PhpSpreadsheet value -165
355 if ($angleInDegrees == self::TEXTROTATION_STACK_EXCEL) {
356 $angleInDegrees = self::TEXTROTATION_STACK_PHPSPREADSHEET;
357 }
358
359 // Set rotation
360 if (($angleInDegrees >= -90 && $angleInDegrees <= 90) || $angleInDegrees == self::TEXTROTATION_STACK_PHPSPREADSHEET) {
361 if ($this->isSupervisor) {
362 $styleArray = $this->getStyleArray(['textRotation' => $angleInDegrees]);
363 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
364 } else {
365 $this->textRotation = $angleInDegrees;
366 }
367 } else {
368 throw new PhpSpreadsheetException("Text rotation $angleInDegrees should be a value between -90 and 90.");
369 }
370
371 return $this;
372 }
373
374 /**
375 * Get Wrap Text.
376 */
377 public function getWrapText(): bool
378 {
379 if ($this->isSupervisor) {
380 return $this->getSharedComponent()->getWrapText();
381 }
382
383 return $this->wrapText;
384 }
385
386 /**
387 * Set Wrap Text.
388 *
389 * @return $this
390 */
391 public function setWrapText(bool $wrapped)
392 {
393 if ($wrapped == '') {
394 $wrapped = false;
395 }
396 if ($this->isSupervisor) {
397 $styleArray = $this->getStyleArray(['wrapText' => $wrapped]);
398 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
399 } else {
400 $this->wrapText = $wrapped;
401 }
402
403 return $this;
404 }
405
406 /**
407 * Get Shrink to fit.
408 */
409 public function getShrinkToFit(): bool
410 {
411 if ($this->isSupervisor) {
412 return $this->getSharedComponent()->getShrinkToFit();
413 }
414
415 return $this->shrinkToFit;
416 }
417
418 /**
419 * Set Shrink to fit.
420 *
421 * @return $this
422 */
423 public function setShrinkToFit(bool $shrink)
424 {
425 if ($shrink == '') {
426 $shrink = false;
427 }
428 if ($this->isSupervisor) {
429 $styleArray = $this->getStyleArray(['shrinkToFit' => $shrink]);
430 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
431 } else {
432 $this->shrinkToFit = $shrink;
433 }
434
435 return $this;
436 }
437
438 /**
439 * Get indent.
440 */
441 public function getIndent(): int
442 {
443 if ($this->isSupervisor) {
444 return $this->getSharedComponent()->getIndent();
445 }
446
447 return $this->indent;
448 }
449
450 /**
451 * Set indent.
452 *
453 * @return $this
454 */
455 public function setIndent(int $indent)
456 {
457 if ($indent > 0) {
458 if (
459 $this->getHorizontal() != self::HORIZONTAL_GENERAL
460 && $this->getHorizontal() != self::HORIZONTAL_LEFT
461 && $this->getHorizontal() != self::HORIZONTAL_RIGHT
462 && $this->getHorizontal() != self::HORIZONTAL_DISTRIBUTED
463 ) {
464 $indent = 0; // indent not supported
465 }
466 }
467 if ($this->isSupervisor) {
468 $styleArray = $this->getStyleArray(['indent' => $indent]);
469 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
470 } else {
471 $this->indent = $indent;
472 }
473
474 return $this;
475 }
476
477 /**
478 * Get read order.
479 */
480 public function getReadOrder(): int
481 {
482 if ($this->isSupervisor) {
483 return $this->getSharedComponent()->getReadOrder();
484 }
485
486 return $this->readOrder;
487 }
488
489 /**
490 * Set read order.
491 *
492 * @return $this
493 */
494 public function setReadOrder(int $readOrder)
495 {
496 if ($readOrder < 0 || $readOrder > 2) {
497 $readOrder = 0;
498 }
499 if ($this->isSupervisor) {
500 $styleArray = $this->getStyleArray(['readOrder' => $readOrder]);
501 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
502 } else {
503 $this->readOrder = $readOrder;
504 }
505
506 return $this;
507 }
508
509 /**
510 * Get hash code.
511 *
512 * @return string Hash code
513 */
514 public function getHashCode(): string
515 {
516 if ($this->isSupervisor) {
517 return $this->getSharedComponent()->getHashCode();
518 }
519
520 return md5(
521 $this->horizontal
522 . (($this->justifyLastLine === null) ? 'null' : ($this->justifyLastLine ? 't' : 'f'))
523 . $this->vertical
524 . $this->textRotation
525 . ($this->wrapText ? 't' : 'f')
526 . ($this->shrinkToFit ? 't' : 'f')
527 . $this->indent
528 . $this->readOrder
529 . __CLASS__
530 );
531 }
532
533 /** @return mixed[] */
534 protected function exportArray1(): array
535 {
536 $exportedArray = [];
537 $this->exportArray2($exportedArray, 'horizontal', $this->getHorizontal());
538 $this->exportArray2($exportedArray, 'justifyLastLine', $this->getJustifyLastLine());
539 $this->exportArray2($exportedArray, 'indent', $this->getIndent());
540 $this->exportArray2($exportedArray, 'readOrder', $this->getReadOrder());
541 $this->exportArray2($exportedArray, 'shrinkToFit', $this->getShrinkToFit());
542 $this->exportArray2($exportedArray, 'textRotation', $this->getTextRotation());
543 $this->exportArray2($exportedArray, 'vertical', $this->getVertical());
544 $this->exportArray2($exportedArray, 'wrapText', $this->getWrapText());
545
546 return $exportedArray;
547 }
548 }
549