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

Documentation generated on Tue, 01 Jun 2010 17:00:40 +0200 by phpDocumentor 1.4.3