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

Source for file Style.php

Documentation is available at Style.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
  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_Style_Color */
  30. require_once 'PHPExcel/Style/Color.php';
  31.  
  32. /** PHPExcel_Style_Font */
  33. require_once 'PHPExcel/Style/Font.php';
  34.  
  35. /** PHPExcel_Style_Fill */
  36. require_once 'PHPExcel/Style/Fill.php';
  37.  
  38. /** PHPExcel_Style_Borders */
  39. require_once 'PHPExcel/Style/Borders.php';
  40.  
  41. /** PHPExcel_Style_Alignment */
  42. require_once 'PHPExcel/Style/Alignment.php';
  43.  
  44. /** PHPExcel_Style_NumberFormat */
  45. require_once 'PHPExcel/Style/NumberFormat.php';
  46.  
  47. /** PHPExcel_Style_Conditional */
  48. require_once 'PHPExcel/Style/Conditional.php';
  49.  
  50. /** PHPExcel_Style_Protection */
  51. require_once 'PHPExcel/Style/Protection.php';
  52.  
  53. /** PHPExcel_IComparable */
  54. require_once 'PHPExcel/IComparable.php';
  55.  
  56. /**
  57.  * PHPExcel_Style
  58.  *
  59.  * @category   PHPExcel
  60.  * @package    PHPExcel_Cell
  61.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  62.  */
  63. class PHPExcel_Style implements PHPExcel_IComparable
  64. {
  65.     /**
  66.      * Font
  67.      *
  68.      * @var PHPExcel_Style_Font 
  69.      */
  70.     private $_font;
  71.     
  72.     /**
  73.      * Fill
  74.      *
  75.      * @var PHPExcel_Style_Fill 
  76.      */
  77.     private $_fill;
  78.  
  79.     /**
  80.      * Borders
  81.      *
  82.      * @var PHPExcel_Style_Borders 
  83.      */
  84.     private $_borders;
  85.     
  86.     /**
  87.      * Alignment
  88.      *
  89.      * @var PHPExcel_Style_Alignment 
  90.      */
  91.     private $_alignment;
  92.     
  93.     /**
  94.      * Number Format
  95.      *
  96.      * @var PHPExcel_Style_NumberFormat 
  97.      */
  98.     private $_numberFormat;
  99.     
  100.     /**
  101.      * Conditional styles
  102.      *
  103.      * @var PHPExcel_Style_Conditional[] 
  104.      */
  105.     private $_conditionalStyles;
  106.     
  107.     /**
  108.      * Protection
  109.      *
  110.      * @var PHPExcel_Style_Protection 
  111.      */
  112.     private $_protection;
  113.     
  114.     /**
  115.      * Create a new PHPExcel_Style
  116.      */
  117.     public function __construct()
  118.     {
  119.         // Initialise values
  120.  
  121.         /**
  122.          * The following properties are late bound. Binding is initiated by property classes when they are modified.
  123.          *
  124.          * _font
  125.          * _fill
  126.          * _borders
  127.          * _alignment
  128.          * _numberFormat
  129.          * _protection
  130.          *
  131.          */
  132.  
  133.         $this->_conditionalStyles     = array();
  134.     }
  135.  
  136.     /**
  137.      * Property Complete Bind
  138.      *
  139.      * Complete the binding process a child property object started
  140.      *
  141.      * @param    $propertyObject 
  142.      * @param    $propertyName            Name of this property in the parent object
  143.      * @throws Exception
  144.      */ 
  145.     public function propertyCompleteBind($propertyObject, $propertyName) {
  146.         switch($propertyName) {
  147.             case "_font":
  148.                 $this->_font = $propertyObject;
  149.                 break;
  150.                 
  151.             case "_fill":
  152.                 $this->_fill = $propertyObject;
  153.                 break;
  154.                 
  155.             case "_borders":
  156.                 $this->_borders = $propertyObject;
  157.                 break;
  158.                 
  159.             case "_alignment":
  160.                 $this->_alignment = $propertyObject;
  161.                 break;
  162.                 
  163.             case "_numberFormat":
  164.                 $this->_numberFormat = $propertyObject;
  165.                 break;
  166.                 
  167.             case "_protection":
  168.                 $this->_protection = $propertyObject;
  169.                 break;
  170.             
  171.             default:
  172.                 throw new Exception("Invalid property passed.");
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * Property Is Bound
  178.      *
  179.      * Determines if a child property is bound to this one
  180.      *
  181.      * @param    $propertyName            Name of this property in the parent object
  182.      *
  183.      * @return boolean 
  184.      *
  185.      * @throws Exception
  186.      */
  187.     public function propertyIsBound($propertyName) {
  188.         switch($propertyName) {
  189.             case "_font":
  190.                 return isset($this->_font);
  191.                 
  192.             case "_fill":
  193.                 return isset($this->_fill);
  194.                 
  195.             case "_borders":
  196.                 return isset($this->_borders);
  197.                 
  198.             case "_alignment":
  199.                 return isset($this->_alignment);
  200.                 
  201.             case "_numberFormat":
  202.                 return isset($this->_numberFormat);
  203.                 
  204.             case "_protection":
  205.                 return isset($this->_protection);
  206.                 
  207.             default:
  208.                 throw new Exception("Invalid property passed.");
  209.         }
  210.     }
  211.     
  212.     /**
  213.      * Apply styles from array
  214.      * 
  215.      * <code>
  216.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray(
  217.      *         array(
  218.      *             'font'    => array(
  219.      *                 'name'      => 'Arial',
  220.      *                 'bold'      => true,
  221.      *                 'italic'    => false,
  222.      *                 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  223.      *                 'strike'    => false,
  224.      *                 'color'     => array(
  225.      *                     'rgb' => '808080'
  226.      *                 )
  227.      *             ),
  228.      *             'borders' => array(
  229.      *                 'bottom'     => array(
  230.      *                     'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  231.      *                     'color' => array(
  232.      *                         'rgb' => '808080'
  233.      *                     )
  234.      *                 ),
  235.      *                 'top'     => array(
  236.      *                     'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  237.      *                     'color' => array(
  238.      *                         'rgb' => '808080'
  239.      *                     )
  240.      *                 )
  241.      *             )
  242.      *         )
  243.      * );
  244.      * </code>
  245.      * 
  246.      * @param    array    $pStyles    Array containing style information
  247.      * @throws    Exception
  248.      */
  249.     public function applyFromArray($pStyles = null) {
  250.         if (is_array($pStyles)) {
  251.             if (array_key_exists('fill', $pStyles)) {
  252.                 $this->getFill()->applyFromArray($pStyles['fill']);
  253.             }
  254.               if (array_key_exists('font', $pStyles)) {
  255.                 $this->getFont()->applyFromArray($pStyles['font']);
  256.             }
  257.             if (array_key_exists('borders', $pStyles)) {
  258.                 $this->getBorders()->applyFromArray($pStyles['borders']);
  259.             }
  260.             if (array_key_exists('alignment', $pStyles)) {
  261.                 $this->getAlignment()->applyFromArray($pStyles['alignment']);
  262.             }
  263.                if (array_key_exists('numberformat', $pStyles)) {
  264.                 $this->getNumberFormat()->applyFromArray($pStyles['numberformat']);
  265.             }
  266.             if (array_key_exists('protection', $pStyles)) {
  267.                 $this->getProtection()->applyFromArray($pStyles['protection']);
  268.             }
  269.         } else {
  270.             throw new Exception("Invalid style array passed.");
  271.         }
  272.     }
  273.     
  274.     /**
  275.      * Get Fill
  276.      *
  277.      * @return PHPExcel_Style_Fill 
  278.      */
  279.     public function getFill() {
  280.         if(isset($this->_fill))
  281.             return $this->_fill;
  282.  
  283.         $property = new PHPExcel_Style_Fill();
  284.         $property->propertyPrepareBind($this, "_fill");
  285.         return $property;
  286.     }
  287.     
  288.     /**
  289.      * Get Font
  290.      *
  291.      * @return PHPExcel_Style_Font 
  292.      */
  293.     public function getFont() {
  294.         if(isset($this->_font))
  295.             return $this->_font;
  296.  
  297.         $property = new PHPExcel_Style_Font();
  298.         $property->propertyPrepareBind($this, "_font");
  299.         return $property;
  300.     }
  301.     
  302.     /**
  303.      * Get Borders
  304.      *
  305.      * @return PHPExcel_Style_Borders 
  306.      */
  307.     public function getBorders() {
  308.         if(isset($this->_borders))
  309.             return $this->_borders;
  310.  
  311.         $property = new PHPExcel_Style_Borders();
  312.         $property->propertyPrepareBind($this, "_borders");
  313.         return $property;
  314.     }
  315.     
  316.     /**
  317.      * Get Alignment
  318.      *
  319.      * @return PHPExcel_Style_Alignment 
  320.      */
  321.     public function getAlignment() {
  322.         if(isset($this->_alignment))
  323.             return $this->_alignment;
  324.  
  325.         $property = new PHPExcel_Style_Alignment();
  326.         $property->propertyPrepareBind($this, "_alignment");
  327.         return $property;
  328.     }
  329.     
  330.     /**
  331.      * Get Number Format
  332.      *
  333.      * @return PHPExcel_Style_NumberFormat 
  334.      */
  335.     public function getNumberFormat() {
  336.         if(isset($this->_numberFormat))
  337.             return $this->_numberFormat;
  338.  
  339.         $property = new PHPExcel_Style_NumberFormat();
  340.         $property->propertyPrepareBind($this, "_numberFormat");
  341.         return $property;
  342.     }
  343.     
  344.     /**
  345.      * Get Conditional Styles
  346.      *
  347.      * @return PHPExcel_Style_Conditional[] 
  348.      */
  349.     public function getConditionalStyles() {
  350.         return $this->_conditionalStyles;
  351.     }
  352.        
  353.     /**
  354.      * Set Conditional Styles
  355.      *
  356.      * @param PHPExcel_Style_Conditional[]    $pValue    Array of condtional styles
  357.      */
  358.     public function setConditionalStyles($pValue = null) {
  359.         if (is_array($pValue)) {
  360.             $this->_conditionalStyles = $pValue;
  361.         }
  362.     }
  363.     
  364.     /**
  365.      * Get Protection
  366.      *
  367.      * @return PHPExcel_Style_Protection 
  368.      */
  369.     public function getProtection() {
  370.         if(isset($this->_protection))
  371.             return $this->_protection;
  372.  
  373.         $property = new PHPExcel_Style_Protection();
  374.         $property->propertyPrepareBind($this, "_protection");
  375.         return $property;
  376.     }
  377.    
  378.     /**
  379.      * Get hash code
  380.      *
  381.      * @return string    Hash code
  382.      */    
  383.     public function getHashCode() {
  384.         $hashConditionals = '';
  385.         foreach ($this->_conditionalStyles as $conditional) {
  386.             $hashConditionals .= $conditional->getHashCode();
  387.         }
  388.         
  389.         return md5(
  390.               $this->getFill()->getHashCode()
  391.             . $this->getFont()->getHashCode()
  392.             . $this->getBorders()->getHashCode()
  393.             . $this->getAlignment()->getHashCode()
  394.             . $this->getNumberFormat()->getHashCode()
  395.             . $hashConditionals
  396.             . $this->getProtection()->getHashCode()
  397.             . __CLASS__
  398.         );
  399.     }
  400.         
  401.     /**
  402.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  403.      */
  404.     public function __clone() {
  405.         $vars = get_object_vars($this);
  406.         foreach ($vars as $key => $value) {
  407.             if (is_object($value)) {
  408.                 $this->$key = clone $value;
  409.             } else {
  410.                 $this->$key = $value;
  411.             }
  412.         }
  413.     }
  414. }

Documentation generated on Mon, 25 Aug 2008 13:50:44 +0200 by phpDocumentor 1.4.1