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

Source for file Fill.php

Documentation is available at Fill.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_IComparable */
  33. require_once 'PHPExcel/IComparable.php';
  34.  
  35.  
  36. /**
  37.  * PHPExcel_Style_Fill
  38.  *
  39.  * @category   PHPExcel
  40.  * @package    PHPExcel_Style
  41.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  42.  */
  43. class PHPExcel_Style_Fill implements PHPExcel_IComparable
  44. {
  45.     /* Fill types */
  46.     const FILL_NONE                            'none';
  47.     const FILL_SOLID                        'solid';
  48.     const FILL_GRADIENT_LINEAR                'linear';
  49.     const FILL_GRADIENT_PATH                'path';
  50.     const FILL_PATTERN_DARKDOWN                'darkDown';
  51.     const FILL_PATTERN_DARKGRAY                'darkGray';
  52.     const FILL_PATTERN_DARKGRID                'darkGrid';
  53.     const FILL_PATTERN_DARKHORIZONTAL        'darkHorizontal';
  54.     const FILL_PATTERN_DARKTRELLIS            'darkTrellis';
  55.     const FILL_PATTERN_DARKUP                'darkUp';
  56.     const FILL_PATTERN_DARKVERTICAL            'darkVertical';
  57.     const FILL_PATTERN_GRAY0625                'gray0625';
  58.     const FILL_PATTERN_GRAY125                'gray125';
  59.     const FILL_PATTERN_LIGHTDOWN            'lightDown';
  60.     const FILL_PATTERN_LIGHTGRAY            'lightGray';
  61.     const FILL_PATTERN_LIGHTGRID            'lightGrid';
  62.     const FILL_PATTERN_LIGHTHORIZONTAL        'lightHorizontal';
  63.     const FILL_PATTERN_LIGHTTRELLIS            'lightTrellis';
  64.     const FILL_PATTERN_LIGHTUP                'lightUp';
  65.     const FILL_PATTERN_LIGHTVERTICAL        'lightVertical';
  66.     const FILL_PATTERN_MEDIUMGRAY            'mediumGray';
  67.  
  68.     /**
  69.      * Fill type
  70.      *
  71.      * @var string 
  72.      */
  73.     private $_fillType;
  74.     
  75.     /**
  76.      * Rotation
  77.      *
  78.      * @var double 
  79.      */
  80.     private $_rotation;
  81.     
  82.     /**
  83.      * Start color
  84.      * 
  85.      * @var PHPExcel_Style_Color 
  86.      */
  87.     private $_startColor;
  88.     
  89.     /**
  90.      * End color
  91.      * 
  92.      * @var PHPExcel_Style_Color 
  93.      */
  94.     private $_endColor;
  95.     
  96.     /**
  97.      * Parent Style
  98.      *
  99.      * @var PHPExcel_Style 
  100.      */
  101.      
  102.     private $_parent;
  103.     
  104.     /**
  105.      * Parent Borders
  106.      *
  107.      * @var _parentPropertyName string
  108.      */
  109.     private $_parentPropertyName;
  110.         
  111.     /**
  112.      * Create a new PHPExcel_Style_Fill
  113.      */
  114.     public function __construct()
  115.     {
  116.         // Initialise values
  117.         $this->_fillType            = PHPExcel_Style_Fill::FILL_NONE;
  118.         $this->_rotation            = 0;
  119.         $this->_startColor            = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_WHITE);
  120.         $this->_endColor            = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK);
  121.     }
  122.  
  123.     /**
  124.      * Property Prepare bind
  125.      *
  126.      * Configures this object for late binding as a property of a parent object
  127.      *     
  128.      * @param $parent 
  129.      * @param $parentPropertyName 
  130.      */
  131.     public function propertyPrepareBind($parent$parentPropertyName)
  132.     {
  133.         // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  134.         // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  135.         $this->_parent                = $parent;
  136.         $this->_parentPropertyName    = $parentPropertyName;
  137.     }
  138.  
  139.     /**
  140.      * Property Get Bound
  141.      *
  142.      * Returns the PHPExcel_Style_Fill that is actual bound to PHPExcel_Style
  143.      *
  144.      * @return PHPExcel_Style_Fill 
  145.      */
  146.     private function propertyGetBound({
  147.         if(!isset($this->_parent))
  148.             return $this;                                                                // I am bound
  149.  
  150.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  151.             return $this->_parent->getFill();                                            // Another one is bound
  152.  
  153.         return $this;                                                                    // No one is bound yet
  154.     }
  155.     
  156.     /**
  157.      * Property Begin Bind
  158.      *
  159.      * If no PHPExcel_Style_Fill has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  160.      *
  161.      * @return PHPExcel_Style_Fill 
  162.      */
  163.     private function propertyBeginBind({
  164.         if(!isset($this->_parent))
  165.             return $this;                                                                // I am already bound
  166.  
  167.         if($this->_parent->propertyIsBound($this->_parentPropertyName))
  168.             return $this->_parent->getFill();                                            // Another one is already bound
  169.             
  170.         $this->_parent->propertyCompleteBind($this$this->_parentPropertyName);        // Bind myself
  171.         $this->_parent = null;
  172.         
  173.         return $this;
  174.     }
  175.         
  176.     /**
  177.      * Apply styles from array
  178.      * 
  179.      * <code>
  180.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  181.      *         array(
  182.      *             'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
  183.      *             'rotation'   => 0,
  184.      *             'startcolor' => array(
  185.      *                 'rgb' => '000000'
  186.      *             ),
  187.      *             'endcolor'   => array(
  188.      *                 'argb' => 'FFFFFFFF'
  189.      *             )
  190.      *         )
  191.      * );
  192.      * </code>
  193.      * 
  194.      * @param    array    $pStyles    Array containing style information
  195.      * @throws    Exception
  196.      */
  197.     public function applyFromArray($pStyles null{
  198.         if (is_array($pStyles)) {
  199.             if (array_key_exists('type'$pStyles)) {
  200.                 $this->setFillType($pStyles['type']);
  201.             }
  202.             if (array_key_exists('rotation'$pStyles)) {
  203.                 $this->setRotation($pStyles['rotation']);
  204.             }
  205.             if (array_key_exists('startcolor'$pStyles)) {
  206.                 $this->getStartColor()->applyFromArray($pStyles['startcolor']);
  207.             }
  208.             if (array_key_exists('endcolor'$pStyles)) {
  209.                 $this->getEndColor()->applyFromArray($pStyles['endcolor']);
  210.             }
  211.             if (array_key_exists('color'$pStyles)) {
  212.                 $this->getStartColor()->applyFromArray($pStyles['color']);
  213.             }
  214.         else {
  215.             throw new Exception("Invalid style array passed.");
  216.         }
  217.     }
  218.     
  219.     /**
  220.      * Get Fill Type
  221.      *
  222.      * @return string 
  223.      */
  224.     public function getFillType({
  225.         $property $this->propertyGetBound();
  226.     
  227.         if ($property->_fillType == ''{
  228.             $property->_fillType self::FILL_NONE;
  229.         }
  230.         return $property->_fillType;
  231.     }
  232.     
  233.     /**
  234.      * Set Fill Type
  235.      *
  236.      * @param string $pValue    PHPExcel_Style_Fill fill type
  237.      */
  238.     public function setFillType($pValue PHPExcel_Style_Fill::FILL_NONE{
  239.         $this->propertyBeginBind()->_fillType $pValue;
  240.     }
  241.     
  242.     /**
  243.      * Get Rotation
  244.      *
  245.      * @return double 
  246.      */
  247.     public function getRotation({
  248.         return $this->propertyGetBound()->_rotation;
  249.     }
  250.     
  251.     /**
  252.      * Set Rotation
  253.      *
  254.      * @param double $pValue 
  255.      */
  256.     public function setRotation($pValue 0{
  257.         $this->propertyBeginBind()->_rotation $pValue;
  258.     }
  259.     
  260.     /**
  261.      * Get Start Color
  262.      *
  263.      * @return PHPExcel_Style_Color 
  264.      */
  265.     public function getStartColor({
  266.         // It's a get but it may lead to a modified color which we won't detect but in which case we must bind.
  267.         // So bind as an assurance.
  268.         return $this->propertyBeginBind()->_startColor;
  269.     }
  270.     
  271.     /**
  272.      * Set Start Color
  273.      *
  274.      * @param     PHPExcel_Style_Color $pValue 
  275.      * @throws     Exception
  276.      */
  277.     public function setStartColor(PHPExcel_Style_Color $pValue null{
  278.            $this->propertyBeginBind()->_startColor $pValue;
  279.     }
  280.     
  281.     /**
  282.      * Get End Color
  283.      *
  284.      * @return PHPExcel_Style_Color 
  285.      */
  286.     public function getEndColor({
  287.         // It's a get but it may lead to a modified color which we won't detect but in which case we must bind.
  288.         // So bind as an assurance.
  289.         return $this->propertyBeginBind()->_endColor;
  290.     }
  291.     
  292.     /**
  293.      * Set End Color
  294.      *
  295.      * @param     PHPExcel_Style_Color $pValue 
  296.      * @throws     Exception
  297.      */
  298.     public function setEndColor(PHPExcel_Style_Color $pValue null{
  299.            $this->propertyBeginBind()->_endColor $pValue;
  300.     }
  301.  
  302.     /**
  303.      * Get hash code
  304.      *
  305.      * @return string    Hash code
  306.      */    
  307.     public function getHashCode({
  308.         $property $this->propertyGetBound();
  309.         return md5(
  310.               $property->getFillType()
  311.             . $property->getRotation()
  312.             . $property->getStartColor()->getHashCode()
  313.             . $property->getEndColor()->getHashCode()
  314.             . __CLASS__
  315.         );
  316.     }
  317.     
  318.     /**
  319.      * Hash index
  320.      *
  321.      * @var string 
  322.      */
  323.     private $_hashIndex;
  324.     
  325.     /**
  326.      * Get hash index
  327.      * 
  328.      * Note that this index may vary during script execution! Only reliable moment is
  329.      * while doing a write of a workbook and when changes are not allowed.
  330.      *
  331.      * @return string    Hash index
  332.      */
  333.     public function getHashIndex({
  334.         return $this->_hashIndex;
  335.     }
  336.     
  337.     /**
  338.      * Set hash index
  339.      * 
  340.      * Note that this index may vary during script execution! Only reliable moment is
  341.      * while doing a write of a workbook and when changes are not allowed.
  342.      *
  343.      * @param string    $value    Hash index
  344.      */
  345.     public function setHashIndex($value{
  346.         $this->_hashIndex = $value;
  347.     }
  348.         
  349.     /**
  350.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  351.      */
  352.     public function __clone({
  353.         $vars get_object_vars($this);
  354.         foreach ($vars as $key => $value{
  355.             if (is_object($value)) {
  356.                 $this->$key clone $value;
  357.             else {
  358.                 $this->$key $value;
  359.             }
  360.         }
  361.     }
  362. }

Documentation generated on Mon, 05 Jan 2009 20:37:13 +0100 by phpDocumentor 1.4.1