PHPExcel_Style
[ class tree: PHPExcel_Style ] [ index: PHPExcel_Style ] [ all elements ]

Source for file Alignment.php

Documentation is available at Alignment.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Style_Alignment
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Style
  34.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. class PHPExcel_Style_Alignment implements PHPExcel_IComparable
  37. {
  38.     /* Horizontal alignment styles */
  39.     const HORIZONTAL_GENERAL                'general';
  40.     const HORIZONTAL_LEFT                    'left';
  41.     const HORIZONTAL_RIGHT                    'right';
  42.     const HORIZONTAL_CENTER                    'center';
  43.     const HORIZONTAL_CENTER_CONTINUOUS        'centerContinuous';
  44.     const HORIZONTAL_JUSTIFY                'justify';
  45.  
  46.     /* Vertical alignment styles */
  47.     const VERTICAL_BOTTOM                    'bottom';
  48.     const VERTICAL_TOP                        'top';
  49.     const VERTICAL_CENTER                    'center';
  50.     const VERTICAL_JUSTIFY                    'justify';
  51.  
  52.     /**
  53.      * Horizontal
  54.      *
  55.      * @var string 
  56.      */
  57.     private $_horizontal    PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  58.  
  59.     /**
  60.      * Vertical
  61.      *
  62.      * @var string 
  63.      */
  64.     private $_vertical        PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  65.  
  66.     /**
  67.      * Text rotation
  68.      *
  69.      * @var int 
  70.      */
  71.     private $_textRotation    0;
  72.  
  73.     /**
  74.      * Wrap text
  75.      *
  76.      * @var boolean 
  77.      */
  78.     private $_wrapText        false;
  79.  
  80.     /**
  81.      * Shrink to fit
  82.      *
  83.      * @var boolean 
  84.      */
  85.     private $_shrinkToFit    false;
  86.  
  87.     /**
  88.      * Indent - only possible with horizontal alignment left and right
  89.      *
  90.      * @var int 
  91.      */
  92.     private $_indent        0;
  93.  
  94.     /**
  95.      * Parent Borders
  96.      *
  97.      * @var _parentPropertyName string
  98.      */
  99.     private $_parentPropertyName;
  100.  
  101.     /**
  102.      * Supervisor?
  103.      *
  104.      * @var boolean 
  105.      */
  106.     private $_isSupervisor;
  107.  
  108.     /**
  109.      * Parent. Only used for supervisor
  110.      *
  111.      * @var PHPExcel_Style 
  112.      */
  113.     private $_parent;
  114.  
  115.     /**
  116.      * Create a new PHPExcel_Style_Alignment
  117.      */
  118.     public function __construct($isSupervisor false)
  119.     {
  120.         // Supervisor?
  121.         $this->_isSupervisor $isSupervisor;
  122.     }
  123.  
  124.     /**
  125.      * Bind parent. Only used for supervisor
  126.      *
  127.      * @param PHPExcel $parent 
  128.      * @return PHPExcel_Style_Alignment 
  129.      */
  130.     public function bindParent($parent)
  131.     {
  132.         $this->_parent $parent;
  133.         return $this;
  134.     }
  135.  
  136.     /**
  137.      * Is this a supervisor or a real style component?
  138.      *
  139.      * @return boolean 
  140.      */
  141.     public function getIsSupervisor()
  142.     {
  143.         return $this->_isSupervisor;
  144.     }
  145.  
  146.     /**
  147.      * Get the shared style component for the currently active cell in currently active sheet.
  148.      * Only used for style supervisor
  149.      *
  150.      * @return PHPExcel_Style_Alignment 
  151.      */
  152.     public function getSharedComponent()
  153.     {
  154.         return $this->_parent->getSharedComponent()->getAlignment();
  155.     }
  156.  
  157.     /**
  158.      * Get the currently active sheet. Only used for supervisor
  159.      *
  160.      * @return PHPExcel_Worksheet 
  161.      */
  162.     public function getActiveSheet()
  163.     {
  164.         return $this->_parent->getActiveSheet();
  165.     }
  166.  
  167.     /**
  168.      * Get the currently active cell coordinate in currently active sheet.
  169.      * Only used for supervisor
  170.      *
  171.      * @return string E.g. 'A1'
  172.      */
  173.     public function getSelectedCells()
  174.     {
  175.         return $this->getActiveSheet()->getSelectedCells();
  176.     }
  177.  
  178.     /**
  179.      * Get the currently active cell coordinate in currently active sheet.
  180.      * Only used for supervisor
  181.      *
  182.      * @return string E.g. 'A1'
  183.      */
  184.     public function getActiveCell()
  185.     {
  186.         return $this->getActiveSheet()->getActiveCell();
  187.     }
  188.  
  189.     /**
  190.      * Build style array from subcomponents
  191.      *
  192.      * @param array $array 
  193.      * @return array 
  194.      */
  195.     public function getStyleArray($array)
  196.     {
  197.         return array('alignment' => $array);
  198.     }
  199.  
  200.     /**
  201.      * Apply styles from array
  202.      *
  203.      * <code>
  204.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  205.      *        array(
  206.      *            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  207.      *            'vertical'   => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  208.      *            'rotation'   => 0,
  209.      *            'wrap'       => true
  210.      *        )
  211.      * );
  212.      * </code>
  213.      *
  214.      * @param    array    $pStyles    Array containing style information
  215.      * @throws    Exception
  216.      * @return PHPExcel_Style_Alignment 
  217.      */
  218.     public function applyFromArray($pStyles null{
  219.         if (is_array($pStyles)) {
  220.             if ($this->_isSupervisor{
  221.                 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  222.             else {
  223.                 if (array_key_exists('horizontal'$pStyles)) {
  224.                     $this->setHorizontal($pStyles['horizontal']);
  225.                 }
  226.                 if (array_key_exists('vertical'$pStyles)) {
  227.                     $this->setVertical($pStyles['vertical']);
  228.                 }
  229.                 if (array_key_exists('rotation'$pStyles)) {
  230.                     $this->setTextRotation($pStyles['rotation']);
  231.                 }
  232.                 if (array_key_exists('wrap'$pStyles)) {
  233.                     $this->setWrapText($pStyles['wrap']);
  234.                 }
  235.                 if (array_key_exists('shrinkToFit'$pStyles)) {
  236.                     $this->setShrinkToFit($pStyles['shrinkToFit']);
  237.                 }
  238.                 if (array_key_exists('indent'$pStyles)) {
  239.                     $this->setIndent($pStyles['indent']);
  240.                 }
  241.             }
  242.         else {
  243.             throw new Exception("Invalid style array passed.");
  244.         }
  245.         return $this;
  246.     }
  247.  
  248.     /**
  249.      * Get Horizontal
  250.      *
  251.      * @return string 
  252.      */
  253.     public function getHorizontal({
  254.         if ($this->_isSupervisor{
  255.             return $this->getSharedComponent()->getHorizontal();
  256.         }
  257.         return $this->_horizontal;
  258.     }
  259.  
  260.     /**
  261.      * Set Horizontal
  262.      *
  263.      * @param string $pValue 
  264.      * @return PHPExcel_Style_Alignment 
  265.      */
  266.     public function setHorizontal($pValue PHPExcel_Style_Alignment::HORIZONTAL_GENERAL{
  267.         if ($pValue == ''{
  268.             $pValue PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  269.         }
  270.  
  271.         if ($this->_isSupervisor{
  272.             $styleArray $this->getStyleArray(array('horizontal' => $pValue));
  273.             $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  274.         }
  275.         else {
  276.             $this->_horizontal $pValue;
  277.         }
  278.         return $this;
  279.     }
  280.  
  281.     /**
  282.      * Get Vertical
  283.      *
  284.      * @return string 
  285.      */
  286.     public function getVertical({
  287.         if ($this->_isSupervisor{
  288.             return $this->getSharedComponent()->getVertical();
  289.         }
  290.         return $this->_vertical;
  291.     }
  292.  
  293.     /**
  294.      * Set Vertical
  295.      *
  296.      * @param string $pValue 
  297.      * @return PHPExcel_Style_Alignment 
  298.      */
  299.     public function setVertical($pValue PHPExcel_Style_Alignment::VERTICAL_BOTTOM{
  300.         if ($pValue == ''{
  301.             $pValue PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  302.         }
  303.  
  304.         if ($this->_isSupervisor{
  305.             $styleArray $this->getStyleArray(array('vertical' => $pValue));
  306.             $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  307.         else {
  308.             $this->_vertical $pValue;
  309.         }
  310.         return $this;
  311.     }
  312.  
  313.     /**
  314.      * Get TextRotation
  315.      *
  316.      * @return int 
  317.      */
  318.     public function getTextRotation({
  319.         if ($this->_isSupervisor{
  320.             return $this->getSharedComponent()->getTextRotation();
  321.         }
  322.         return $this->_textRotation;
  323.     }
  324.  
  325.     /**
  326.      * Set TextRotation
  327.      *
  328.      * @param int $pValue 
  329.      * @throws Exception
  330.      * @return PHPExcel_Style_Alignment 
  331.      */
  332.     public function setTextRotation($pValue 0{
  333.         // Excel2007 value 255 => PHPExcel value -165
  334.         if ($pValue == 255{
  335.             $pValue = -165;
  336.         }
  337.  
  338.         // Set rotation
  339.         if ( ($pValue >= -90 && $pValue <= 90|| $pValue == -165 {
  340.             if ($this->_isSupervisor{
  341.                 $styleArray $this->getStyleArray(array('rotation' => $pValue));
  342.                 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  343.             else {
  344.                 $this->_textRotation $pValue;
  345.             }
  346.         else {
  347.             throw new Exception("Text rotation should be a value between -90 and 90.");
  348.         }
  349.  
  350.         return $this;
  351.     }
  352.  
  353.     /**
  354.      * Get Wrap Text
  355.      *
  356.      * @return boolean 
  357.      */
  358.     public function getWrapText({
  359.         if ($this->_isSupervisor{
  360.             return $this->getSharedComponent()->getWrapText();
  361.         }
  362.         return $this->_wrapText;
  363.     }
  364.  
  365.     /**
  366.      * Set Wrap Text
  367.      *
  368.      * @param boolean $pValue 
  369.      * @return PHPExcel_Style_Alignment 
  370.      */
  371.     public function setWrapText($pValue false{
  372.         if ($pValue == ''{
  373.             $pValue false;
  374.         }
  375.         if ($this->_isSupervisor{
  376.             $styleArray $this->getStyleArray(array('wrap' => $pValue));
  377.             $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  378.         else {
  379.             $this->_wrapText $pValue;
  380.         }
  381.         return $this;
  382.     }
  383.  
  384.     /**
  385.      * Get Shrink to fit
  386.      *
  387.      * @return boolean 
  388.      */
  389.     public function getShrinkToFit({
  390.         if ($this->_isSupervisor{
  391.             return $this->getSharedComponent()->getShrinkToFit();
  392.         }
  393.         return $this->_shrinkToFit;
  394.     }
  395.  
  396.     /**
  397.      * Set Shrink to fit
  398.      *
  399.      * @param boolean $pValue 
  400.      * @return PHPExcel_Style_Alignment 
  401.      */
  402.     public function setShrinkToFit($pValue false{
  403.         if ($pValue == ''{
  404.             $pValue false;
  405.         }
  406.         if ($this->_isSupervisor{
  407.             $styleArray $this->getStyleArray(array('shrinkToFit' => $pValue));
  408.             $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  409.         else {
  410.             $this->_shrinkToFit $pValue;
  411.         }
  412.         return $this;
  413.     }
  414.  
  415.     /**
  416.      * Get indent
  417.      *
  418.      * @return int 
  419.      */
  420.     public function getIndent({
  421.         if ($this->_isSupervisor{
  422.             return $this->getSharedComponent()->getIndent();
  423.         }
  424.         return $this->_indent;
  425.     }
  426.  
  427.     /**
  428.      * Set indent
  429.      *
  430.      * @param int $pValue 
  431.      * @return PHPExcel_Style_Alignment 
  432.      */
  433.     public function setIndent($pValue 0{
  434.         if ($pValue 0{
  435.             if ($this->getHorizontal(!= self::HORIZONTAL_GENERAL && $this->getHorizontal(!= self::HORIZONTAL_LEFT && $this->getHorizontal(!= self::HORIZONTAL_RIGHT{
  436.                 $pValue 0// indent not supported
  437.             }
  438.         }
  439.         if ($this->_isSupervisor{
  440.             $styleArray $this->getStyleArray(array('indent' => $pValue));
  441.             $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  442.         else {
  443.             $this->_indent $pValue;
  444.         }
  445.         return $this;
  446.     }
  447.  
  448.     /**
  449.      * Get hash code
  450.      *
  451.      * @return string    Hash code
  452.      */
  453.     public function getHashCode({
  454.         if ($this->_isSupervisor{
  455.             return $this->getSharedComponent()->getHashCode();
  456.         }
  457.         return md5(
  458.               $this->_horizontal
  459.             . $this->_vertical
  460.             . $this->_textRotation
  461.             . ($this->_wrapText 't' 'f')
  462.             . ($this->_shrinkToFit 't' 'f')
  463.             . $this->_indent
  464.             . __CLASS__
  465.         );
  466.     }
  467.  
  468.     /**
  469.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  470.      */
  471.     public function __clone({
  472.         $vars get_object_vars($this);
  473.         foreach ($vars as $key => $value{
  474.             if ((is_object($value)) && ($key != '_parent')) {
  475.                 $this->$key clone $value;
  476.             else {
  477.                 $this->$key $value;
  478.             }
  479.         }
  480.     }
  481. }

Documentation generated on Sun, 27 Feb 2011 16:26:49 -0800 by phpDocumentor 1.4.3