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

Source for file Style.php

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

Documentation generated on Mon, 05 Jan 2009 20:38:35 +0100 by phpDocumentor 1.4.1