Completed
Push — develop ( e6c95b...1c5db4 )
by Adrien
32:25
created

Borders::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 2

Importance

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