Passed
Pull Request — master (#4426)
by
unknown
13:11
created

Borders::getTop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
7
class Borders extends Supervisor
8
{
9
    // Diagonal directions
10
    const DIAGONAL_NONE = 0;
11
    const DIAGONAL_UP = 1;
12
    const DIAGONAL_DOWN = 2;
13
    const DIAGONAL_BOTH = 3;
14
15
    /**
16
     * Left.
17
     */
18
    protected Border $left;
19
20
    /**
21
     * Right.
22
     */
23
    protected Border $right;
24
25
    /**
26
     * Top.
27
     */
28
    protected Border $top;
29
30
    /**
31
     * Bottom.
32
     */
33
    protected Border $bottom;
34
35
    /**
36
     * Diagonal.
37
     */
38
    protected Border $diagonal;
39
40
    /**
41
     * DiagonalDirection.
42
     */
43
    protected int $diagonalDirection;
44
45
    /**
46
     * All borders pseudo-border. Only applies to supervisor.
47
     */
48
    protected Border $allBorders;
49
50
    /**
51
     * Outline pseudo-border. Only applies to supervisor.
52
     */
53
    protected Border $outline;
54
55
    /**
56
     * Inside pseudo-border. Only applies to supervisor.
57
     */
58
    protected Border $inside;
59
60
    /**
61
     * Vertical pseudo-border. Only applies to supervisor.
62
     */
63
    protected Border $vertical;
64
65
    /**
66
     * Horizontal pseudo-border. Only applies to supervisor.
67
     */
68
    protected Border $horizontal;
69
70
    /**
71
     * Create a new Borders.
72
     *
73
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
74
     *                                    Leave this value at default unless you understand exactly what
75
     *                                        its ramifications are
76
     */
77 10574
    public function __construct(bool $isSupervisor = false, bool $isConditional = false)
78
    {
79
        // Supervisor?
80 10574
        parent::__construct($isSupervisor);
81
82
        // Initialise values
83 10574
        $this->left = new Border($isSupervisor, $isConditional);
84 10574
        $this->right = new Border($isSupervisor, $isConditional);
85 10574
        $this->top = new Border($isSupervisor, $isConditional);
86 10574
        $this->bottom = new Border($isSupervisor, $isConditional);
87 10574
        $this->diagonal = new Border($isSupervisor, $isConditional);
88 10574
        $this->diagonalDirection = self::DIAGONAL_NONE;
89
90
        // Specially for supervisor
91 10574
        if ($isSupervisor) {
92
            // Initialize pseudo-borders
93 10505
            $this->allBorders = new Border(true, $isConditional);
94 10505
            $this->outline = new Border(true, $isConditional);
95 10505
            $this->inside = new Border(true, $isConditional);
96 10505
            $this->vertical = new Border(true, $isConditional);
97 10505
            $this->horizontal = new Border(true, $isConditional);
98
99
            // bind parent if we are a supervisor
100 10505
            $this->left->bindParent($this, 'left');
101 10505
            $this->right->bindParent($this, 'right');
102 10505
            $this->top->bindParent($this, 'top');
103 10505
            $this->bottom->bindParent($this, 'bottom');
104 10505
            $this->diagonal->bindParent($this, 'diagonal');
105 10505
            $this->allBorders->bindParent($this, 'allBorders');
106 10505
            $this->outline->bindParent($this, 'outline');
107 10505
            $this->inside->bindParent($this, 'inside');
108 10505
            $this->vertical->bindParent($this, 'vertical');
109 10505
            $this->horizontal->bindParent($this, 'horizontal');
110
        }
111
    }
112
113
    /**
114
     * Get the shared style component for the currently active cell in currently active sheet.
115
     * Only used for style supervisor.
116
     */
117 41
    public function getSharedComponent(): self
118
    {
119
        /** @var Style $parent */
120 41
        $parent = $this->parent;
121
122 41
        return $parent->getSharedComponent()->getBorders();
123
    }
124
125
    /**
126
     * Build style array from subcomponents.
127
     */
128 31
    public function getStyleArray(array $array): array
129
    {
130 31
        return ['borders' => $array];
131
    }
132
133
    /**
134
     * Apply styles from array.
135
     *
136
     * <code>
137
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
138
     *         [
139
     *             'bottom' => [
140
     *                 'borderStyle' => Border::BORDER_DASHDOT,
141
     *                 'color' => [
142
     *                     'rgb' => '808080'
143
     *                 ]
144
     *             ],
145
     *             'top' => [
146
     *                 'borderStyle' => Border::BORDER_DASHDOT,
147
     *                 'color' => [
148
     *                     'rgb' => '808080'
149
     *                 ]
150
     *             ]
151
     *         ]
152
     * );
153
     * </code>
154
     *
155
     * <code>
156
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
157
     *         [
158
     *             'allBorders' => [
159
     *                 'borderStyle' => Border::BORDER_DASHDOT,
160
     *                 'color' => [
161
     *                     'rgb' => '808080'
162
     *                 ]
163
     *             ]
164
     *         ]
165
     * );
166
     * </code>
167
     *
168
     * @param array $styleArray Array containing style information
169
     *
170
     * @return $this
171
     */
172 94
    public function applyFromArray(array $styleArray): static
173
    {
174 94
        if ($this->isSupervisor) {
175 2
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
176
        } else {
177 94
            if (isset($styleArray['left'])) {
178 84
                $this->getLeft()->applyFromArray($styleArray['left']);
179
            }
180 94
            if (isset($styleArray['right'])) {
181 86
                $this->getRight()->applyFromArray($styleArray['right']);
182
            }
183 94
            if (isset($styleArray['top'])) {
184 86
                $this->getTop()->applyFromArray($styleArray['top']);
185
            }
186 94
            if (isset($styleArray['bottom'])) {
187 87
                $this->getBottom()->applyFromArray($styleArray['bottom']);
188
            }
189 94
            if (isset($styleArray['diagonal'])) {
190 29
                $this->getDiagonal()->applyFromArray($styleArray['diagonal']);
191
            }
192 94
            if (isset($styleArray['diagonalDirection'])) {
193 29
                $this->setDiagonalDirection($styleArray['diagonalDirection']);
194
            }
195 94
            if (isset($styleArray['allBorders'])) {
196 1
                $this->getLeft()->applyFromArray($styleArray['allBorders']);
197 1
                $this->getRight()->applyFromArray($styleArray['allBorders']);
198 1
                $this->getTop()->applyFromArray($styleArray['allBorders']);
199 1
                $this->getBottom()->applyFromArray($styleArray['allBorders']);
200
            }
201
        }
202
        $this->updateHashBeforeUse();
203 94
204
        return $this;
205
    }
206
207
    /**
208
     * Get Left.
209 1836
     */
210
    public function getLeft(): Border
211 1836
    {
212
        return $this->left;
213
    }
214
215
    /**
216
     * Get Right.
217 1835
     */
218
    public function getRight(): Border
219 1835
    {
220
        return $this->right;
221
    }
222
223
    /**
224
     * Get Top.
225 1837
     */
226
    public function getTop(): Border
227 1837
    {
228
        return $this->top;
229
    }
230
231
    /**
232
     * Get Bottom.
233 1836
     */
234
    public function getBottom(): Border
235 1836
    {
236
        return $this->bottom;
237
    }
238
239
    /**
240
     * Get Diagonal.
241 1737
     */
242
    public function getDiagonal(): Border
243 1737
    {
244
        return $this->diagonal;
245
    }
246
247
    /**
248
     * Get AllBorders (pseudo-border). Only applies to supervisor.
249 6
     */
250
    public function getAllBorders(): Border
251 6
    {
252 1
        if (!$this->isSupervisor) {
253
            throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
254
        }
255 5
256
        return $this->allBorders;
257
    }
258
259
    /**
260
     * Get Outline (pseudo-border). Only applies to supervisor.
261 2
     */
262
    public function getOutline(): Border
263 2
    {
264 1
        if (!$this->isSupervisor) {
265
            throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
266
        }
267 1
268
        return $this->outline;
269
    }
270
271
    /**
272
     * Get Inside (pseudo-border). Only applies to supervisor.
273 2
     */
274
    public function getInside(): Border
275 2
    {
276 1
        if (!$this->isSupervisor) {
277
            throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
278
        }
279 1
280
        return $this->inside;
281
    }
282
283
    /**
284
     * Get Vertical (pseudo-border). Only applies to supervisor.
285 2
     */
286
    public function getVertical(): Border
287 2
    {
288 1
        if (!$this->isSupervisor) {
289
            throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
290
        }
291 1
292
        return $this->vertical;
293
    }
294
295
    /**
296
     * Get Horizontal (pseudo-border). Only applies to supervisor.
297 3
     */
298
    public function getHorizontal(): Border
299 3
    {
300 1
        if (!$this->isSupervisor) {
301
            throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
302
        }
303 2
304
        return $this->horizontal;
305
    }
306
307
    /**
308
     * Get DiagonalDirection.
309 1299
     */
310
    public function getDiagonalDirection(): int
311 1299
    {
312 12
        if ($this->isSupervisor) {
313
            return $this->getSharedComponent()->getDiagonalDirection();
314
        }
315 1299
316
        return $this->diagonalDirection;
317
    }
318
319
    /**
320
     * Set DiagonalDirection.
321
     *
322
     * @param int $direction see self::DIAGONAL_*
323
     *
324
     * @return $this
325 807
     */
326
    public function setDiagonalDirection(int $direction): static
327 807
    {
328 2
        if ($this->isSupervisor) {
329 2
            $styleArray = $this->getStyleArray(['diagonalDirection' => $direction]);
330
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
331 807
        } else {
332
            $this->diagonalDirection = $direction;
333
        }
334 807
        $this->updateHashBeforeUse();
335
336
        return $this;
337
    }
338
339
    /**
340
     * Update Hash when something changes.
341
     */
342 1245
    protected function updateHash(): void
343
    {
344 1245
        $this->md5Sum = md5(
345 2
            $this->getLeft()->getHashCode()
346
            . $this->getRight()->getHashCode()
347
            . $this->getTop()->getHashCode()
348 1245
            . $this->getBottom()->getHashCode()
349 1245
            . $this->getDiagonal()->getHashCode()
350 1245
            . $this->getDiagonalDirection()
351 1245
            . __CLASS__
352 1245
        );
353 1245
    }
354 1245
355 1245
    /**
356 1245
     * Get hash code.
357
     *
358
     * @return string Hash code
359 14
     */
360
    public function getHashCode(): string
361 14
    {
362 14
        if ($this->isSupervisor) {
363 14
            return $this->getSharedComponent()->getHashcode();
364 14
        }
365 14
366 14
        if ($this->updateMd5Sum) {
367 14
            $this->updateHash();
368
        }
369 14
370
        return $this->md5Sum;
371
    }
372
373
    protected function exportArray1(): array
374
    {
375
        $exportedArray = [];
376
        $this->exportArray2($exportedArray, 'bottom', $this->getBottom());
377
        $this->exportArray2($exportedArray, 'diagonal', $this->getDiagonal());
378
        $this->exportArray2($exportedArray, 'diagonalDirection', $this->getDiagonalDirection());
379
        $this->exportArray2($exportedArray, 'left', $this->getLeft());
380
        $this->exportArray2($exportedArray, 'right', $this->getRight());
381
        $this->exportArray2($exportedArray, 'top', $this->getTop());
382
383
        return $exportedArray;
384
    }
385
}
386