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

Documentation generated on Sun, 27 Feb 2011 16:28:40 -0800 by phpDocumentor 1.4.3