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

Source for file Border.php

Documentation is available at Border.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_Border
  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_Border implements PHPExcel_IComparable
  52. {
  53.     /* Border style */
  54.     const BORDER_NONE                'none';
  55.     const BORDER_DASHDOT            'dashDot';
  56.     const BORDER_DASHDOTDOT            'dashDotDot';
  57.     const BORDER_DASHED                'dashed';
  58.     const BORDER_DOTTED                'dotted';
  59.     const BORDER_DOUBLE                'double';
  60.     const BORDER_HAIR                'hair';
  61.     const BORDER_MEDIUM                'medium';
  62.     const BORDER_MEDIUMDASHDOT        'mediumDashDot';
  63.     const BORDER_MEDIUMDASHDOTDOT    'mediumDashDotDot';
  64.     const BORDER_MEDIUMDASHED        'mediumDashed';
  65.     const BORDER_SLANTDASHDOT        'slantDashDot';
  66.     const BORDER_THICK                'thick';
  67.     const BORDER_THIN                'thin';
  68.     
  69.     /**
  70.      * Border style
  71.      *
  72.      * @var string 
  73.      */
  74.     private $_borderStyle;
  75.     
  76.     /**
  77.      * Border color
  78.      * 
  79.      * @var PHPExcel_Style_Color 
  80.      */
  81.     private $_color;
  82.  
  83.     /**
  84.      * Supervisor?
  85.      *
  86.      * @var boolean 
  87.      */
  88.     private $_isSupervisor;
  89.  
  90.     /**
  91.      * Parent. Only used for supervisor
  92.      *
  93.      * @var PHPExcel_Style_Borders 
  94.      */
  95.     private $_parent;
  96.  
  97.     /**
  98.      * Parent property name
  99.      *
  100.      * @var string 
  101.      */
  102.     private $_parentPropertyName;
  103.  
  104.     /**
  105.      * Create a new PHPExcel_Style_Border
  106.      */
  107.     public function __construct($isSupervisor false)
  108.     {
  109.         // Supervisor?
  110.         $this->_isSupervisor = $isSupervisor;
  111.  
  112.         // Initialise values
  113.         $this->_borderStyle            = PHPExcel_Style_Border::BORDER_NONE;
  114.         $this->_color            = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK$isSupervisor);
  115.  
  116.         // bind parent if we are a supervisor
  117.         if ($isSupervisor{
  118.             $this->_color->bindParent($this'_color');
  119.         }
  120.     }
  121.  
  122.     /**
  123.      * Bind parent. Only used for supervisor
  124.      *
  125.      * @param PHPExcel_Style_Borders $parent 
  126.      * @param string $parentPropertyName 
  127.      * @return PHPExcel_Style_Border 
  128.      */
  129.     public function bindParent($parent$parentPropertyName)
  130.     {
  131.         $this->_parent = $parent;
  132.         $this->_parentPropertyName = $parentPropertyName;
  133.         return $this;
  134.     }
  135.  
  136.     /**
  137.      * Is this a supervisor or a real style component?
  138.      *
  139.      * @return boolean 
  140.      */
  141.     public function getIsSupervisor()
  142.     {
  143.         return $this->_isSupervisor;
  144.     }
  145.  
  146.     /**
  147.      * Get the shared style component for the currently active cell in currently active sheet.
  148.      * Only used for style supervisor
  149.      *
  150.      * @return PHPExcel_Style_Border 
  151.      * @throws Exception
  152.      */
  153.     public function getSharedComponent()
  154.     {
  155.         switch ($this->_parentPropertyName{
  156.             case '_allBorders':
  157.             case '_horizontal':
  158.             case '_inside':
  159.             case '_outline':
  160.             case '_vertical':
  161.                 throw new Exception('Cannot get shared component for a pseudo-border.');
  162.                 break;
  163.  
  164.             case '_bottom':
  165.                 return $this->_parent->getSharedComponent()->getBottom();
  166.                 break;
  167.  
  168.             case '_diagonal':
  169.                 return $this->_parent->getSharedComponent()->getDiagonal();
  170.                 break;
  171.  
  172.             case '_left':
  173.                 return $this->_parent->getSharedComponent()->getLeft();
  174.                 break;
  175.  
  176.             case '_right':
  177.                 return $this->_parent->getSharedComponent()->getRight();
  178.                 break;
  179.  
  180.             case '_top':
  181.                 return $this->_parent->getSharedComponent()->getTop();
  182.                 break;
  183.  
  184.         }
  185.     }
  186.  
  187.     /**
  188.      * Get the currently active sheet. Only used for supervisor
  189.      *
  190.      * @return PHPExcel_Worksheet 
  191.      */
  192.     public function getActiveSheet()
  193.     {
  194.         return $this->_parent->getActiveSheet();
  195.     }
  196.  
  197.     /**
  198.      * Get the currently active cell coordinate in currently active sheet.
  199.      * Only used for supervisor
  200.      *
  201.      * @return string E.g. 'A1'
  202.      */
  203.     public function getXSelectedCells()
  204.     {
  205.         return $this->getActiveSheet()->getXSelectedCells();
  206.     }
  207.  
  208.     /**
  209.      * Get the currently active cell coordinate in currently active sheet.
  210.      * Only used for supervisor
  211.      *
  212.      * @return string E.g. 'A1'
  213.      */
  214.     public function getXActiveCell()
  215.     {
  216.         return $this->getActiveSheet()->getXActiveCell();
  217.     }
  218.  
  219.     /**
  220.      * Build style array from subcomponents
  221.      *
  222.      * @param array $array 
  223.      * @return array 
  224.      */
  225.     public function getStyleArray($array)
  226.     {
  227.         switch ($this->_parentPropertyName{
  228.         case '_allBorders':
  229.             $key 'allborders';
  230.             break;
  231.  
  232.         case '_bottom':
  233.             $key 'bottom';
  234.             break;
  235.  
  236.         case '_diagonal':
  237.             $key 'diagonal';
  238.             break;
  239.  
  240.         case '_horizontal':
  241.             $key 'horizontal';
  242.             break;
  243.  
  244.         case '_inside':
  245.             $key 'inside';
  246.             break;
  247.  
  248.         case '_left':
  249.             $key 'left';
  250.             break;
  251.  
  252.         case '_outline':
  253.             $key 'outline';
  254.             break;
  255.  
  256.         case '_right':
  257.             $key 'right';
  258.             break;
  259.  
  260.         case '_top':
  261.             $key 'top';
  262.             break;
  263.  
  264.         case '_vertical':
  265.             $key 'vertical';
  266.             break;
  267.         }
  268.         return $this->_parent->getStyleArray(array($key => $array));
  269.     }
  270.  
  271.     /**
  272.      * Apply styles from array
  273.      * 
  274.      * <code>
  275.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
  276.      *         array(
  277.      *             'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  278.      *             'color' => array(
  279.      *                 'rgb' => '808080'
  280.      *             )
  281.      *         )
  282.      * );
  283.      * </code>
  284.      * 
  285.      * @param    array    $pStyles    Array containing style information
  286.      * @throws    Exception
  287.      * @return PHPExcel_Style_Border 
  288.      */
  289.     public function applyFromArray($pStyles null{
  290.         if (is_array($pStyles)) {
  291.             if ($this->_isSupervisor{
  292.                 $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  293.             else {
  294.                 if (array_key_exists('style'$pStyles)) {
  295.                     $this->setBorderStyle($pStyles['style']);
  296.                 }
  297.                 if (array_key_exists('color'$pStyles)) {
  298.                     $this->getColor()->applyFromArray($pStyles['color']);
  299.                 }
  300.             }
  301.         else {
  302.             throw new Exception("Invalid style array passed.");
  303.         }
  304.         return $this;
  305.     }
  306.     
  307.     /**
  308.      * Get Border style
  309.      *
  310.      * @return string 
  311.      */
  312.     public function getBorderStyle({
  313.         if ($this->_isSupervisor{
  314.             return $this->getSharedComponent()->getBorderStyle();
  315.         }
  316.         return $this->_borderStyle;
  317.     }
  318.     
  319.     /**
  320.      * Set Border style
  321.      *
  322.      * @param string $pValue 
  323.      * @return PHPExcel_Style_Border 
  324.      */
  325.     public function setBorderStyle($pValue PHPExcel_Style_Border::BORDER_NONE{
  326.     
  327.         if ($pValue == ''{
  328.             $pValue PHPExcel_Style_Border::BORDER_NONE;
  329.         }
  330.         if ($this->_isSupervisor{
  331.             $styleArray $this->getStyleArray(array('style' => $pValue));
  332.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  333.         else {
  334.             $this->_borderStyle = $pValue;
  335.         }
  336.         return $this;
  337.     }
  338.     
  339.     /**
  340.      * Get Border Color
  341.      *
  342.      * @return PHPExcel_Style_Color 
  343.      */
  344.     public function getColor({
  345.         return $this->_color;
  346.     }
  347.     
  348.     /**
  349.      * Set Border Color
  350.      *
  351.      * @param     PHPExcel_Style_Color $pValue 
  352.      * @throws     Exception
  353.      * @return PHPExcel_Style_Border 
  354.      */
  355.     public function setColor(PHPExcel_Style_Color $pValue null{
  356.         // make sure parameter is a real color and not a supervisor
  357.         $color $pValue->getIsSupervisor($pValue->getSharedComponent($pValue;
  358.         
  359.         if ($this->_isSupervisor{
  360.             $styleArray $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
  361.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  362.         else {
  363.             $this->_color = $color;
  364.         }
  365.         return $this;
  366.     }
  367.     
  368.     /**
  369.      * Get hash code
  370.      *
  371.      * @return string    Hash code
  372.      */    
  373.     public function getHashCode({
  374.         if ($this->_isSupervisor{
  375.             return $this->getSharedComponent()->getHashCode();
  376.         }
  377.         return md5(
  378.               $this->_borderStyle
  379.             . $this->_color->getHashCode()
  380.             . __CLASS__
  381.         );
  382.     }
  383.     
  384.     /**
  385.      * Hash index
  386.      *
  387.      * @var string 
  388.      */
  389.     private $_hashIndex;
  390.     
  391.     /**
  392.      * Get hash index
  393.      * 
  394.      * Note that this index may vary during script execution! Only reliable moment is
  395.      * while doing a write of a workbook and when changes are not allowed.
  396.      *
  397.      * @return string    Hash index
  398.      */
  399.     public function getHashIndex({
  400.         return $this->_hashIndex;
  401.     }
  402.     
  403.     /**
  404.      * Set hash index
  405.      * 
  406.      * Note that this index may vary during script execution! Only reliable moment is
  407.      * while doing a write of a workbook and when changes are not allowed.
  408.      *
  409.      * @param string    $value    Hash index
  410.      */
  411.     public function setHashIndex($value{
  412.         $this->_hashIndex = $value;
  413.     }
  414.     
  415.     /**
  416.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  417.      */
  418.     public function __clone({
  419.         $vars get_object_vars($this);
  420.         foreach ($vars as $key => $value{
  421.             if (is_object($value)) {
  422.                 $this->$key clone $value;
  423.             else {
  424.                 $this->$key $value;
  425.             }
  426.         }
  427.     }
  428. }

Documentation generated on Mon, 10 Aug 2009 08:02:15 +0200 by phpDocumentor 1.4.1