[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Style/ -> Alignment.php (source)

   1  <?php
   2  /**
   3   * PHPExcel_Style_Alignment
   4   *
   5   * Copyright (c) 2006 - 2015 PHPExcel
   6   *
   7   * This library is free software; you can redistribute it and/or
   8   * modify it under the terms of the GNU Lesser General Public
   9   * License as published by the Free Software Foundation; either
  10   * version 2.1 of the License, or (at your option) any later version.
  11   *
  12   * This library is distributed in the hope that it will be useful,
  13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15   * Lesser General Public License for more details.
  16   *
  17   * You should have received a copy of the GNU Lesser General Public
  18   * License along with this library; if not, write to the Free Software
  19   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20   *
  21   * @category   PHPExcel
  22   * @package    PHPExcel_Style
  23   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25   * @version    ##VERSION##, ##DATE##
  26   */
  27  class PHPExcel_Style_Alignment extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  28  {
  29      /* Horizontal alignment styles */
  30      const HORIZONTAL_GENERAL           = 'general';
  31      const HORIZONTAL_LEFT              = 'left';
  32      const HORIZONTAL_RIGHT             = 'right';
  33      const HORIZONTAL_CENTER            = 'center';
  34      const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
  35      const HORIZONTAL_JUSTIFY           = 'justify';
  36      const HORIZONTAL_FILL              = 'fill';
  37      const HORIZONTAL_DISTRIBUTED       = 'distributed';        // Excel2007 only
  38  
  39      /* Vertical alignment styles */
  40      const VERTICAL_BOTTOM      = 'bottom';
  41      const VERTICAL_TOP         = 'top';
  42      const VERTICAL_CENTER      = 'center';
  43      const VERTICAL_JUSTIFY     = 'justify';
  44      const VERTICAL_DISTRIBUTED = 'distributed';        // Excel2007 only
  45  
  46      /* Read order */
  47      const READORDER_CONTEXT = 0;
  48      const READORDER_LTR     = 1;
  49      const READORDER_RTL     = 2;
  50  
  51      /**
  52       * Horizontal alignment
  53       *
  54       * @var string
  55       */
  56      protected $horizontal = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  57  
  58      /**
  59       * Vertical alignment
  60       *
  61       * @var string
  62       */
  63      protected $vertical = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  64  
  65      /**
  66       * Text rotation
  67       *
  68       * @var integer
  69       */
  70      protected $textRotation = 0;
  71  
  72      /**
  73       * Wrap text
  74       *
  75       * @var boolean
  76       */
  77      protected $wrapText = false;
  78  
  79      /**
  80       * Shrink to fit
  81       *
  82       * @var boolean
  83       */
  84      protected $shrinkToFit = false;
  85  
  86      /**
  87       * Indent - only possible with horizontal alignment left and right
  88       *
  89       * @var integer
  90       */
  91      protected $indent = 0;
  92  
  93      /**
  94       * Read order
  95       *
  96       * @var integer
  97       */
  98      protected $readorder = 0;
  99  
 100      /**
 101       * Create a new PHPExcel_Style_Alignment
 102       *
 103       * @param    boolean    $isSupervisor    Flag indicating if this is a supervisor or not
 104       *                                       Leave this value at default unless you understand exactly what
 105       *                                          its ramifications are
 106       * @param    boolean    $isConditional   Flag indicating if this is a conditional style or not
 107       *                                       Leave this value at default unless you understand exactly what
 108       *                                          its ramifications are
 109       */
 110      public function __construct($isSupervisor = false, $isConditional = false)
 111      {
 112          // Supervisor?
 113          parent::__construct($isSupervisor);
 114  
 115          if ($isConditional) {
 116              $this->horizontal   = null;
 117              $this->vertical     = null;
 118              $this->textRotation = null;
 119          }
 120      }
 121  
 122      /**
 123       * Get the shared style component for the currently active cell in currently active sheet.
 124       * Only used for style supervisor
 125       *
 126       * @return PHPExcel_Style_Alignment
 127       */
 128      public function getSharedComponent()
 129      {
 130          return $this->parent->getSharedComponent()->getAlignment();
 131      }
 132  
 133      /**
 134       * Build style array from subcomponents
 135       *
 136       * @param array $array
 137       * @return array
 138       */
 139      public function getStyleArray($array)
 140      {
 141          return array('alignment' => $array);
 142      }
 143  
 144      /**
 145       * Apply styles from array
 146       *
 147       * <code>
 148       * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
 149       *        array(
 150       *            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
 151       *            'vertical'   => PHPExcel_Style_Alignment::VERTICAL_CENTER,
 152       *            'rotation'   => 0,
 153       *            'wrap'            => TRUE
 154       *        )
 155       * );
 156       * </code>
 157       *
 158       * @param    array    $pStyles    Array containing style information
 159       * @throws    PHPExcel_Exception
 160       * @return PHPExcel_Style_Alignment
 161       */
 162      public function applyFromArray($pStyles = null)
 163      {
 164          if (is_array($pStyles)) {
 165              if ($this->isSupervisor) {
 166                  $this->getActiveSheet()->getStyle($this->getSelectedCells())
 167                      ->applyFromArray($this->getStyleArray($pStyles));
 168              } else {
 169                  if (isset($pStyles['horizontal'])) {
 170                      $this->setHorizontal($pStyles['horizontal']);
 171                  }
 172                  if (isset($pStyles['vertical'])) {
 173                      $this->setVertical($pStyles['vertical']);
 174                  }
 175                  if (isset($pStyles['rotation'])) {
 176                      $this->setTextRotation($pStyles['rotation']);
 177                  }
 178                  if (isset($pStyles['wrap'])) {
 179                      $this->setWrapText($pStyles['wrap']);
 180                  }
 181                  if (isset($pStyles['shrinkToFit'])) {
 182                      $this->setShrinkToFit($pStyles['shrinkToFit']);
 183                  }
 184                  if (isset($pStyles['indent'])) {
 185                      $this->setIndent($pStyles['indent']);
 186                  }
 187                  if (isset($pStyles['readorder'])) {
 188                      $this->setReadorder($pStyles['readorder']);
 189                  }
 190              }
 191          } else {
 192              throw new PHPExcel_Exception("Invalid style array passed.");
 193          }
 194          return $this;
 195      }
 196  
 197      /**
 198       * Get Horizontal
 199       *
 200       * @return string
 201       */
 202      public function getHorizontal()
 203      {
 204          if ($this->isSupervisor) {
 205              return $this->getSharedComponent()->getHorizontal();
 206          }
 207          return $this->horizontal;
 208      }
 209  
 210      /**
 211       * Set Horizontal
 212       *
 213       * @param string $pValue
 214       * @return PHPExcel_Style_Alignment
 215       */
 216      public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL)
 217      {
 218          if ($pValue == '') {
 219              $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
 220          }
 221  
 222          if ($this->isSupervisor) {
 223              $styleArray = $this->getStyleArray(array('horizontal' => $pValue));
 224              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 225          } else {
 226              $this->horizontal = $pValue;
 227          }
 228          return $this;
 229      }
 230  
 231      /**
 232       * Get Vertical
 233       *
 234       * @return string
 235       */
 236      public function getVertical()
 237      {
 238          if ($this->isSupervisor) {
 239              return $this->getSharedComponent()->getVertical();
 240          }
 241          return $this->vertical;
 242      }
 243  
 244      /**
 245       * Set Vertical
 246       *
 247       * @param string $pValue
 248       * @return PHPExcel_Style_Alignment
 249       */
 250      public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM)
 251      {
 252          if ($pValue == '') {
 253              $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
 254          }
 255  
 256          if ($this->isSupervisor) {
 257              $styleArray = $this->getStyleArray(array('vertical' => $pValue));
 258              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 259          } else {
 260              $this->vertical = $pValue;
 261          }
 262          return $this;
 263      }
 264  
 265      /**
 266       * Get TextRotation
 267       *
 268       * @return int
 269       */
 270      public function getTextRotation()
 271      {
 272          if ($this->isSupervisor) {
 273              return $this->getSharedComponent()->getTextRotation();
 274          }
 275          return $this->textRotation;
 276      }
 277  
 278      /**
 279       * Set TextRotation
 280       *
 281       * @param int $pValue
 282       * @throws PHPExcel_Exception
 283       * @return PHPExcel_Style_Alignment
 284       */
 285      public function setTextRotation($pValue = 0)
 286      {
 287          // Excel2007 value 255 => PHPExcel value -165
 288          if ($pValue == 255) {
 289              $pValue = -165;
 290          }
 291  
 292          // Set rotation
 293          if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
 294              if ($this->isSupervisor) {
 295                  $styleArray = $this->getStyleArray(array('rotation' => $pValue));
 296                  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 297              } else {
 298                  $this->textRotation = $pValue;
 299              }
 300          } else {
 301              throw new PHPExcel_Exception("Text rotation should be a value between -90 and 90.");
 302          }
 303  
 304          return $this;
 305      }
 306  
 307      /**
 308       * Get Wrap Text
 309       *
 310       * @return boolean
 311       */
 312      public function getWrapText()
 313      {
 314          if ($this->isSupervisor) {
 315              return $this->getSharedComponent()->getWrapText();
 316          }
 317          return $this->wrapText;
 318      }
 319  
 320      /**
 321       * Set Wrap Text
 322       *
 323       * @param boolean $pValue
 324       * @return PHPExcel_Style_Alignment
 325       */
 326      public function setWrapText($pValue = false)
 327      {
 328          if ($pValue == '') {
 329              $pValue = false;
 330          }
 331          if ($this->isSupervisor) {
 332              $styleArray = $this->getStyleArray(array('wrap' => $pValue));
 333              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 334          } else {
 335              $this->wrapText = $pValue;
 336          }
 337          return $this;
 338      }
 339  
 340      /**
 341       * Get Shrink to fit
 342       *
 343       * @return boolean
 344       */
 345      public function getShrinkToFit()
 346      {
 347          if ($this->isSupervisor) {
 348              return $this->getSharedComponent()->getShrinkToFit();
 349          }
 350          return $this->shrinkToFit;
 351      }
 352  
 353      /**
 354       * Set Shrink to fit
 355       *
 356       * @param boolean $pValue
 357       * @return PHPExcel_Style_Alignment
 358       */
 359      public function setShrinkToFit($pValue = false)
 360      {
 361          if ($pValue == '') {
 362              $pValue = false;
 363          }
 364          if ($this->isSupervisor) {
 365              $styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
 366              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 367          } else {
 368              $this->shrinkToFit = $pValue;
 369          }
 370          return $this;
 371      }
 372  
 373      /**
 374       * Get indent
 375       *
 376       * @return int
 377       */
 378      public function getIndent()
 379      {
 380          if ($this->isSupervisor) {
 381              return $this->getSharedComponent()->getIndent();
 382          }
 383          return $this->indent;
 384      }
 385  
 386      /**
 387       * Set indent
 388       *
 389       * @param int $pValue
 390       * @return PHPExcel_Style_Alignment
 391       */
 392      public function setIndent($pValue = 0)
 393      {
 394          if ($pValue > 0) {
 395              if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
 396                  $this->getHorizontal() != self::HORIZONTAL_LEFT &&
 397                  $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
 398                  $pValue = 0; // indent not supported
 399              }
 400          }
 401          if ($this->isSupervisor) {
 402              $styleArray = $this->getStyleArray(array('indent' => $pValue));
 403              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 404          } else {
 405              $this->indent = $pValue;
 406          }
 407          return $this;
 408      }
 409  
 410      /**
 411       * Get read order
 412       *
 413       * @return integer
 414       */
 415      public function getReadorder()
 416      {
 417          if ($this->isSupervisor) {
 418              return $this->getSharedComponent()->getReadorder();
 419          }
 420          return $this->readorder;
 421      }
 422  
 423      /**
 424       * Set read order
 425       *
 426       * @param int $pValue
 427       * @return PHPExcel_Style_Alignment
 428       */
 429      public function setReadorder($pValue = 0)
 430      {
 431          if ($pValue < 0 || $pValue > 2) {
 432              $pValue = 0;
 433          }
 434          if ($this->isSupervisor) {
 435              $styleArray = $this->getStyleArray(array('readorder' => $pValue));
 436              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 437          } else {
 438              $this->readorder = $pValue;
 439          }
 440          return $this;
 441      }
 442  
 443      /**
 444       * Get hash code
 445       *
 446       * @return string    Hash code
 447       */
 448      public function getHashCode()
 449      {
 450          if ($this->isSupervisor) {
 451              return $this->getSharedComponent()->getHashCode();
 452          }
 453          return md5(
 454              $this->horizontal .
 455              $this->vertical .
 456              $this->textRotation .
 457              ($this->wrapText ? 't' : 'f') .
 458              ($this->shrinkToFit ? 't' : 'f') .
 459              $this->indent .
 460              $this->readorder .
 461              __CLASS__
 462          );
 463      }
 464  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1