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.3c, 2010-06-01
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Style_Borders
  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_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.             }
  319.         else {
  320.             throw new Exception("Invalid style array passed.");
  321.         }
  322.         return $this;
  323.     }
  324.  
  325.     /**
  326.      * Get Left
  327.      *
  328.      * @return PHPExcel_Style_Border 
  329.      */
  330.     public function getLeft({
  331.         return $this->_left;
  332.     }
  333.  
  334.     /**
  335.      * Get Right
  336.      *
  337.      * @return PHPExcel_Style_Border 
  338.      */
  339.     public function getRight({
  340.         return $this->_right;
  341.     }
  342.  
  343.     /**
  344.      * Get Top
  345.      *
  346.      * @return PHPExcel_Style_Border 
  347.      */
  348.     public function getTop({
  349.         return $this->_top;
  350.     }
  351.  
  352.     /**
  353.      * Get Bottom
  354.      *
  355.      * @return PHPExcel_Style_Border 
  356.      */
  357.     public function getBottom({
  358.         return $this->_bottom;
  359.     }
  360.  
  361.     /**
  362.      * Get Diagonal
  363.      *
  364.      * @return PHPExcel_Style_Border 
  365.      */
  366.     public function getDiagonal({
  367.         return $this->_diagonal;
  368.     }
  369.  
  370.     /**
  371.      * Get AllBorders (pseudo-border). Only applies to supervisor.
  372.      *
  373.      * @return PHPExcel_Style_Border 
  374.      * @throws Exception
  375.      */
  376.     public function getAllBorders({
  377.         if (!$this->_isSupervisor{
  378.             throw new Exception('Can only get pseudo-border for supervisor.');
  379.         }
  380.         return $this->_allBorders;
  381.     }
  382.  
  383.     /**
  384.      * Get Outline (pseudo-border). Only applies to supervisor.
  385.      *
  386.      * @return boolean 
  387.      * @throws Exception
  388.      */
  389.     public function getOutline({
  390.         if (!$this->_isSupervisor{
  391.             throw new Exception('Can only get pseudo-border for supervisor.');
  392.         }
  393.         return $this->_outline;
  394.     }
  395.  
  396.     /**
  397.      * Get Inside (pseudo-border). Only applies to supervisor.
  398.      *
  399.      * @return boolean 
  400.      * @throws Exception
  401.      */
  402.     public function getInside({
  403.         if (!$this->_isSupervisor{
  404.             throw new Exception('Can only get pseudo-border for supervisor.');
  405.         }
  406.         return $this->_inside;
  407.     }
  408.  
  409.     /**
  410.      * Get Vertical (pseudo-border). Only applies to supervisor.
  411.      *
  412.      * @return PHPExcel_Style_Border 
  413.      * @throws Exception
  414.      */
  415.     public function getVertical({
  416.         if (!$this->_isSupervisor{
  417.             throw new Exception('Can only get pseudo-border for supervisor.');
  418.         }
  419.         return $this->_vertical;
  420.     }
  421.  
  422.     /**
  423.      * Get Horizontal (pseudo-border). Only applies to supervisor.
  424.      *
  425.      * @return PHPExcel_Style_Border 
  426.      * @throws Exception
  427.      */
  428.     public function getHorizontal({
  429.         if (!$this->_isSupervisor{
  430.             throw new Exception('Can only get pseudo-border for supervisor.');
  431.         }
  432.         return $this->_horizontal;
  433.     }
  434.  
  435.     /**
  436.      * Get DiagonalDirection
  437.      *
  438.      * @return int 
  439.      */
  440.     public function getDiagonalDirection({
  441.         if ($this->_isSupervisor{
  442.             return $this->getSharedComponent()->getDiagonalDirection();
  443.         }
  444.         return $this->_diagonalDirection;
  445.     }
  446.  
  447.     /**
  448.      * Set DiagonalDirection
  449.      *
  450.      * @param int $pValue 
  451.      * @return PHPExcel_Style_Borders 
  452.      */
  453.     public function setDiagonalDirection($pValue PHPExcel_Style_Borders::DIAGONAL_NONE{
  454.         if ($pValue == ''{
  455.             $pValue PHPExcel_Style_Borders::DIAGONAL_NONE;
  456.         }
  457.         if ($this->_isSupervisor{
  458.             $styleArray $this->getStyleArray(array('diagonaldirection' => $pValue));
  459.             $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  460.         else {
  461.             $this->_diagonalDirection $pValue;
  462.         }
  463.         return $this;
  464.     }
  465.  
  466.     /**
  467.      * Get hash code
  468.      *
  469.      * @return string    Hash code
  470.      */
  471.     public function getHashCode({
  472.         if ($this->_isSupervisor{
  473.             return $this->getSharedComponent()->getHashcode();
  474.         }
  475.         return md5(
  476.               $this->getLeft()->getHashCode()
  477.             . $this->getRight()->getHashCode()
  478.             . $this->getTop()->getHashCode()
  479.             . $this->getBottom()->getHashCode()
  480.             . $this->getDiagonal()->getHashCode()
  481.             . $this->getDiagonalDirection()
  482.             . __CLASS__
  483.         );
  484.     }
  485.  
  486.     /**
  487.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  488.      */
  489.     public function __clone({
  490.         $vars get_object_vars($this);
  491.         foreach ($vars as $key => $value{
  492.             if (is_object($value)) {
  493.                 $this->$key clone $value;
  494.             else {
  495.                 $this->$key $value;
  496.             }
  497.         }
  498.     }
  499. }

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