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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.5, 2009-01-05
  26.  */
  27.  
  28.  
  29. /** PHPExcel_IComparable */
  30. require_once 'PHPExcel/IComparable.php';
  31.  
  32.  
  33. /**
  34.  * PHPExcel_Style_Alignment
  35.  *
  36.  * @category   PHPExcel
  37.  * @package    PHPExcel_Style
  38.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  39.  */
  40. class PHPExcel_Style_Alignment implements PHPExcel_IComparable
  41. {    
  42.     /* Horizontal alignment styles */
  43.     const HORIZONTAL_GENERAL                'general';
  44.     const HORIZONTAL_LEFT                    'left';
  45.     const HORIZONTAL_RIGHT                    'right';
  46.     const HORIZONTAL_CENTER                    'center';
  47.     const HORIZONTAL_JUSTIFY                'justify';
  48.     
  49.     /* Vertical alignment styles */
  50.     const VERTICAL_BOTTOM                    'bottom';
  51.     const VERTICAL_TOP                        'top';
  52.     const VERTICAL_CENTER                    'center';
  53.     const VERTICAL_JUSTIFY                    'justify';
  54.     
  55.     /**
  56.      * Horizontal
  57.      *
  58.      * @var string 
  59.      */
  60.     private $_horizontal;
  61.     
  62.     /**
  63.      * Vertical
  64.      *
  65.      * @var string 
  66.      */
  67.     private $_vertical;
  68.     
  69.     /**
  70.      * Text rotation
  71.      *
  72.      * @var int 
  73.      */
  74.     private $_textRotation;
  75.     
  76.     /**
  77.      * Wrap text
  78.      *
  79.      * @var boolean 
  80.      */
  81.     private $_wrapText;
  82.     
  83.     /**
  84.      * Shrink to fit
  85.      *
  86.      * @var boolean 
  87.      */
  88.     private $_shrinkToFit;
  89.     
  90.     /**
  91.      * Indent - only possible with horizontal alignment left and right
  92.      *
  93.      * @var int 
  94.      */
  95.     private $_indent;
  96.     
  97.     /**
  98.      * Parent Style
  99.      *
  100.      * @var PHPExcel_Style 
  101.      */
  102.      
  103.     private $_parent;
  104.     
  105.     /**
  106.      * Parent Borders
  107.      *
  108.      * @var _parentPropertyName string
  109.      */
  110.     private $_parentPropertyName;
  111.         
  112.     /**
  113.      * Create a new PHPExcel_Style_Alignment
  114.      */
  115.     public function __construct()
  116.     {
  117.         // Initialise values
  118.         $this->_horizontal            = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  119.         $this->_vertical            = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  120.         $this->_textRotation        = 0;
  121.         $this->_wrapText            = false;
  122.         $this->_shrinkToFit            = false;
  123.         $this->_indent                = 0;
  124.     }
  125.  
  126.     /**
  127.      * Property Prepare bind
  128.      *
  129.      * Configures this object for late binding as a property of a parent object
  130.      *     
  131.      * @param $parent 
  132.      * @param $parentPropertyName 
  133.      */
  134.     public function propertyPrepareBind($parent$parentPropertyName)
  135.     {
  136.         // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  137.         // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  138.         $this->_parent                = $parent;
  139.         $this->_parentPropertyName    = $parentPropertyName;
  140.     }
  141.     
  142.     /**
  143.      * Property Get Bound
  144.      *
  145.      * Returns the PHPExcel_Style_Alignment that is actual bound to PHPExcel_Style
  146.      *
  147.      * @return PHPExcel_Style_Alignment 
  148.      */
  149.     private function propertyGetBound({
  150.         if(!isset($this->_parent))
  151.             return $this;                                                                // I am bound
  152.  
  153.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  154.             return $this->_parent->getAlignment();                                        // Another one is bound
  155.  
  156.         return $this;                                                                    // No one is bound yet
  157.     }
  158.     
  159.     /**
  160.      * Property Begin Bind
  161.      *
  162.      * If no PHPExcel_Style_Alignment has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  163.      *
  164.      * @return PHPExcel_Style_Alignment 
  165.      */
  166.     private function propertyBeginBind({
  167.         if(!isset($this->_parent))
  168.             return $this;                                                                // I am already bound
  169.  
  170.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  171.             return $this->_parent->getAlignment();                                        // Another one is already bound
  172.             
  173.         $this->_parent->propertyCompleteBind($this$this->_parentPropertyName);        // Bind myself
  174.         $this->_parent = null;
  175.         
  176.         return $this;
  177.     }
  178.     
  179.     /**
  180.      * Apply styles from array
  181.      * 
  182.      * <code>
  183.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  184.      *         array(
  185.      *             'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  186.      *             'vertical'   => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  187.      *             'rotation'   => 0,
  188.      *             'wrap'       => true
  189.      *         )
  190.      * );
  191.      * </code>
  192.      * 
  193.      * @param    array    $pStyles    Array containing style information
  194.      * @throws    Exception
  195.      */
  196.     public function applyFromArray($pStyles null{
  197.         if (is_array($pStyles)) {
  198.             if (array_key_exists('horizontal'$pStyles)) {
  199.                 $this->setHorizontal($pStyles['horizontal']);
  200.             }
  201.             if (array_key_exists('vertical'$pStyles)) {
  202.                 $this->setVertical($pStyles['vertical']);
  203.             }
  204.             if (array_key_exists('rotation'$pStyles)) {
  205.                 $this->setTextRotation($pStyles['rotation']);
  206.             }
  207.             if (array_key_exists('wrap'$pStyles)) {
  208.                 $this->setWrapText($pStyles['wrap']);
  209.             }
  210.             if (array_key_exists('shrinkToFit'$pStyles)) {
  211.                 $this->setShrinkToFit($pStyles['shrinkToFit']);
  212.             }
  213.             if (array_key_exists('indent'$pStyles)) {
  214.                 $this->setIndent($pStyles['indent']);
  215.             }
  216.         else {
  217.             throw new Exception("Invalid style array passed.");
  218.         }
  219.     }
  220.     
  221.     /**
  222.      * Get Horizontal
  223.      *
  224.      * @return string 
  225.      */
  226.     public function getHorizontal({
  227.         return $this->propertyGetBound()->_horizontal;
  228.     }
  229.     
  230.     /**
  231.      * Set Horizontal
  232.      *
  233.      * @param string $pValue 
  234.      */
  235.     public function setHorizontal($pValue PHPExcel_Style_Alignment::HORIZONTAL_GENERAL{
  236.         if ($pValue == ''{
  237.             $pValue PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  238.         }
  239.         $this->propertyBeginBind()->_horizontal $pValue;
  240.     }
  241.     
  242.     /**
  243.      * Get Vertical
  244.      *
  245.      * @return string 
  246.      */
  247.     public function getVertical({
  248.         return $this->propertyGetBound()->_vertical;
  249.     }
  250.     
  251.     /**
  252.      * Set Vertical
  253.      *
  254.      * @param string $pValue 
  255.      */
  256.     public function setVertical($pValue PHPExcel_Style_Alignment::VERTICAL_BOTTOM{
  257.         if ($pValue == ''{
  258.             $pValue PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  259.         }
  260.         $this->propertyBeginBind()->_vertical $pValue;
  261.     }
  262.     
  263.     /**
  264.      * Get TextRotation
  265.      *
  266.      * @return int 
  267.      */
  268.     public function getTextRotation({
  269.         return $this->propertyGetBound()->_textRotation;
  270.     }
  271.     
  272.     /**
  273.      * Set TextRotation
  274.      *
  275.      * @param int $pValue 
  276.      * @throws Exception
  277.      */
  278.     public function setTextRotation($pValue 0{
  279.     // Excel2007 value 255 => PHPExcel value -165
  280.         if ($pValue == 255{
  281.             $pValue = -165;
  282.         }
  283.  
  284.     // Set rotation
  285.         if ( ($pValue >= -90 && $pValue <= 90|| $pValue == -165 {
  286.             $this->propertyBeginBind()->_textRotation $pValue;
  287.         else {
  288.             throw new Exception("Text rotation should be a value between -90 and 90.");
  289.         }
  290.     }
  291.     
  292.     /**
  293.      * Get Wrap Text
  294.      *
  295.      * @return boolean 
  296.      */
  297.     public function getWrapText({
  298.         return $this->propertyGetBound()->_wrapText;
  299.     }
  300.     
  301.     /**
  302.      * Set Wrap Text
  303.      *
  304.      * @param boolean $pValue 
  305.      */
  306.     public function setWrapText($pValue false{
  307.         if ($pValue == ''{
  308.             $pValue false;
  309.         }
  310.         $this->propertyBeginBind()->_wrapText $pValue;
  311.     }
  312.     
  313.     /**
  314.      * Get Shrink to fit
  315.      *
  316.      * @return boolean 
  317.      */
  318.     public function getShrinkToFit({
  319.         return $this->propertyGetBound()->_shrinkToFit;
  320.     }
  321.     
  322.     /**
  323.      * Set Shrink to fit
  324.      *
  325.      * @param boolean $pValue 
  326.      */
  327.     public function setShrinkToFit($pValue false{
  328.         if ($pValue == ''{
  329.             $pValue false;
  330.         }
  331.         $this->propertyBeginBind()->_shrinkToFit $pValue;
  332.     }
  333.  
  334.     /**
  335.      * Get indent
  336.      *
  337.      * @return int 
  338.      */
  339.     public function getIndent({
  340.         return $this->propertyGetBound()->_indent;
  341.     }
  342.     
  343.     /**
  344.      * Set indent
  345.      *
  346.      * @param int $pValue 
  347.      */
  348.     public function setIndent($pValue 0{
  349.         if ($pValue 0{
  350.             if ($this->getHorizontal(!= self::HORIZONTAL_GENERAL && $this->getHorizontal(!= self::HORIZONTAL_LEFT && $this->getHorizontal(!= self::HORIZONTAL_RIGHT{
  351.                 $pValue 0// indent not supported
  352.             }
  353.         }
  354.         
  355.         $this->propertyBeginBind()->_indent $pValue;
  356.     }
  357.     
  358.     /**
  359.      * Get hash code
  360.      *
  361.      * @return string    Hash code
  362.      */    
  363.     public function getHashCode({
  364.         $property $this->propertyGetBound();
  365.         return md5(
  366.               $property->_horizontal
  367.             . $property->_vertical
  368.             . $property->_textRotation
  369.             . ($property->_wrapText 't' 'f')
  370.             . ($property->_shrinkToFit 't' 'f')
  371.             . $property->_indent
  372.             . __CLASS__
  373.         );
  374.     }
  375.     
  376.     /**
  377.      * Hash index
  378.      *
  379.      * @var string 
  380.      */
  381.     private $_hashIndex;
  382.     
  383.     /**
  384.      * Get hash index
  385.      * 
  386.      * Note that this index may vary during script execution! Only reliable moment is
  387.      * while doing a write of a workbook and when changes are not allowed.
  388.      *
  389.      * @return string    Hash index
  390.      */
  391.     public function getHashIndex({
  392.         return $this->_hashIndex;
  393.     }
  394.     
  395.     /**
  396.      * Set hash index
  397.      * 
  398.      * Note that this index may vary during script execution! Only reliable moment is
  399.      * while doing a write of a workbook and when changes are not allowed.
  400.      *
  401.      * @param string    $value    Hash index
  402.      */
  403.     public function setHashIndex($value{
  404.         $this->_hashIndex = $value;
  405.     }
  406.     
  407.     /**
  408.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  409.      */
  410.     public function __clone({
  411.         $vars get_object_vars($this);
  412.         foreach ($vars as $key => $value{
  413.             if (is_object($value)) {
  414.                 $this->$key clone $value;
  415.             else {
  416.                 $this->$key $value;
  417.             }
  418.         }
  419.     }
  420. }

Documentation generated on Mon, 05 Jan 2009 20:36:24 +0100 by phpDocumentor 1.4.1