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

Source for file Borders.php

Documentation is available at Borders.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_Border */
  38. require_once PHPEXCEL_ROOT 'PHPExcel/Style/Border.php';
  39.  
  40. /** PHPExcel_IComparable */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/IComparable.php';
  42.  
  43.  
  44. /**
  45.  * PHPExcel_Style_Borders
  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_Borders implements PHPExcel_IComparable
  52. {
  53.     /* Diagonal directions */
  54.     const DIAGONAL_NONE        0;
  55.     const DIAGONAL_UP        1;
  56.     const DIAGONAL_DOWN        2;
  57.     
  58.     /**
  59.      * Left
  60.      *
  61.      * @var PHPExcel_Style_Border 
  62.      */
  63.     private $_left;
  64.     
  65.     /**
  66.      * Right
  67.      *
  68.      * @var PHPExcel_Style_Border 
  69.      */
  70.     private $_right;
  71.     
  72.     /**
  73.      * Top
  74.      *
  75.      * @var PHPExcel_Style_Border 
  76.      */
  77.     private $_top;
  78.     
  79.     /**
  80.      * Bottom
  81.      *
  82.      * @var PHPExcel_Style_Border 
  83.      */
  84.     private $_bottom;
  85.     
  86.     /**
  87.      * Diagonal
  88.      *
  89.      * @var PHPExcel_Style_Border 
  90.      */
  91.     private $_diagonal;
  92.     
  93.     /**
  94.      * DiagonalDirection
  95.      *
  96.      * @var int 
  97.      */
  98.     private $_diagonalDirection;
  99.     
  100.     /**
  101.      * All borders psedo-border. Only applies to supervisor.
  102.      *
  103.      * @var PHPExcel_Style_Border 
  104.      */
  105.     private $_allBorders;
  106.     
  107.     /**
  108.      * Outline psedo-border. Only applies to supervisor.
  109.      *
  110.      * @var PHPExcel_Style_Border 
  111.      */
  112.     private $_outline;
  113.     
  114.     /**
  115.      * Inside psedo-border. Only applies to supervisor.
  116.      *
  117.      * @var PHPExcel_Style_Border 
  118.      */
  119.     private $_inside;
  120.     
  121.     /**
  122.      * Vertical pseudo-border. Only applies to supervisor.
  123.      *
  124.      * @var PHPExcel_Style_Border 
  125.      */
  126.     private $_vertical;
  127.     
  128.     /**
  129.      * Horizontal pseudo-border. Only applies to supervisor.
  130.      *
  131.      * @var PHPExcel_Style_Border 
  132.      */
  133.     private $_horizontal;
  134.     
  135.     /**
  136.      * Parent Borders
  137.      *
  138.      * @var _parentPropertyName string
  139.      */
  140.     private $_parentPropertyName;
  141.  
  142.     /**
  143.      * Supervisor?
  144.      *
  145.      * @var boolean 
  146.      */
  147.     private $_isSupervisor;
  148.  
  149.     /**
  150.      * Parent. Only used for supervisor
  151.      *
  152.      * @var PHPExcel_Style 
  153.      */
  154.     private $_parent;
  155.  
  156.     /**
  157.      * Create a new PHPExcel_Style_Borders
  158.      */
  159.     public function __construct($isSupervisor false)
  160.     {
  161.         // Supervisor?
  162.         $this->_isSupervisor = $isSupervisor;
  163.  
  164.         // Initialise values
  165.         $this->_left                = new PHPExcel_Style_Border($isSupervisor);
  166.         $this->_right                = new PHPExcel_Style_Border($isSupervisor);
  167.         $this->_top                    = new PHPExcel_Style_Border($isSupervisor);
  168.         $this->_bottom                = new PHPExcel_Style_Border($isSupervisor);
  169.         $this->_diagonal            = new PHPExcel_Style_Border($isSupervisor);
  170.         $this->_diagonalDirection    = PHPExcel_Style_Borders::DIAGONAL_NONE;
  171.  
  172.         // Specially for supervisor
  173.         if ($isSupervisor{
  174.             // Initialize pseudo-borders
  175.             $this->_allBorders            = new PHPExcel_Style_Border(true);
  176.             $this->_outline                = new PHPExcel_Style_Border(true);
  177.             $this->_inside                = new PHPExcel_Style_Border(true);
  178.             $this->_vertical            = new PHPExcel_Style_Border(true);
  179.             $this->_horizontal            = new PHPExcel_Style_Border(true);
  180.  
  181.             // bind parent if we are a supervisor
  182.             $this->_left->bindParent($this'_left');
  183.             $this->_right->bindParent($this'_right');
  184.             $this->_top->bindParent($this'_top');
  185.             $this->_bottom->bindParent($this'_bottom');
  186.             $this->_diagonal->bindParent($this'_diagonal');
  187.             $this->_allBorders->bindParent($this'_allBorders');
  188.             $this->_outline->bindParent($this'_outline');
  189.             $this->_inside->bindParent($this'_inside');
  190.             $this->_vertical->bindParent($this'_vertical');
  191.             $this->_horizontal->bindParent($this'_horizontal');
  192.         }
  193.     }
  194.  
  195.     /**
  196.      * Bind parent. Only used for supervisor
  197.      *
  198.      * @param PHPExcel_Style $parent 
  199.      * @return PHPExcel_Style_Borders 
  200.      */
  201.     public function bindParent($parent)
  202.     {
  203.         $this->_parent = $parent;
  204.         return $this;
  205.     }
  206.  
  207.     /**
  208.      * Is this a supervisor or a real style component?
  209.      *
  210.      * @return boolean 
  211.      */
  212.     public function getIsSupervisor()
  213.     {
  214.         return $this->_isSupervisor;
  215.     }
  216.  
  217.     /**
  218.      * Get the shared style component for the currently active cell in currently active sheet.
  219.      * Only used for style supervisor
  220.      *
  221.      * @return PHPExcel_Style_Borders 
  222.      */
  223.     public function getSharedComponent()
  224.     {
  225.         return $this->_parent->getSharedComponent()->getBorders();
  226.     }
  227.  
  228.     /**
  229.      * Get the currently active sheet. Only used for supervisor
  230.      *
  231.      * @return PHPExcel_Worksheet 
  232.      */
  233.     public function getActiveSheet()
  234.     {
  235.         return $this->_parent->getActiveSheet();
  236.     }
  237.  
  238.     /**
  239.      * Get the currently active cell coordinate in currently active sheet.
  240.      * Only used for supervisor
  241.      *
  242.      * @return string E.g. 'A1'
  243.      */
  244.     public function getXSelectedCells()
  245.     {
  246.         return $this->getActiveSheet()->getXSelectedCells();
  247.     }
  248.  
  249.     /**
  250.      * Get the currently active cell coordinate in currently active sheet.
  251.      * Only used for supervisor
  252.      *
  253.      * @return string E.g. 'A1'
  254.      */
  255.     public function getXActiveCell()
  256.     {
  257.         return $this->getActiveSheet()->getXActiveCell();
  258.     }
  259.  
  260.     /**
  261.      * Build style array from subcomponents
  262.      *
  263.      * @param array $array 
  264.      * @return array 
  265.      */
  266.     public function getStyleArray($array)
  267.     {
  268.         return array('borders' => $array);
  269.     }
  270.  
  271.     /**
  272.      * Apply styles from array
  273.      * 
  274.      * <code>
  275.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  276.      *         array(
  277.      *             'bottom'     => array(
  278.      *                 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  279.      *                 'color' => array(
  280.      *                     'rgb' => '808080'
  281.      *                 )
  282.      *             ),
  283.      *             'top'     => array(
  284.      *                 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  285.      *                 'color' => array(
  286.      *                     'rgb' => '808080'
  287.      *                 )
  288.      *             )
  289.      *         )
  290.      * );
  291.      * </code>
  292.      * <code>
  293.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  294.      *         array(
  295.      *             'allborders' => array(
  296.      *                 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  297.      *                 'color' => array(
  298.      *                     'rgb' => '808080'
  299.      *                 )
  300.      *             )
  301.      *         )
  302.      * );
  303.      * </code>
  304.      * 
  305.      * @param    array    $pStyles    Array containing style information
  306.      * @throws    Exception
  307.      * @return PHPExcel_Style_Borders 
  308.      */
  309.     public function applyFromArray($pStyles null{
  310.         if (is_array($pStyles)) {
  311.             if ($this->_isSupervisor{
  312.                 $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  313.             else {
  314.                 if (array_key_exists('left'$pStyles)) {
  315.                     $this->getLeft()->applyFromArray($pStyles['left']);
  316.                 }
  317.                 if (array_key_exists('right'$pStyles)) {
  318.                     $this->getRight()->applyFromArray($pStyles['right']);
  319.                 }
  320.                 if (array_key_exists('top'$pStyles)) {
  321.                     $this->getTop()->applyFromArray($pStyles['top']);
  322.                 }
  323.                 if (array_key_exists('bottom'$pStyles)) {
  324.                     $this->getBottom()->applyFromArray($pStyles['bottom']);
  325.                 }
  326.                 if (array_key_exists('diagonal'$pStyles)) {
  327.                     $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
  328.                 }
  329.                 if (array_key_exists('diagonaldirection'$pStyles)) {
  330.                     $this->setDiagonalDirection($pStyles['diagonaldirection']);
  331.                 }
  332.             }
  333.         else {
  334.             throw new Exception("Invalid style array passed.");
  335.         }
  336.         return $this;
  337.     }
  338.     
  339.     /**
  340.      * Get Left
  341.      *
  342.      * @return PHPExcel_Style_Border 
  343.      */
  344.     public function getLeft({
  345.         return $this->_left;
  346.     }
  347.     
  348.     /**
  349.      * Get Right
  350.      *
  351.      * @return PHPExcel_Style_Border 
  352.      */
  353.     public function getRight({
  354.         return $this->_right;
  355.     }
  356.        
  357.     /**
  358.      * Get Top
  359.      *
  360.      * @return PHPExcel_Style_Border 
  361.      */
  362.     public function getTop({
  363.         return $this->_top;
  364.     }
  365.     
  366.     /**
  367.      * Get Bottom
  368.      *
  369.      * @return PHPExcel_Style_Border 
  370.      */
  371.     public function getBottom({
  372.         return $this->_bottom;
  373.     }
  374.  
  375.     /**
  376.      * Get Diagonal
  377.      *
  378.      * @return PHPExcel_Style_Border 
  379.      */
  380.     public function getDiagonal({
  381.         return $this->_diagonal;
  382.     }
  383.     
  384.     /**
  385.      * Get AllBorders (pseudo-border). Only applies to supervisor.
  386.      *
  387.      * @return PHPExcel_Style_Border 
  388.      * @throws Exception
  389.      */
  390.     public function getAllBorders({
  391.         if (!$this->_isSupervisor{
  392.             throw new Exception('Can only get pseudo-border for supervisor.');
  393.         }
  394.         return $this->_allBorders;
  395.     }
  396.     
  397.     /**
  398.      * Get Outline (pseudo-border). Only applies to supervisor.
  399.      *
  400.      * @return boolean 
  401.      * @throws Exception
  402.      */
  403.     public function getOutline({
  404.         if (!$this->_isSupervisor{
  405.             throw new Exception('Can only get pseudo-border for supervisor.');
  406.         }
  407.         return $this->_outline;
  408.     }
  409.     
  410.     /**
  411.      * Get Inside (pseudo-border). Only applies to supervisor.
  412.      *
  413.      * @return boolean 
  414.      * @throws Exception
  415.      */
  416.     public function getInside({
  417.         if (!$this->_isSupervisor{
  418.             throw new Exception('Can only get pseudo-border for supervisor.');
  419.         }
  420.         return $this->_inside;
  421.     }
  422.     
  423.     /**
  424.      * Get Vertical (pseudo-border). Only applies to supervisor.
  425.      *
  426.      * @return PHPExcel_Style_Border 
  427.      * @throws Exception
  428.      */
  429.     public function getVertical({
  430.         if (!$this->_isSupervisor{
  431.             throw new Exception('Can only get pseudo-border for supervisor.');
  432.         }
  433.         return $this->_vertical;
  434.     }
  435.     
  436.     /**
  437.      * Get Horizontal (pseudo-border). Only applies to supervisor.
  438.      *
  439.      * @return PHPExcel_Style_Border 
  440.      * @throws Exception
  441.      */
  442.     public function getHorizontal({
  443.         if (!$this->_isSupervisor{
  444.             throw new Exception('Can only get pseudo-border for supervisor.');
  445.         }
  446.         return $this->_horizontal;
  447.     }
  448.     
  449.     /**
  450.      * Get DiagonalDirection
  451.      *
  452.      * @return int 
  453.      */
  454.     public function getDiagonalDirection({
  455.         if ($this->_isSupervisor{
  456.             return $this->getSharedComponent()->getDiagonalDirection();
  457.         }
  458.         return $this->_diagonalDirection;
  459.     }
  460.     
  461.     /**
  462.      * Set DiagonalDirection
  463.      *
  464.      * @param int $pValue 
  465.      * @return PHPExcel_Style_Borders 
  466.      */
  467.     public function setDiagonalDirection($pValue PHPExcel_Style_Borders::DIAGONAL_NONE{
  468.         if ($pValue == ''{
  469.             $pValue PHPExcel_Style_Borders::DIAGONAL_NONE;
  470.         }
  471.         if ($this->_isSupervisor{
  472.             $styleArray $this->getStyleArray(array('diagonaldirection' => $pValue));
  473.             $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  474.         else {
  475.             $this->_diagonalDirection = $pValue;
  476.         }
  477.         return $this;
  478.     }
  479.     
  480.     /**
  481.      * Get hash code
  482.      *
  483.      * @return string    Hash code
  484.      */    
  485.     public function getHashCode({
  486.         if ($this->_isSupervisor{
  487.             return $this->getSharedComponent()->getHashcode();
  488.         }
  489.         return md5(
  490.               $this->getLeft()->getHashCode()
  491.             . $this->getRight()->getHashCode()
  492.             . $this->getTop()->getHashCode()
  493.             . $this->getBottom()->getHashCode()
  494.             . $this->getDiagonal()->getHashCode()
  495.             . $this->getDiagonalDirection()
  496.             . __CLASS__
  497.         );
  498.     }
  499.     
  500.     /**
  501.      * Hash index
  502.      *
  503.      * @var string 
  504.      */
  505.     private $_hashIndex;
  506.     
  507.     /**
  508.      * Get hash index
  509.      * 
  510.      * Note that this index may vary during script execution! Only reliable moment is
  511.      * while doing a write of a workbook and when changes are not allowed.
  512.      *
  513.      * @return string    Hash index
  514.      */
  515.     public function getHashIndex({
  516.         return $this->_hashIndex;
  517.     }
  518.     
  519.     /**
  520.      * Set hash index
  521.      * 
  522.      * Note that this index may vary during script execution! Only reliable moment is
  523.      * while doing a write of a workbook and when changes are not allowed.
  524.      *
  525.      * @param string    $value    Hash index
  526.      */
  527.     public function setHashIndex($value{
  528.         $this->_hashIndex = $value;
  529.     }
  530.     
  531.     /**
  532.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  533.      */
  534.     public function __clone({
  535.         $vars get_object_vars($this);
  536.         foreach ($vars as $key => $value{
  537.             if (is_object($value)) {
  538.                 $this->$key clone $value;
  539.             else {
  540.                 $this->$key $value;
  541.             }
  542.         }
  543.     }
  544. }

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