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.7.0, 2009-08-10
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35. }
  36.  
  37. /** PHPExcel_Style_Color */
  38. require_once PHPEXCEL_ROOT 'PHPExcel/Style/Color.php';
  39.  
  40. /** PHPExcel_IComparable */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/IComparable.php';
  42.  
  43.  
  44. /**
  45.  * PHPExcel_Style_Fill
  46.  *
  47.  * @category   PHPExcel
  48.  * @package    PHPExcel_Style
  49.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  50.  */
  51. class PHPExcel_Style_Fill implements PHPExcel_IComparable
  52. {
  53.     /* Fill types */
  54.     const FILL_NONE                            'none';
  55.     const FILL_SOLID                        'solid';
  56.     const FILL_GRADIENT_LINEAR                'linear';
  57.     const FILL_GRADIENT_PATH                'path';
  58.     const FILL_PATTERN_DARKDOWN                'darkDown';
  59.     const FILL_PATTERN_DARKGRAY                'darkGray';
  60.     const FILL_PATTERN_DARKGRID                'darkGrid';
  61.     const FILL_PATTERN_DARKHORIZONTAL        'darkHorizontal';
  62.     const FILL_PATTERN_DARKTRELLIS            'darkTrellis';
  63.     const FILL_PATTERN_DARKUP                'darkUp';
  64.     const FILL_PATTERN_DARKVERTICAL            'darkVertical';
  65.     const FILL_PATTERN_GRAY0625                'gray0625';
  66.     const FILL_PATTERN_GRAY125                'gray125';
  67.     const FILL_PATTERN_LIGHTDOWN            'lightDown';
  68.     const FILL_PATTERN_LIGHTGRAY            'lightGray';
  69.     const FILL_PATTERN_LIGHTGRID            'lightGrid';
  70.     const FILL_PATTERN_LIGHTHORIZONTAL        'lightHorizontal';
  71.     const FILL_PATTERN_LIGHTTRELLIS            'lightTrellis';
  72.     const FILL_PATTERN_LIGHTUP                'lightUp';
  73.     const FILL_PATTERN_LIGHTVERTICAL        'lightVertical';
  74.     const FILL_PATTERN_MEDIUMGRAY            'mediumGray';
  75.  
  76.     /**
  77.      * Fill type
  78.      *
  79.      * @var string 
  80.      */
  81.     private $_fillType;
  82.     
  83.     /**
  84.      * Rotation
  85.      *
  86.      * @var double 
  87.      */
  88.     private $_rotation;
  89.     
  90.     /**
  91.      * Start color
  92.      * 
  93.      * @var PHPExcel_Style_Color 
  94.      */
  95.     private $_startColor;
  96.     
  97.     /**
  98.      * End color
  99.      * 
  100.      * @var PHPExcel_Style_Color 
  101.      */
  102.     private $_endColor;
  103.     
  104.     /**
  105.      * Parent Borders
  106.      *
  107.      * @var _parentPropertyName string
  108.      */
  109.     private $_parentPropertyName;
  110.  
  111.     /**
  112.      * Supervisor?
  113.      *
  114.      * @var boolean 
  115.      */
  116.     private $_isSupervisor;
  117.  
  118.     /**
  119.      * Parent. Only used for supervisor
  120.      *
  121.      * @var PHPExcel_Style 
  122.      */
  123.     private $_parent;
  124.  
  125.     /**
  126.      * Create a new PHPExcel_Style_Fill
  127.      */
  128.     public function __construct($isSupervisor false)
  129.     {
  130.         // Supervisor?
  131.         $this->_isSupervisor = $isSupervisor;
  132.  
  133.         // Initialise values
  134.         $this->_fillType            = PHPExcel_Style_Fill::FILL_NONE;
  135.         $this->_rotation            = 0;
  136.         $this->_startColor            = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_WHITE$isSupervisor);
  137.         $this->_endColor            = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK$isSupervisor);
  138.  
  139.         // bind parent if we are a supervisor
  140.         if ($isSupervisor{
  141.             $this->_startColor->bindParent($this'_startColor');
  142.             $this->_endColor->bindParent($this'_endColor');
  143.         }
  144.     }
  145.  
  146.     /**
  147.      * Bind parent. Only used for supervisor
  148.      *
  149.      * @param PHPExcel_Style $parent 
  150.      * @return PHPExcel_Style_Fill 
  151.      */
  152.     public function bindParent($parent)
  153.     {
  154.         $this->_parent = $parent;
  155.         return $this;
  156.     }
  157.  
  158.     /**
  159.      * Is this a supervisor or a real style component?
  160.      *
  161.      * @return boolean 
  162.      */
  163.     public function getIsSupervisor()
  164.     {
  165.         return $this->_isSupervisor;
  166.     }
  167.  
  168.     /**
  169.      * Get the shared style component for the currently active cell in currently active sheet.
  170.      * Only used for style supervisor
  171.      *
  172.      * @return PHPExcel_Style_Fill 
  173.      */
  174.     public function getSharedComponent()
  175.     {
  176.         return $this->_parent->getSharedComponent()->getFill();
  177.     }
  178.  
  179.     /**
  180.      * Get the currently active sheet. Only used for supervisor
  181.      *
  182.      * @return PHPExcel_Worksheet 
  183.      */
  184.     public function getActiveSheet()
  185.     {
  186.         return $this->_parent->getActiveSheet();
  187.     }
  188.  
  189.     /**
  190.      * Get the currently active cell coordinate in currently active sheet.
  191.      * Only used for supervisor
  192.      *
  193.      * @return string E.g. 'A1'
  194.      */
  195.     public function getXSelectedCells()
  196.     {
  197.         return $this->getActiveSheet()->getXSelectedCells();
  198.     }
  199.  
  200.     /**
  201.      * Get the currently active cell coordinate in currently active sheet.
  202.      * Only used for supervisor
  203.      *
  204.      * @return string E.g. 'A1'
  205.      */
  206.     public function getXActiveCell()
  207.     {
  208.         return $this->getActiveSheet()->getXActiveCell();
  209.     }
  210.  
  211.     /**
  212.      * Build style array from subcomponents
  213.      *
  214.      * @param array $array 
  215.      * @return array 
  216.      */
  217.     public function getStyleArray($array)
  218.     {
  219.         return array('fill' => $array);
  220.     }
  221.  
  222.     /**
  223.      * Apply styles from array
  224.      * 
  225.      * <code>
  226.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  227.      *         array(
  228.      *             'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
  229.      *             'rotation'   => 0,
  230.      *             'startcolor' => array(
  231.      *                 'rgb' => '000000'
  232.      *             ),
  233.      *             'endcolor'   => array(
  234.      *                 'argb' => 'FFFFFFFF'
  235.      *             )
  236.      *         )
  237.      * );
  238.      * </code>
  239.      * 
  240.      * @param    array    $pStyles    Array containing style information
  241.      * @throws    Exception
  242.      * @return PHPExcel_Style_Fill 
  243.      */
  244.     public function applyFromArray($pStyles null{
  245.         if (is_array($pStyles)) {
  246.             if ($this->_isSupervisor{
  247.                 $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  248.             else {
  249.                 if (array_key_exists('type'$pStyles)) {
  250.                     $this->setFillType($pStyles['type']);
  251.                 }
  252.                 if (array_key_exists('rotation'$pStyles)) {
  253.                     $this->setRotation($pStyles['rotation']);
  254.                 }
  255.                 if (array_key_exists('startcolor'$pStyles)) {
  256.                     $this->getStartColor()->applyFromArray($pStyles['startcolor']);
  257.                 }
  258.                 if (array_key_exists('endcolor'$pStyles)) {
  259.                     $this->getEndColor()->applyFromArray($pStyles['endcolor']);
  260.                 }
  261.                 if (array_key_exists('color'$pStyles)) {
  262.                     $this->getStartColor()->applyFromArray($pStyles['color']);
  263.                 }
  264.             }
  265.         else {
  266.             throw new Exception("Invalid style array passed.");
  267.         }
  268.         return $this;
  269.     }
  270.     
  271.     /**
  272.      * Get Fill Type
  273.      *
  274.      * @return string 
  275.      */
  276.     public function getFillType({
  277.         if ($this->_isSupervisor{
  278.             return $this->getSharedComponent()->getFillType();
  279.         }
  280.         return $this->_fillType;
  281.     }
  282.     
  283.     /**
  284.      * Set Fill Type
  285.      *
  286.      * @param string $pValue    PHPExcel_Style_Fill fill type
  287.      * @return PHPExcel_Style_Fill 
  288.      */
  289.     public function setFillType($pValue PHPExcel_Style_Fill::FILL_NONE{
  290.         if ($this->_isSupervisor{
  291.             $styleArray $this->getStyleArray(array('type' => $pValue));
  292.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  293.         else {
  294.             $this->_fillType = $pValue;
  295.         }
  296.         return $this;
  297.     }
  298.     
  299.     /**
  300.      * Get Rotation
  301.      *
  302.      * @return double 
  303.      */
  304.     public function getRotation({
  305.         if ($this->_isSupervisor{
  306.             return $this->getSharedComponent()->getRotation();
  307.         }
  308.         return $this->_rotation;
  309.     }
  310.     
  311.     /**
  312.      * Set Rotation
  313.      *
  314.      * @param double $pValue 
  315.      * @return PHPExcel_Style_Fill 
  316.      */
  317.     public function setRotation($pValue 0{
  318.         if ($this->_isSupervisor{
  319.             $styleArray $this->getStyleArray(array('rotation' => $pValue));
  320.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  321.         else {
  322.             $this->_rotation = $pValue;
  323.         }
  324.         return $this;
  325.     }
  326.     
  327.     /**
  328.      * Get Start Color
  329.      *
  330.      * @return PHPExcel_Style_Color 
  331.      */
  332.     public function getStartColor({
  333.         return $this->_startColor;
  334.     }
  335.     
  336.     /**
  337.      * Set Start Color
  338.      *
  339.      * @param     PHPExcel_Style_Color $pValue 
  340.      * @throws     Exception
  341.      * @return PHPExcel_Style_Fill 
  342.      */
  343.     public function setStartColor(PHPExcel_Style_Color $pValue null{
  344.         // make sure parameter is a real color and not a supervisor
  345.         $color $pValue->getIsSupervisor($pValue->getSharedComponent($pValue;
  346.         
  347.         if ($this->_isSupervisor{
  348.             $styleArray $this->getStartColor()->getStyleArray(array('argb' => $color->getARGB()));
  349.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  350.         else {
  351.             $this->_startColor = $color;
  352.         }
  353.         return $this;
  354.     }
  355.     
  356.     /**
  357.      * Get End Color
  358.      *
  359.      * @return PHPExcel_Style_Color 
  360.      */
  361.     public function getEndColor({
  362.         return $this->_endColor;
  363.     }
  364.     
  365.     /**
  366.      * Set End Color
  367.      *
  368.      * @param     PHPExcel_Style_Color $pValue 
  369.      * @throws     Exception
  370.      * @return PHPExcel_Style_Fill 
  371.      */
  372.     public function setEndColor(PHPExcel_Style_Color $pValue null{
  373.         // make sure parameter is a real color and not a supervisor
  374.         $color $pValue->getIsSupervisor($pValue->getSharedComponent($pValue;
  375.         
  376.         if ($this->_isSupervisor{
  377.             $styleArray $this->getEndColor()->getStyleArray(array('argb' => $color->getARGB()));
  378.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  379.         else {
  380.             $this->_endColor = $color;
  381.         }
  382.         return $this;
  383.     }
  384.  
  385.     /**
  386.      * Get hash code
  387.      *
  388.      * @return string    Hash code
  389.      */    
  390.     public function getHashCode({
  391.         if ($this->_isSupervisor{
  392.             return $this->getSharedComponent()->getHashCode();
  393.         }
  394.         return md5(
  395.               $this->getFillType()
  396.             . $this->getRotation()
  397.             . $this->getStartColor()->getHashCode()
  398.             . $this->getEndColor()->getHashCode()
  399.             . __CLASS__
  400.         );
  401.     }
  402.     
  403.     /**
  404.      * Hash index
  405.      *
  406.      * @var string 
  407.      */
  408.     private $_hashIndex;
  409.     
  410.     /**
  411.      * Get hash index
  412.      * 
  413.      * Note that this index may vary during script execution! Only reliable moment is
  414.      * while doing a write of a workbook and when changes are not allowed.
  415.      *
  416.      * @return string    Hash index
  417.      */
  418.     public function getHashIndex({
  419.         return $this->_hashIndex;
  420.     }
  421.     
  422.     /**
  423.      * Set hash index
  424.      * 
  425.      * Note that this index may vary during script execution! Only reliable moment is
  426.      * while doing a write of a workbook and when changes are not allowed.
  427.      *
  428.      * @param string    $value    Hash index
  429.      */
  430.     public function setHashIndex($value{
  431.         $this->_hashIndex = $value;
  432.     }
  433.         
  434.     /**
  435.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  436.      */
  437.     public function __clone({
  438.         $vars get_object_vars($this);
  439.         foreach ($vars as $key => $value{
  440.             if (is_object($value)) {
  441.                 $this->$key clone $value;
  442.             else {
  443.                 $this->$key $value;
  444.             }
  445.         }
  446.     }
  447. }

Documentation generated on Mon, 10 Aug 2009 08:04:16 +0200 by phpDocumentor 1.4.1