1<?php
2
3/**
4 * PHPExcel_Style_Borders
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 * @category   PHPExcel
23 * @package    PHPExcel_Style
24 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26 * @version    ##VERSION##, ##DATE##
27 */
28class PHPExcel_Style_Borders extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
29{
30    /* Diagonal directions */
31    const DIAGONAL_NONE = 0;
32    const DIAGONAL_UP   = 1;
33    const DIAGONAL_DOWN = 2;
34    const DIAGONAL_BOTH = 3;
35
36    /**
37     * Left
38     *
39     * @var PHPExcel_Style_Border
40     */
41    protected $left;
42
43    /**
44     * Right
45     *
46     * @var PHPExcel_Style_Border
47     */
48    protected $right;
49
50    /**
51     * Top
52     *
53     * @var PHPExcel_Style_Border
54     */
55    protected $top;
56
57    /**
58     * Bottom
59     *
60     * @var PHPExcel_Style_Border
61     */
62    protected $bottom;
63
64    /**
65     * Diagonal
66     *
67     * @var PHPExcel_Style_Border
68     */
69    protected $diagonal;
70
71    /**
72     * DiagonalDirection
73     *
74     * @var int
75     */
76    protected $diagonalDirection;
77
78    /**
79     * All borders psedo-border. Only applies to supervisor.
80     *
81     * @var PHPExcel_Style_Border
82     */
83    protected $allBorders;
84
85    /**
86     * Outline psedo-border. Only applies to supervisor.
87     *
88     * @var PHPExcel_Style_Border
89     */
90    protected $outline;
91
92    /**
93     * Inside psedo-border. Only applies to supervisor.
94     *
95     * @var PHPExcel_Style_Border
96     */
97    protected $inside;
98
99    /**
100     * Vertical pseudo-border. Only applies to supervisor.
101     *
102     * @var PHPExcel_Style_Border
103     */
104    protected $vertical;
105
106    /**
107     * Horizontal pseudo-border. Only applies to supervisor.
108     *
109     * @var PHPExcel_Style_Border
110     */
111    protected $horizontal;
112
113    /**
114     * Create a new PHPExcel_Style_Borders
115     *
116     * @param    boolean    $isSupervisor    Flag indicating if this is a supervisor or not
117     *                                    Leave this value at default unless you understand exactly what
118     *                                        its ramifications are
119     * @param    boolean    $isConditional    Flag indicating if this is a conditional style or not
120     *                                    Leave this value at default unless you understand exactly what
121     *                                        its ramifications are
122     */
123    public function __construct($isSupervisor = false, $isConditional = false)
124    {
125        // Supervisor?
126        parent::__construct($isSupervisor);
127
128        // Initialise values
129        $this->left = new PHPExcel_Style_Border($isSupervisor, $isConditional);
130        $this->right = new PHPExcel_Style_Border($isSupervisor, $isConditional);
131        $this->top = new PHPExcel_Style_Border($isSupervisor, $isConditional);
132        $this->bottom = new PHPExcel_Style_Border($isSupervisor, $isConditional);
133        $this->diagonal = new PHPExcel_Style_Border($isSupervisor, $isConditional);
134        $this->diagonalDirection = PHPExcel_Style_Borders::DIAGONAL_NONE;
135
136        // Specially for supervisor
137        if ($isSupervisor) {
138            // Initialize pseudo-borders
139            $this->allBorders = new PHPExcel_Style_Border(true);
140            $this->outline = new PHPExcel_Style_Border(true);
141            $this->inside = new PHPExcel_Style_Border(true);
142            $this->vertical = new PHPExcel_Style_Border(true);
143            $this->horizontal = new PHPExcel_Style_Border(true);
144
145            // bind parent if we are a supervisor
146            $this->left->bindParent($this, 'left');
147            $this->right->bindParent($this, 'right');
148            $this->top->bindParent($this, 'top');
149            $this->bottom->bindParent($this, 'bottom');
150            $this->diagonal->bindParent($this, 'diagonal');
151            $this->allBorders->bindParent($this, 'allBorders');
152            $this->outline->bindParent($this, 'outline');
153            $this->inside->bindParent($this, 'inside');
154            $this->vertical->bindParent($this, 'vertical');
155            $this->horizontal->bindParent($this, 'horizontal');
156        }
157    }
158
159    /**
160     * Get the shared style component for the currently active cell in currently active sheet.
161     * Only used for style supervisor
162     *
163     * @return PHPExcel_Style_Borders
164     */
165    public function getSharedComponent()
166    {
167        return $this->parent->getSharedComponent()->getBorders();
168    }
169
170    /**
171     * Build style array from subcomponents
172     *
173     * @param array $array
174     * @return array
175     */
176    public function getStyleArray($array)
177    {
178        return array('borders' => $array);
179    }
180
181    /**
182     * Apply styles from array
183     *
184     * <code>
185     * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
186     *         array(
187     *             'bottom'     => array(
188     *                 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
189     *                 'color' => array(
190     *                     'rgb' => '808080'
191     *                 )
192     *             ),
193     *             'top'     => array(
194     *                 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
195     *                 'color' => array(
196     *                     'rgb' => '808080'
197     *                 )
198     *             )
199     *         )
200     * );
201     * </code>
202     * <code>
203     * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
204     *         array(
205     *             'allborders' => array(
206     *                 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
207     *                 'color' => array(
208     *                     'rgb' => '808080'
209     *                 )
210     *             )
211     *         )
212     * );
213     * </code>
214     *
215     * @param    array    $pStyles    Array containing style information
216     * @throws    PHPExcel_Exception
217     * @return PHPExcel_Style_Borders
218     */
219    public function applyFromArray($pStyles = null)
220    {
221        if (is_array($pStyles)) {
222            if ($this->isSupervisor) {
223                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
224            } else {
225                if (array_key_exists('left', $pStyles)) {
226                    $this->getLeft()->applyFromArray($pStyles['left']);
227                }
228                if (array_key_exists('right', $pStyles)) {
229                    $this->getRight()->applyFromArray($pStyles['right']);
230                }
231                if (array_key_exists('top', $pStyles)) {
232                    $this->getTop()->applyFromArray($pStyles['top']);
233                }
234                if (array_key_exists('bottom', $pStyles)) {
235                    $this->getBottom()->applyFromArray($pStyles['bottom']);
236                }
237                if (array_key_exists('diagonal', $pStyles)) {
238                    $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
239                }
240                if (array_key_exists('diagonaldirection', $pStyles)) {
241                    $this->setDiagonalDirection($pStyles['diagonaldirection']);
242                }
243                if (array_key_exists('allborders', $pStyles)) {
244                    $this->getLeft()->applyFromArray($pStyles['allborders']);
245                    $this->getRight()->applyFromArray($pStyles['allborders']);
246                    $this->getTop()->applyFromArray($pStyles['allborders']);
247                    $this->getBottom()->applyFromArray($pStyles['allborders']);
248                }
249            }
250        } else {
251            throw new PHPExcel_Exception("Invalid style array passed.");
252        }
253        return $this;
254    }
255
256    /**
257     * Get Left
258     *
259     * @return PHPExcel_Style_Border
260     */
261    public function getLeft()
262    {
263        return $this->left;
264    }
265
266    /**
267     * Get Right
268     *
269     * @return PHPExcel_Style_Border
270     */
271    public function getRight()
272    {
273        return $this->right;
274    }
275
276    /**
277     * Get Top
278     *
279     * @return PHPExcel_Style_Border
280     */
281    public function getTop()
282    {
283        return $this->top;
284    }
285
286    /**
287     * Get Bottom
288     *
289     * @return PHPExcel_Style_Border
290     */
291    public function getBottom()
292    {
293        return $this->bottom;
294    }
295
296    /**
297     * Get Diagonal
298     *
299     * @return PHPExcel_Style_Border
300     */
301    public function getDiagonal()
302    {
303        return $this->diagonal;
304    }
305
306    /**
307     * Get AllBorders (pseudo-border). Only applies to supervisor.
308     *
309     * @return PHPExcel_Style_Border
310     * @throws PHPExcel_Exception
311     */
312    public function getAllBorders()
313    {
314        if (!$this->isSupervisor) {
315            throw new PHPExcel_Exception('Can only get pseudo-border for supervisor.');
316        }
317        return $this->allBorders;
318    }
319
320    /**
321     * Get Outline (pseudo-border). Only applies to supervisor.
322     *
323     * @return boolean
324     * @throws PHPExcel_Exception
325     */
326    public function getOutline()
327    {
328        if (!$this->isSupervisor) {
329            throw new PHPExcel_Exception('Can only get pseudo-border for supervisor.');
330        }
331        return $this->outline;
332    }
333
334    /**
335     * Get Inside (pseudo-border). Only applies to supervisor.
336     *
337     * @return boolean
338     * @throws PHPExcel_Exception
339     */
340    public function getInside()
341    {
342        if (!$this->isSupervisor) {
343            throw new PHPExcel_Exception('Can only get pseudo-border for supervisor.');
344        }
345        return $this->inside;
346    }
347
348    /**
349     * Get Vertical (pseudo-border). Only applies to supervisor.
350     *
351     * @return PHPExcel_Style_Border
352     * @throws PHPExcel_Exception
353     */
354    public function getVertical()
355    {
356        if (!$this->isSupervisor) {
357            throw new PHPExcel_Exception('Can only get pseudo-border for supervisor.');
358        }
359        return $this->vertical;
360    }
361
362    /**
363     * Get Horizontal (pseudo-border). Only applies to supervisor.
364     *
365     * @return PHPExcel_Style_Border
366     * @throws PHPExcel_Exception
367     */
368    public function getHorizontal()
369    {
370        if (!$this->isSupervisor) {
371            throw new PHPExcel_Exception('Can only get pseudo-border for supervisor.');
372        }
373        return $this->horizontal;
374    }
375
376    /**
377     * Get DiagonalDirection
378     *
379     * @return int
380     */
381    public function getDiagonalDirection()
382    {
383        if ($this->isSupervisor) {
384            return $this->getSharedComponent()->getDiagonalDirection();
385        }
386        return $this->diagonalDirection;
387    }
388
389    /**
390     * Set DiagonalDirection
391     *
392     * @param int $pValue
393     * @return PHPExcel_Style_Borders
394     */
395    public function setDiagonalDirection($pValue = PHPExcel_Style_Borders::DIAGONAL_NONE)
396    {
397        if ($pValue == '') {
398            $pValue = PHPExcel_Style_Borders::DIAGONAL_NONE;
399        }
400        if ($this->isSupervisor) {
401            $styleArray = $this->getStyleArray(array('diagonaldirection' => $pValue));
402            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
403        } else {
404            $this->diagonalDirection = $pValue;
405        }
406        return $this;
407    }
408
409    /**
410     * Get hash code
411     *
412     * @return string    Hash code
413     */
414    public function getHashCode()
415    {
416        if ($this->isSupervisor) {
417            return $this->getSharedComponent()->getHashcode();
418        }
419        return md5(
420            $this->getLeft()->getHashCode() .
421            $this->getRight()->getHashCode() .
422            $this->getTop()->getHashCode() .
423            $this->getBottom()->getHashCode() .
424            $this->getDiagonal()->getHashCode() .
425            $this->getDiagonalDirection() .
426            __CLASS__
427        );
428    }
429}
430