Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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