Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Style;
   4  
   5  use 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  
  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      /**
  32       * Horizontal alignment.
  33       *
  34       * @var string
  35       */
  36      protected $horizontal = self::HORIZONTAL_GENERAL;
  37  
  38      /**
  39       * Vertical alignment.
  40       *
  41       * @var string
  42       */
  43      protected $vertical = self::VERTICAL_BOTTOM;
  44  
  45      /**
  46       * Text rotation.
  47       *
  48       * @var int
  49       */
  50      protected $textRotation = 0;
  51  
  52      /**
  53       * Wrap text.
  54       *
  55       * @var bool
  56       */
  57      protected $wrapText = false;
  58  
  59      /**
  60       * Shrink to fit.
  61       *
  62       * @var bool
  63       */
  64      protected $shrinkToFit = false;
  65  
  66      /**
  67       * Indent - only possible with horizontal alignment left and right.
  68       *
  69       * @var int
  70       */
  71      protected $indent = 0;
  72  
  73      /**
  74       * Read order.
  75       *
  76       * @var int
  77       */
  78      protected $readOrder = 0;
  79  
  80      /**
  81       * Create a new Alignment.
  82       *
  83       * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  84       *                                       Leave this value at default unless you understand exactly what
  85       *                                          its ramifications are
  86       * @param bool $isConditional Flag indicating if this is a conditional style or not
  87       *                                       Leave this value at default unless you understand exactly what
  88       *                                          its ramifications are
  89       */
  90      public function __construct($isSupervisor = false, $isConditional = false)
  91      {
  92          // Supervisor?
  93          parent::__construct($isSupervisor);
  94  
  95          if ($isConditional) {
  96              $this->horizontal = null;
  97              $this->vertical = null;
  98              $this->textRotation = null;
  99          }
 100      }
 101  
 102      /**
 103       * Get the shared style component for the currently active cell in currently active sheet.
 104       * Only used for style supervisor.
 105       *
 106       * @return Alignment
 107       */
 108      public function getSharedComponent()
 109      {
 110          return $this->parent->getSharedComponent()->getAlignment();
 111      }
 112  
 113      /**
 114       * Build style array from subcomponents.
 115       *
 116       * @param array $array
 117       *
 118       * @return array
 119       */
 120      public function getStyleArray($array)
 121      {
 122          return ['alignment' => $array];
 123      }
 124  
 125      /**
 126       * Apply styles from array.
 127       *
 128       * <code>
 129       * $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
 130       *        [
 131       *            'horizontal'   => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
 132       *            'vertical'     => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
 133       *            'textRotation' => 0,
 134       *            'wrapText'     => TRUE
 135       *        ]
 136       * );
 137       * </code>
 138       *
 139       * @param array $pStyles Array containing style information
 140       *
 141       * @throws PhpSpreadsheetException
 142       *
 143       * @return Alignment
 144       */
 145      public function applyFromArray(array $pStyles)
 146      {
 147          if ($this->isSupervisor) {
 148              $this->getActiveSheet()->getStyle($this->getSelectedCells())
 149                  ->applyFromArray($this->getStyleArray($pStyles));
 150          } else {
 151              if (isset($pStyles['horizontal'])) {
 152                  $this->setHorizontal($pStyles['horizontal']);
 153              }
 154              if (isset($pStyles['vertical'])) {
 155                  $this->setVertical($pStyles['vertical']);
 156              }
 157              if (isset($pStyles['textRotation'])) {
 158                  $this->setTextRotation($pStyles['textRotation']);
 159              }
 160              if (isset($pStyles['wrapText'])) {
 161                  $this->setWrapText($pStyles['wrapText']);
 162              }
 163              if (isset($pStyles['shrinkToFit'])) {
 164                  $this->setShrinkToFit($pStyles['shrinkToFit']);
 165              }
 166              if (isset($pStyles['indent'])) {
 167                  $this->setIndent($pStyles['indent']);
 168              }
 169              if (isset($pStyles['readOrder'])) {
 170                  $this->setReadOrder($pStyles['readOrder']);
 171              }
 172          }
 173  
 174          return $this;
 175      }
 176  
 177      /**
 178       * Get Horizontal.
 179       *
 180       * @return string
 181       */
 182      public function getHorizontal()
 183      {
 184          if ($this->isSupervisor) {
 185              return $this->getSharedComponent()->getHorizontal();
 186          }
 187  
 188          return $this->horizontal;
 189      }
 190  
 191      /**
 192       * Set Horizontal.
 193       *
 194       * @param string $pValue see self::HORIZONTAL_*
 195       *
 196       * @return Alignment
 197       */
 198      public function setHorizontal($pValue)
 199      {
 200          if ($pValue == '') {
 201              $pValue = self::HORIZONTAL_GENERAL;
 202          }
 203  
 204          if ($this->isSupervisor) {
 205              $styleArray = $this->getStyleArray(['horizontal' => $pValue]);
 206              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 207          } else {
 208              $this->horizontal = $pValue;
 209          }
 210  
 211          return $this;
 212      }
 213  
 214      /**
 215       * Get Vertical.
 216       *
 217       * @return string
 218       */
 219      public function getVertical()
 220      {
 221          if ($this->isSupervisor) {
 222              return $this->getSharedComponent()->getVertical();
 223          }
 224  
 225          return $this->vertical;
 226      }
 227  
 228      /**
 229       * Set Vertical.
 230       *
 231       * @param string $pValue see self::VERTICAL_*
 232       *
 233       * @return Alignment
 234       */
 235      public function setVertical($pValue)
 236      {
 237          if ($pValue == '') {
 238              $pValue = self::VERTICAL_BOTTOM;
 239          }
 240  
 241          if ($this->isSupervisor) {
 242              $styleArray = $this->getStyleArray(['vertical' => $pValue]);
 243              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 244          } else {
 245              $this->vertical = $pValue;
 246          }
 247  
 248          return $this;
 249      }
 250  
 251      /**
 252       * Get TextRotation.
 253       *
 254       * @return int
 255       */
 256      public function getTextRotation()
 257      {
 258          if ($this->isSupervisor) {
 259              return $this->getSharedComponent()->getTextRotation();
 260          }
 261  
 262          return $this->textRotation;
 263      }
 264  
 265      /**
 266       * Set TextRotation.
 267       *
 268       * @param int $pValue
 269       *
 270       * @throws PhpSpreadsheetException
 271       *
 272       * @return Alignment
 273       */
 274      public function setTextRotation($pValue)
 275      {
 276          // Excel2007 value 255 => PhpSpreadsheet value -165
 277          if ($pValue == 255) {
 278              $pValue = -165;
 279          }
 280  
 281          // Set rotation
 282          if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
 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  
 296      /**
 297       * Get Wrap Text.
 298       *
 299       * @return bool
 300       */
 301      public function getWrapText()
 302      {
 303          if ($this->isSupervisor) {
 304              return $this->getSharedComponent()->getWrapText();
 305          }
 306  
 307          return $this->wrapText;
 308      }
 309  
 310      /**
 311       * Set Wrap Text.
 312       *
 313       * @param bool $pValue
 314       *
 315       * @return Alignment
 316       */
 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  
 332      /**
 333       * Get Shrink to fit.
 334       *
 335       * @return bool
 336       */
 337      public function getShrinkToFit()
 338      {
 339          if ($this->isSupervisor) {
 340              return $this->getSharedComponent()->getShrinkToFit();
 341          }
 342  
 343          return $this->shrinkToFit;
 344      }
 345  
 346      /**
 347       * Set Shrink to fit.
 348       *
 349       * @param bool $pValue
 350       *
 351       * @return Alignment
 352       */
 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  
 368      /**
 369       * Get indent.
 370       *
 371       * @return int
 372       */
 373      public function getIndent()
 374      {
 375          if ($this->isSupervisor) {
 376              return $this->getSharedComponent()->getIndent();
 377          }
 378  
 379          return $this->indent;
 380      }
 381  
 382      /**
 383       * Set indent.
 384       *
 385       * @param int $pValue
 386       *
 387       * @return Alignment
 388       */
 389      public function setIndent($pValue)
 390      {
 391          if ($pValue > 0) {
 392              if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
 393                  $this->getHorizontal() != self::HORIZONTAL_LEFT &&
 394                  $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
 395                  $pValue = 0; // indent not supported
 396              }
 397          }
 398          if ($this->isSupervisor) {
 399              $styleArray = $this->getStyleArray(['indent' => $pValue]);
 400              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 401          } else {
 402              $this->indent = $pValue;
 403          }
 404  
 405          return $this;
 406      }
 407  
 408      /**
 409       * Get read order.
 410       *
 411       * @return int
 412       */
 413      public function getReadOrder()
 414      {
 415          if ($this->isSupervisor) {
 416              return $this->getSharedComponent()->getReadOrder();
 417          }
 418  
 419          return $this->readOrder;
 420      }
 421  
 422      /**
 423       * Set read order.
 424       *
 425       * @param int $pValue
 426       *
 427       * @return Alignment
 428       */
 429      public function setReadOrder($pValue)
 430      {
 431          if ($pValue < 0 || $pValue > 2) {
 432              $pValue = 0;
 433          }
 434          if ($this->isSupervisor) {
 435              $styleArray = $this->getStyleArray(['readOrder' => $pValue]);
 436              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 437          } else {
 438              $this->readOrder = $pValue;
 439          }
 440  
 441          return $this;
 442      }
 443  
 444      /**
 445       * Get hash code.
 446       *
 447       * @return string Hash code
 448       */
 449      public function getHashCode()
 450      {
 451          if ($this->isSupervisor) {
 452              return $this->getSharedComponent()->getHashCode();
 453          }
 454  
 455          return md5(
 456              $this->horizontal .
 457              $this->vertical .
 458              $this->textRotation .
 459              ($this->wrapText ? 't' : 'f') .
 460              ($this->shrinkToFit ? 't' : 'f') .
 461              $this->indent .
 462              $this->readOrder .
 463              __CLASS__
 464          );
 465      }
 466  }