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

Documentation generated on Tue, 01 Jun 2010 17:02:12 +0200 by phpDocumentor 1.4.3