Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
    public function __construct(bool $isSupervisor = false, bool $isConditional = false)
78
    {
79
        // Supervisor?
80
        parent::__construct($isSupervisor);
81
 
82
        // Initialise values
83
        $this->left = new Border($isSupervisor, $isConditional);
84
        $this->right = new Border($isSupervisor, $isConditional);
85
        $this->top = new Border($isSupervisor, $isConditional);
86
        $this->bottom = new Border($isSupervisor, $isConditional);
87
        $this->diagonal = new Border($isSupervisor, $isConditional);
88
        $this->diagonalDirection = self::DIAGONAL_NONE;
89
 
90
        // Specially for supervisor
91
        if ($isSupervisor) {
92
            // Initialize pseudo-borders
93
            $this->allBorders = new Border(true, $isConditional);
94
            $this->outline = new Border(true, $isConditional);
95
            $this->inside = new Border(true, $isConditional);
96
            $this->vertical = new Border(true, $isConditional);
97
            $this->horizontal = new Border(true, $isConditional);
98
 
99
            // bind parent if we are a supervisor
100
            $this->left->bindParent($this, 'left');
101
            $this->right->bindParent($this, 'right');
102
            $this->top->bindParent($this, 'top');
103
            $this->bottom->bindParent($this, 'bottom');
104
            $this->diagonal->bindParent($this, 'diagonal');
105
            $this->allBorders->bindParent($this, 'allBorders');
106
            $this->outline->bindParent($this, 'outline');
107
            $this->inside->bindParent($this, 'inside');
108
            $this->vertical->bindParent($this, 'vertical');
109
            $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
    public function getSharedComponent(): self
118
    {
119
        /** @var Style $parent */
120
        $parent = $this->parent;
121
 
122
        return $parent->getSharedComponent()->getBorders();
123
    }
124
 
125
    /**
126
     * Build style array from subcomponents.
127
     */
128
    public function getStyleArray(array $array): array
129
    {
130
        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
    public function applyFromArray(array $styleArray): static
173
    {
174
        if ($this->isSupervisor) {
175
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
176
        } else {
177
            if (isset($styleArray['left'])) {
178
                $this->getLeft()->applyFromArray($styleArray['left']);
179
            }
180
            if (isset($styleArray['right'])) {
181
                $this->getRight()->applyFromArray($styleArray['right']);
182
            }
183
            if (isset($styleArray['top'])) {
184
                $this->getTop()->applyFromArray($styleArray['top']);
185
            }
186
            if (isset($styleArray['bottom'])) {
187
                $this->getBottom()->applyFromArray($styleArray['bottom']);
188
            }
189
            if (isset($styleArray['diagonal'])) {
190
                $this->getDiagonal()->applyFromArray($styleArray['diagonal']);
191
            }
192
            if (isset($styleArray['diagonalDirection'])) {
193
                $this->setDiagonalDirection($styleArray['diagonalDirection']);
194
            }
195
            if (isset($styleArray['allBorders'])) {
196
                $this->getLeft()->applyFromArray($styleArray['allBorders']);
197
                $this->getRight()->applyFromArray($styleArray['allBorders']);
198
                $this->getTop()->applyFromArray($styleArray['allBorders']);
199
                $this->getBottom()->applyFromArray($styleArray['allBorders']);
200
            }
201
        }
202
 
203
        return $this;
204
    }
205
 
206
    /**
207
     * Get Left.
208
     */
209
    public function getLeft(): Border
210
    {
211
        return $this->left;
212
    }
213
 
214
    /**
215
     * Get Right.
216
     */
217
    public function getRight(): Border
218
    {
219
        return $this->right;
220
    }
221
 
222
    /**
223
     * Get Top.
224
     */
225
    public function getTop(): Border
226
    {
227
        return $this->top;
228
    }
229
 
230
    /**
231
     * Get Bottom.
232
     */
233
    public function getBottom(): Border
234
    {
235
        return $this->bottom;
236
    }
237
 
238
    /**
239
     * Get Diagonal.
240
     */
241
    public function getDiagonal(): Border
242
    {
243
        return $this->diagonal;
244
    }
245
 
246
    /**
247
     * Get AllBorders (pseudo-border). Only applies to supervisor.
248
     */
249
    public function getAllBorders(): Border
250
    {
251
        if (!$this->isSupervisor) {
252
            throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
253
        }
254
 
255
        return $this->allBorders;
256
    }
257
 
258
    /**
259
     * Get Outline (pseudo-border). Only applies to supervisor.
260
     */
261
    public function getOutline(): Border
262
    {
263
        if (!$this->isSupervisor) {
264
            throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
265
        }
266
 
267
        return $this->outline;
268
    }
269
 
270
    /**
271
     * Get Inside (pseudo-border). Only applies to supervisor.
272
     */
273
    public function getInside(): Border
274
    {
275
        if (!$this->isSupervisor) {
276
            throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
277
        }
278
 
279
        return $this->inside;
280
    }
281
 
282
    /**
283
     * Get Vertical (pseudo-border). Only applies to supervisor.
284
     */
285
    public function getVertical(): Border
286
    {
287
        if (!$this->isSupervisor) {
288
            throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
289
        }
290
 
291
        return $this->vertical;
292
    }
293
 
294
    /**
295
     * Get Horizontal (pseudo-border). Only applies to supervisor.
296
     */
297
    public function getHorizontal(): Border
298
    {
299
        if (!$this->isSupervisor) {
300
            throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
301
        }
302
 
303
        return $this->horizontal;
304
    }
305
 
306
    /**
307
     * Get DiagonalDirection.
308
     */
309
    public function getDiagonalDirection(): int
310
    {
311
        if ($this->isSupervisor) {
312
            return $this->getSharedComponent()->getDiagonalDirection();
313
        }
314
 
315
        return $this->diagonalDirection;
316
    }
317
 
318
    /**
319
     * Set DiagonalDirection.
320
     *
321
     * @param int $direction see self::DIAGONAL_*
322
     *
323
     * @return $this
324
     */
325
    public function setDiagonalDirection(int $direction): static
326
    {
327
        if ($this->isSupervisor) {
328
            $styleArray = $this->getStyleArray(['diagonalDirection' => $direction]);
329
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
330
        } else {
331
            $this->diagonalDirection = $direction;
332
        }
333
 
334
        return $this;
335
    }
336
 
337
    /**
338
     * Get hash code.
339
     *
340
     * @return string Hash code
341
     */
342
    public function getHashCode(): string
343
    {
344
        if ($this->isSupervisor) {
345
            return $this->getSharedComponent()->getHashcode();
346
        }
347
 
348
        return md5(
349
            $this->getLeft()->getHashCode()
350
            . $this->getRight()->getHashCode()
351
            . $this->getTop()->getHashCode()
352
            . $this->getBottom()->getHashCode()
353
            . $this->getDiagonal()->getHashCode()
354
            . $this->getDiagonalDirection()
355
            . __CLASS__
356
        );
357
    }
358
 
359
    protected function exportArray1(): array
360
    {
361
        $exportedArray = [];
362
        $this->exportArray2($exportedArray, 'bottom', $this->getBottom());
363
        $this->exportArray2($exportedArray, 'diagonal', $this->getDiagonal());
364
        $this->exportArray2($exportedArray, 'diagonalDirection', $this->getDiagonalDirection());
365
        $this->exportArray2($exportedArray, 'left', $this->getLeft());
366
        $this->exportArray2($exportedArray, 'right', $this->getRight());
367
        $this->exportArray2($exportedArray, 'top', $this->getTop());
368
 
369
        return $exportedArray;
370
    }
371
}