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

Documentation generated on Mon, 11 Jan 2010 08:03:41 +0100 by phpDocumentor 1.4.1