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

Documentation generated on Tue, 01 Jun 2010 17:03:47 +0200 by phpDocumentor 1.4.3