ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Alignment.php
Go to the documentation of this file.
1 <?php
2 
4 
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 
19  // Vertical alignment styles
20  const VERTICAL_BOTTOM = 'bottom';
21  const VERTICAL_TOP = 'top';
22  const VERTICAL_CENTER = 'center';
23  const VERTICAL_JUSTIFY = 'justify';
24  const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only
25 
26  // Read order
27  const READORDER_CONTEXT = 0;
28  const READORDER_LTR = 1;
29  const READORDER_RTL = 2;
30 
31  // Special value for Text Rotation
33  const TEXTROTATION_STACK_PHPSPREADSHEET = -165; // 90 - 255
34 
40  protected $horizontal = self::HORIZONTAL_GENERAL;
41 
47  protected $vertical = self::VERTICAL_BOTTOM;
48 
54  protected $textRotation = 0;
55 
61  protected $wrapText = false;
62 
68  protected $shrinkToFit = false;
69 
75  protected $indent = 0;
76 
82  protected $readOrder = 0;
83 
94  public function __construct($isSupervisor = false, $isConditional = false)
95  {
96  // Supervisor?
97  parent::__construct($isSupervisor);
98 
99  if ($isConditional) {
100  $this->horizontal = null;
101  $this->vertical = null;
102  $this->textRotation = null;
103  }
104  }
105 
112  public function getSharedComponent()
113  {
114  return $this->parent->getSharedComponent()->getAlignment();
115  }
116 
124  public function getStyleArray($array)
125  {
126  return ['alignment' => $array];
127  }
128 
147  public function applyFromArray(array $pStyles)
148  {
149  if ($this->isSupervisor) {
150  $this->getActiveSheet()->getStyle($this->getSelectedCells())
151  ->applyFromArray($this->getStyleArray($pStyles));
152  } else {
153  if (isset($pStyles['horizontal'])) {
154  $this->setHorizontal($pStyles['horizontal']);
155  }
156  if (isset($pStyles['vertical'])) {
157  $this->setVertical($pStyles['vertical']);
158  }
159  if (isset($pStyles['textRotation'])) {
160  $this->setTextRotation($pStyles['textRotation']);
161  }
162  if (isset($pStyles['wrapText'])) {
163  $this->setWrapText($pStyles['wrapText']);
164  }
165  if (isset($pStyles['shrinkToFit'])) {
166  $this->setShrinkToFit($pStyles['shrinkToFit']);
167  }
168  if (isset($pStyles['indent'])) {
169  $this->setIndent($pStyles['indent']);
170  }
171  if (isset($pStyles['readOrder'])) {
172  $this->setReadOrder($pStyles['readOrder']);
173  }
174  }
175 
176  return $this;
177  }
178 
184  public function getHorizontal()
185  {
186  if ($this->isSupervisor) {
187  return $this->getSharedComponent()->getHorizontal();
188  }
189 
190  return $this->horizontal;
191  }
192 
200  public function setHorizontal($pValue)
201  {
202  if ($pValue == '') {
203  $pValue = self::HORIZONTAL_GENERAL;
204  }
205 
206  if ($this->isSupervisor) {
207  $styleArray = $this->getStyleArray(['horizontal' => $pValue]);
208  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
209  } else {
210  $this->horizontal = $pValue;
211  }
212 
213  return $this;
214  }
215 
221  public function getVertical()
222  {
223  if ($this->isSupervisor) {
224  return $this->getSharedComponent()->getVertical();
225  }
226 
227  return $this->vertical;
228  }
229 
237  public function setVertical($pValue)
238  {
239  if ($pValue == '') {
240  $pValue = self::VERTICAL_BOTTOM;
241  }
242 
243  if ($this->isSupervisor) {
244  $styleArray = $this->getStyleArray(['vertical' => $pValue]);
245  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
246  } else {
247  $this->vertical = $pValue;
248  }
249 
250  return $this;
251  }
252 
258  public function getTextRotation()
259  {
260  if ($this->isSupervisor) {
261  return $this->getSharedComponent()->getTextRotation();
262  }
263 
264  return $this->textRotation;
265  }
266 
274  public function setTextRotation($pValue)
275  {
276  // Excel2007 value 255 => PhpSpreadsheet value -165
277  if ($pValue == self::TEXTROTATION_STACK_EXCEL) {
278  $pValue = self::TEXTROTATION_STACK_PHPSPREADSHEET;
279  }
280 
281  // Set rotation
282  if (($pValue >= -90 && $pValue <= 90) || $pValue == self::TEXTROTATION_STACK_PHPSPREADSHEET) {
283  if ($this->isSupervisor) {
284  $styleArray = $this->getStyleArray(['textRotation' => $pValue]);
285  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
286  } else {
287  $this->textRotation = $pValue;
288  }
289  } else {
290  throw new PhpSpreadsheetException('Text rotation should be a value between -90 and 90.');
291  }
292 
293  return $this;
294  }
295 
301  public function getWrapText()
302  {
303  if ($this->isSupervisor) {
304  return $this->getSharedComponent()->getWrapText();
305  }
306 
307  return $this->wrapText;
308  }
309 
317  public function setWrapText($pValue)
318  {
319  if ($pValue == '') {
320  $pValue = false;
321  }
322  if ($this->isSupervisor) {
323  $styleArray = $this->getStyleArray(['wrapText' => $pValue]);
324  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
325  } else {
326  $this->wrapText = $pValue;
327  }
328 
329  return $this;
330  }
331 
337  public function getShrinkToFit()
338  {
339  if ($this->isSupervisor) {
340  return $this->getSharedComponent()->getShrinkToFit();
341  }
342 
343  return $this->shrinkToFit;
344  }
345 
353  public function setShrinkToFit($pValue)
354  {
355  if ($pValue == '') {
356  $pValue = false;
357  }
358  if ($this->isSupervisor) {
359  $styleArray = $this->getStyleArray(['shrinkToFit' => $pValue]);
360  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
361  } else {
362  $this->shrinkToFit = $pValue;
363  }
364 
365  return $this;
366  }
367 
373  public function getIndent()
374  {
375  if ($this->isSupervisor) {
376  return $this->getSharedComponent()->getIndent();
377  }
378 
379  return $this->indent;
380  }
381 
389  public function setIndent($pValue)
390  {
391  if ($pValue > 0) {
392  if (
393  $this->getHorizontal() != self::HORIZONTAL_GENERAL &&
394  $this->getHorizontal() != self::HORIZONTAL_LEFT &&
395  $this->getHorizontal() != self::HORIZONTAL_RIGHT &&
396  $this->getHorizontal() != self::HORIZONTAL_DISTRIBUTED
397  ) {
398  $pValue = 0; // indent not supported
399  }
400  }
401  if ($this->isSupervisor) {
402  $styleArray = $this->getStyleArray(['indent' => $pValue]);
403  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
404  } else {
405  $this->indent = $pValue;
406  }
407 
408  return $this;
409  }
410 
416  public function getReadOrder()
417  {
418  if ($this->isSupervisor) {
419  return $this->getSharedComponent()->getReadOrder();
420  }
421 
422  return $this->readOrder;
423  }
424 
432  public function setReadOrder($pValue)
433  {
434  if ($pValue < 0 || $pValue > 2) {
435  $pValue = 0;
436  }
437  if ($this->isSupervisor) {
438  $styleArray = $this->getStyleArray(['readOrder' => $pValue]);
439  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
440  } else {
441  $this->readOrder = $pValue;
442  }
443 
444  return $this;
445  }
446 
452  public function getHashCode()
453  {
454  if ($this->isSupervisor) {
455  return $this->getSharedComponent()->getHashCode();
456  }
457 
458  return md5(
459  $this->horizontal .
460  $this->vertical .
461  $this->textRotation .
462  ($this->wrapText ? 't' : 'f') .
463  ($this->shrinkToFit ? 't' : 'f') .
464  $this->indent .
465  $this->readOrder .
466  __CLASS__
467  );
468  }
469 
470  protected function exportArray1(): array
471  {
472  $exportedArray = [];
473  $this->exportArray2($exportedArray, 'horizontal', $this->getHorizontal());
474  $this->exportArray2($exportedArray, 'indent', $this->getIndent());
475  $this->exportArray2($exportedArray, 'readOrder', $this->getReadOrder());
476  $this->exportArray2($exportedArray, 'shrinkToFit', $this->getShrinkToFit());
477  $this->exportArray2($exportedArray, 'textRotation', $this->getTextRotation());
478  $this->exportArray2($exportedArray, 'vertical', $this->getVertical());
479  $this->exportArray2($exportedArray, 'wrapText', $this->getWrapText());
480 
481  return $exportedArray;
482  }
483 }
getSharedComponent()
Get the shared style component for the currently active cell in currently active sheet.
Definition: Alignment.php:112
applyFromArray(array $pStyles)
Apply styles from array.
Definition: Alignment.php:147
setTextRotation($pValue)
Set TextRotation.
Definition: Alignment.php:274
getStyleArray($array)
Build style array from subcomponents.
Definition: Alignment.php:124
getSelectedCells()
Get the currently active cell coordinate in currently active sheet.
Definition: Supervisor.php:87
setWrapText($pValue)
Set Wrap Text.
Definition: Alignment.php:317
setShrinkToFit($pValue)
Set Shrink to fit.
Definition: Alignment.php:353
setReadOrder($pValue)
Set read order.
Definition: Alignment.php:432
getActiveSheet()
Get the currently active sheet.
Definition: Supervisor.php:76
__construct($isSupervisor=false, $isConditional=false)
Create a new Alignment.
Definition: Alignment.php:94
setVertical($pValue)
Set Vertical.
Definition: Alignment.php:237
exportArray2(array &$exportedArray, string $index, $objOrValue)
Populate array from exportArray1.
Definition: Supervisor.php:150
setHorizontal($pValue)
Set Horizontal.
Definition: Alignment.php:200