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

Documentation generated on Mon, 11 Jan 2010 08:06:43 +0100 by phpDocumentor 1.4.1