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 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.3, 2008-08-25
  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 - 2008 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.      * Parent Style
  92.      *
  93.      * @var PHPExcel_Style 
  94.      */
  95.      
  96.     private $_parent;
  97.     
  98.     /**
  99.      * Parent Borders
  100.      *
  101.      * @var _parentPropertyName string
  102.      */
  103.     private $_parentPropertyName;
  104.         
  105.     /**
  106.      * Create a new PHPExcel_Style_Alignment
  107.      */
  108.     public function __construct()
  109.     {
  110.         // Initialise values
  111.         $this->_horizontal            = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  112.         $this->_vertical            = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  113.         $this->_textRotation        = 0;
  114.         $this->_wrapText            = false;
  115.         $this->_shrinkToFit            = false;
  116.     }
  117.  
  118.     /**
  119.      * Property Prepare bind
  120.      *
  121.      * Configures this object for late binding as a property of a parent object
  122.      *     
  123.      * @param $parent 
  124.      * @param $parentPropertyName 
  125.      */
  126.     public function propertyPrepareBind($parent, $parentPropertyName)
  127.     {
  128.         // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  129.         // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  130.         $this->_parent                = $parent;
  131.         $this->_parentPropertyName    = $parentPropertyName;
  132.     }
  133.     
  134.     /**
  135.      * Property Get Bound
  136.      *
  137.      * Returns the PHPExcel_Style_Alignment that is actual bound to PHPExcel_Style
  138.      *
  139.      * @return PHPExcel_Style_Alignment 
  140.      */
  141.     private function propertyGetBound() {
  142.         if(!isset($this->_parent))
  143.             return $this;                                                                // I am bound
  144.  
  145.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  146.             return $this->_parent->getAlignment();                                        // Another one is bound
  147.  
  148.         return $this;                                                                    // No one is bound yet
  149.     }
  150.     
  151.     /**
  152.      * Property Begin Bind
  153.      *
  154.      * If no PHPExcel_Style_Alignment has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  155.      *
  156.      * @return PHPExcel_Style_Alignment 
  157.      */
  158.     private function propertyBeginBind() {
  159.         if(!isset($this->_parent))
  160.             return $this;                                                                // I am already bound
  161.  
  162.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  163.             return $this->_parent->getAlignment();                                        // Another one is already bound
  164.             
  165.         $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName);        // Bind myself
  166.         $this->_parent = null;
  167.         
  168.         return $this;
  169.     }
  170.     
  171.     /**
  172.      * Apply styles from array
  173.      * 
  174.      * <code>
  175.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  176.      *         array(
  177.      *             'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  178.      *             'vertical'   => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  179.      *             'rotation'   => 0,
  180.      *             'wrap'       => true
  181.      *         )
  182.      * );
  183.      * </code>
  184.      * 
  185.      * @param    array    $pStyles    Array containing style information
  186.      * @throws    Exception
  187.      */
  188.     public function applyFromArray($pStyles = null) {
  189.         if (is_array($pStyles)) {
  190.             if (array_key_exists('horizontal', $pStyles)) {
  191.                 $this->setHorizontal($pStyles['horizontal']);
  192.             }
  193.             if (array_key_exists('vertical', $pStyles)) {
  194.                 $this->setVertical($pStyles['vertical']);
  195.             }
  196.             if (array_key_exists('rotation', $pStyles)) {
  197.                 $this->setTextRotation($pStyles['rotation']);
  198.             }
  199.             if (array_key_exists('wrap', $pStyles)) {
  200.                 $this->setWrapText($pStyles['wrap']);
  201.             }
  202.             if (array_key_exists('shrinkToFit', $pStyles)) {
  203.                 $this->setShrinkToFit($pStyles['shrinkToFit']);
  204.             }
  205.         } else {
  206.             throw new Exception("Invalid style array passed.");
  207.         }
  208.     }
  209.     
  210.     /**
  211.      * Get Horizontal
  212.      *
  213.      * @return string 
  214.      */
  215.     public function getHorizontal() {
  216.         return $this->propertyGetBound()->_horizontal;
  217.     }
  218.     
  219.     /**
  220.      * Set Horizontal
  221.      *
  222.      * @param string $pValue 
  223.      */
  224.     public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL) {
  225.         if ($pValue == '') {
  226.             $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  227.         }
  228.         $this->propertyBeginBind()->_horizontal = $pValue;
  229.     }
  230.     
  231.     /**
  232.      * Get Vertical
  233.      *
  234.      * @return string 
  235.      */
  236.     public function getVertical() {
  237.         return $this->propertyGetBound()->_vertical;
  238.     }
  239.     
  240.     /**
  241.      * Set Vertical
  242.      *
  243.      * @param string $pValue 
  244.      */
  245.     public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM) {
  246.         if ($pValue == '') {
  247.             $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  248.         }
  249.         $this->propertyBeginBind()->_vertical = $pValue;
  250.     }
  251.     
  252.     /**
  253.      * Get TextRotation
  254.      *
  255.      * @return int 
  256.      */
  257.     public function getTextRotation() {
  258.         return $this->propertyGetBound()->_textRotation;
  259.     }
  260.     
  261.     /**
  262.      * Set TextRotation
  263.      *
  264.      * @param int $pValue 
  265.      * @throws Exception
  266.      */
  267.     public function setTextRotation($pValue = 0) {
  268.     // Excel2007 value 255 => PHPExcel value -165
  269.         if ($pValue == 255) {
  270.             $pValue = -165;
  271.         }
  272.  
  273.     // Set rotation
  274.         if ( ($pValue >= -90 && $pValue <= 90) || $pValue == -165 ) {
  275.             $this->propertyBeginBind()->_textRotation = $pValue;
  276.         } else {
  277.             throw new Exception("Text rotation should be a value between -90 and 90.");
  278.         }
  279.     }
  280.     
  281.     /**
  282.      * Get Wrap Text
  283.      *
  284.      * @return boolean 
  285.      */
  286.     public function getWrapText() {
  287.         return $this->propertyGetBound()->_wrapText;
  288.     }
  289.     
  290.     /**
  291.      * Set Wrap Text
  292.      *
  293.      * @param boolean $pValue 
  294.      */
  295.     public function setWrapText($pValue = false) {
  296.         if ($pValue == '') {
  297.             $pValue = false;
  298.         }
  299.         $this->propertyBeginBind()->_wrapText = $pValue;
  300.     }
  301.     
  302.     /**
  303.      * Get Shrink to fit
  304.      *
  305.      * @return boolean 
  306.      */
  307.     public function getShrinkToFit() {
  308.         return $this->propertyGetBound()->_shrinkToFit;
  309.     }
  310.     
  311.     /**
  312.      * Set Shrink to fit
  313.      *
  314.      * @param boolean $pValue 
  315.      */
  316.     public function setShrinkToFit($pValue = false) {
  317.         if ($pValue == '') {
  318.             $pValue = false;
  319.         }
  320.         $this->propertyBeginBind()->_shrinkToFit = $pValue;
  321.     }
  322.  
  323.     /**
  324.      * Get hash code
  325.      *
  326.      * @return string    Hash code
  327.      */    
  328.     public function getHashCode() {
  329.         $property = $this->propertyGetBound();
  330.         return md5(
  331.               $property->_horizontal
  332.             . $property->_vertical
  333.             . $property->_textRotation
  334.             . ($property->_wrapText ? 't' : 'f')
  335.             . ($property->_shrinkToFit ? 't' : 'f')
  336.             . __CLASS__
  337.         );
  338.     }
  339.     
  340.     /**
  341.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  342.      */
  343.     public function __clone() {
  344.         $vars = get_object_vars($this);
  345.         foreach ($vars as $key => $value) {
  346.             if (is_object($value)) {
  347.                 $this->$key = clone $value;
  348.             } else {
  349.                 $this->$key = $value;
  350.             }
  351.         }
  352.     }
  353. }

Documentation generated on Mon, 25 Aug 2008 13:29:45 +0200 by phpDocumentor 1.4.1