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\Calculation\Functions;
6
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
7
use PhpOffice\PhpSpreadsheet\Exception;
8
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
9
use PhpOffice\PhpSpreadsheet\Spreadsheet;
10
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
11
 
12
class Style extends Supervisor
13
{
14
    /**
15
     * Font.
16
     */
17
    protected Font $font;
18
 
19
    /**
20
     * Fill.
21
     */
22
    protected Fill $fill;
23
 
24
    /**
25
     * Borders.
26
     */
27
    protected Borders $borders;
28
 
29
    /**
30
     * Alignment.
31
     */
32
    protected Alignment $alignment;
33
 
34
    /**
35
     * Number Format.
36
     */
37
    protected NumberFormat $numberFormat;
38
 
39
    /**
40
     * Protection.
41
     */
42
    protected Protection $protection;
43
 
44
    /**
45
     * Index of style in collection. Only used for real style.
46
     */
47
    protected int $index;
48
 
49
    /**
50
     * Use Quote Prefix when displaying in cell editor. Only used for real style.
51
     */
52
    protected bool $quotePrefix = false;
53
 
54
    /**
55
     * Internal cache for styles
56
     * Used when applying style on range of cells (column or row) and cleared when
57
     * all cells in range is styled.
58
     *
59
     * PhpSpreadsheet will always minimize the amount of styles used. So cells with
60
     * same styles will reference the same Style instance. To check if two styles
61
     * are similar Style::getHashCode() is used. This call is expensive. To minimize
62
     * the need to call this method we can cache the internal PHP object id of the
63
     * Style in the range. Style::getHashCode() will then only be called when we
64
     * encounter a unique style.
65
     *
66
     * @see Style::applyFromArray()
67
     * @see Style::getHashCode()
68
     *
69
     * @var null|array<string, array>
70
     */
71
    private static ?array $cachedStyles = null;
72
 
73
    /**
74
     * Create a new Style.
75
     *
76
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
77
     *         Leave this value at default unless you understand exactly what
78
     *    its ramifications are
79
     * @param bool $isConditional Flag indicating if this is a conditional style or not
80
     *       Leave this value at default unless you understand exactly what
81
     *    its ramifications are
82
     */
83
    public function __construct(bool $isSupervisor = false, bool $isConditional = false)
84
    {
85
        parent::__construct($isSupervisor);
86
 
87
        // Initialise values
88
        $this->font = new Font($isSupervisor, $isConditional);
89
        $this->fill = new Fill($isSupervisor, $isConditional);
90
        $this->borders = new Borders($isSupervisor, $isConditional);
91
        $this->alignment = new Alignment($isSupervisor, $isConditional);
92
        $this->numberFormat = new NumberFormat($isSupervisor, $isConditional);
93
        $this->protection = new Protection($isSupervisor, $isConditional);
94
 
95
        // bind parent if we are a supervisor
96
        if ($isSupervisor) {
97
            $this->font->bindParent($this);
98
            $this->fill->bindParent($this);
99
            $this->borders->bindParent($this);
100
            $this->alignment->bindParent($this);
101
            $this->numberFormat->bindParent($this);
102
            $this->protection->bindParent($this);
103
        }
104
    }
105
 
106
    /**
107
     * Get the shared style component for the currently active cell in currently active sheet.
108
     * Only used for style supervisor.
109
     */
110
    public function getSharedComponent(): self
111
    {
112
        $activeSheet = $this->getActiveSheet();
113
        $selectedCell = Functions::trimSheetFromCellReference($this->getActiveCell()); // e.g. 'A1'
114
 
115
        if ($activeSheet->cellExists($selectedCell)) {
116
            $xfIndex = $activeSheet->getCell($selectedCell)->getXfIndex();
117
        } else {
118
            $xfIndex = 0;
119
        }
120
 
121
        return $activeSheet->getParentOrThrow()->getCellXfByIndex($xfIndex);
122
    }
123
 
124
    /**
125
     * Get parent. Only used for style supervisor.
126
     */
127
    public function getParent(): Spreadsheet
128
    {
129
        return $this->getActiveSheet()->getParentOrThrow();
130
    }
131
 
132
    /**
133
     * Build style array from subcomponents.
134
     */
135
    public function getStyleArray(array $array): array
136
    {
137
        return ['quotePrefix' => $array];
138
    }
139
 
140
    /**
141
     * Apply styles from array.
142
     *
143
     * <code>
144
     * $spreadsheet->getActiveSheet()->getStyle('B2')->applyFromArray(
145
     *     [
146
     *         'font' => [
147
     *             'name' => 'Arial',
148
     *             'bold' => true,
149
     *             'italic' => false,
150
     *             'underline' => Font::UNDERLINE_DOUBLE,
151
     *             'strikethrough' => false,
152
     *             'color' => [
153
     *                 'rgb' => '808080'
154
     *             ]
155
     *         ],
156
     *         'borders' => [
157
     *             'bottom' => [
158
     *                 'borderStyle' => Border::BORDER_DASHDOT,
159
     *                 'color' => [
160
     *                     'rgb' => '808080'
161
     *                 ]
162
     *             ],
163
     *             'top' => [
164
     *                 'borderStyle' => Border::BORDER_DASHDOT,
165
     *                 'color' => [
166
     *                     'rgb' => '808080'
167
     *                 ]
168
     *             ]
169
     *         ],
170
     *         'alignment' => [
171
     *             'horizontal' => Alignment::HORIZONTAL_CENTER,
172
     *             'vertical' => Alignment::VERTICAL_CENTER,
173
     *             'wrapText' => true,
174
     *         ],
175
     *         'quotePrefix'    => true
176
     *     ]
177
     * );
178
     * </code>
179
     *
180
     * @param array $styleArray Array containing style information
181
     * @param bool $advancedBorders advanced mode for setting borders
182
     *
183
     * @return $this
184
     */
185
    public function applyFromArray(array $styleArray, bool $advancedBorders = true): static
186
    {
187
        if ($this->isSupervisor) {
188
            $pRange = $this->getSelectedCells();
189
 
190
            // Uppercase coordinate and strip any Worksheet reference from the selected range
191
            $pRange = strtoupper($pRange);
192
            if (str_contains($pRange, '!')) {
193
                $pRangeWorksheet = StringHelper::strToUpper(substr($pRange, 0, (int) strrpos($pRange, '!')));
194
                $pRangeWorksheet = Worksheet::unApostrophizeTitle($pRangeWorksheet);
195
                if ($pRangeWorksheet !== '' && StringHelper::strToUpper($this->getActiveSheet()->getTitle()) !== $pRangeWorksheet) {
196
                    throw new Exception('Invalid Worksheet for specified Range');
197
                }
198
                $pRange = strtoupper(Functions::trimSheetFromCellReference($pRange));
199
            }
200
 
201
            // Is it a cell range or a single cell?
202
            if (!str_contains($pRange, ':')) {
203
                $rangeA = $pRange;
204
                $rangeB = $pRange;
205
            } else {
206
                [$rangeA, $rangeB] = explode(':', $pRange);
207
            }
208
 
209
            // Calculate range outer borders
210
            $rangeStart = Coordinate::coordinateFromString($rangeA);
211
            $rangeEnd = Coordinate::coordinateFromString($rangeB);
212
            $rangeStartIndexes = Coordinate::indexesFromString($rangeA);
213
            $rangeEndIndexes = Coordinate::indexesFromString($rangeB);
214
 
215
            $columnStart = $rangeStart[0];
216
            $columnEnd = $rangeEnd[0];
217
 
218
            // Make sure we can loop upwards on rows and columns
219
            if ($rangeStartIndexes[0] > $rangeEndIndexes[0] && $rangeStartIndexes[1] > $rangeEndIndexes[1]) {
220
                $tmp = $rangeStartIndexes;
221
                $rangeStartIndexes = $rangeEndIndexes;
222
                $rangeEndIndexes = $tmp;
223
            }
224
 
225
            // ADVANCED MODE:
226
            if ($advancedBorders && isset($styleArray['borders'])) {
227
                // 'allBorders' is a shorthand property for 'outline' and 'inside' and
228
                //        it applies to components that have not been set explicitly
229
                if (isset($styleArray['borders']['allBorders'])) {
230
                    foreach (['outline', 'inside'] as $component) {
231
                        if (!isset($styleArray['borders'][$component])) {
232
                            $styleArray['borders'][$component] = $styleArray['borders']['allBorders'];
233
                        }
234
                    }
235
                    unset($styleArray['borders']['allBorders']); // not needed any more
236
                }
237
                // 'outline' is a shorthand property for 'top', 'right', 'bottom', 'left'
238
                //        it applies to components that have not been set explicitly
239
                if (isset($styleArray['borders']['outline'])) {
240
                    foreach (['top', 'right', 'bottom', 'left'] as $component) {
241
                        if (!isset($styleArray['borders'][$component])) {
242
                            $styleArray['borders'][$component] = $styleArray['borders']['outline'];
243
                        }
244
                    }
245
                    unset($styleArray['borders']['outline']); // not needed any more
246
                }
247
                // 'inside' is a shorthand property for 'vertical' and 'horizontal'
248
                //        it applies to components that have not been set explicitly
249
                if (isset($styleArray['borders']['inside'])) {
250
                    foreach (['vertical', 'horizontal'] as $component) {
251
                        if (!isset($styleArray['borders'][$component])) {
252
                            $styleArray['borders'][$component] = $styleArray['borders']['inside'];
253
                        }
254
                    }
255
                    unset($styleArray['borders']['inside']); // not needed any more
256
                }
257
                // width and height characteristics of selection, 1, 2, or 3 (for 3 or more)
258
                $xMax = min($rangeEndIndexes[0] - $rangeStartIndexes[0] + 1, 3);
259
                $yMax = min($rangeEndIndexes[1] - $rangeStartIndexes[1] + 1, 3);
260
 
261
                // loop through up to 3 x 3 = 9 regions
262
                for ($x = 1; $x <= $xMax; ++$x) {
263
                    // start column index for region
264
                    $colStart = ($x == 3)
265
                        ? Coordinate::stringFromColumnIndex($rangeEndIndexes[0])
266
                        : Coordinate::stringFromColumnIndex($rangeStartIndexes[0] + $x - 1);
267
                    // end column index for region
268
                    $colEnd = ($x == 1)
269
                        ? Coordinate::stringFromColumnIndex($rangeStartIndexes[0])
270
                        : Coordinate::stringFromColumnIndex($rangeEndIndexes[0] - $xMax + $x);
271
 
272
                    for ($y = 1; $y <= $yMax; ++$y) {
273
                        // which edges are touching the region
274
                        $edges = [];
275
                        if ($x == 1) {
276
                            // are we at left edge
277
                            $edges[] = 'left';
278
                        }
279
                        if ($x == $xMax) {
280
                            // are we at right edge
281
                            $edges[] = 'right';
282
                        }
283
                        if ($y == 1) {
284
                            // are we at top edge?
285
                            $edges[] = 'top';
286
                        }
287
                        if ($y == $yMax) {
288
                            // are we at bottom edge?
289
                            $edges[] = 'bottom';
290
                        }
291
 
292
                        // start row index for region
293
                        $rowStart = ($y == 3)
294
                            ? $rangeEndIndexes[1] : $rangeStartIndexes[1] + $y - 1;
295
 
296
                        // end row index for region
297
                        $rowEnd = ($y == 1)
298
                            ? $rangeStartIndexes[1] : $rangeEndIndexes[1] - $yMax + $y;
299
 
300
                        // build range for region
301
                        $range = $colStart . $rowStart . ':' . $colEnd . $rowEnd;
302
 
303
                        // retrieve relevant style array for region
304
                        $regionStyles = $styleArray;
305
                        unset($regionStyles['borders']['inside']);
306
 
307
                        // what are the inner edges of the region when looking at the selection
308
                        $innerEdges = array_diff(['top', 'right', 'bottom', 'left'], $edges);
309
 
310
                        // inner edges that are not touching the region should take the 'inside' border properties if they have been set
311
                        foreach ($innerEdges as $innerEdge) {
312
                            switch ($innerEdge) {
313
                                case 'top':
314
                                case 'bottom':
315
                                    // should pick up 'horizontal' border property if set
316
                                    if (isset($styleArray['borders']['horizontal'])) {
317
                                        $regionStyles['borders'][$innerEdge] = $styleArray['borders']['horizontal'];
318
                                    } else {
319
                                        unset($regionStyles['borders'][$innerEdge]);
320
                                    }
321
 
322
                                    break;
323
                                case 'left':
324
                                case 'right':
325
                                    // should pick up 'vertical' border property if set
326
                                    if (isset($styleArray['borders']['vertical'])) {
327
                                        $regionStyles['borders'][$innerEdge] = $styleArray['borders']['vertical'];
328
                                    } else {
329
                                        unset($regionStyles['borders'][$innerEdge]);
330
                                    }
331
 
332
                                    break;
333
                            }
334
                        }
335
 
336
                        // apply region style to region by calling applyFromArray() in simple mode
337
                        $this->getActiveSheet()->getStyle($range)->applyFromArray($regionStyles, false);
338
                    }
339
                }
340
 
341
                // restore initial cell selection range
342
                $this->getActiveSheet()->getStyle($pRange);
343
 
344
                return $this;
345
            }
346
 
347
            // SIMPLE MODE:
348
            // Selection type, inspect
349
            if (preg_match('/^[A-Z]+1:[A-Z]+1048576$/', $pRange)) {
350
                $selectionType = 'COLUMN';
351
 
352
                // Enable caching of styles
353
                self::$cachedStyles = ['hashByObjId' => [], 'styleByHash' => []];
354
            } elseif (preg_match('/^A\d+:XFD\d+$/', $pRange)) {
355
                $selectionType = 'ROW';
356
 
357
                // Enable caching of styles
358
                self::$cachedStyles = ['hashByObjId' => [], 'styleByHash' => []];
359
            } else {
360
                $selectionType = 'CELL';
361
            }
362
 
363
            // First loop through columns, rows, or cells to find out which styles are affected by this operation
364
            $oldXfIndexes = $this->getOldXfIndexes($selectionType, $rangeStartIndexes, $rangeEndIndexes, $columnStart, $columnEnd, $styleArray);
365
 
366
            // clone each of the affected styles, apply the style array, and add the new styles to the workbook
367
            $workbook = $this->getActiveSheet()->getParentOrThrow();
368
            $newXfIndexes = [];
369
            foreach ($oldXfIndexes as $oldXfIndex => $dummy) {
370
                $style = $workbook->getCellXfByIndex($oldXfIndex);
371
 
372
                // $cachedStyles is set when applying style for a range of cells, either column or row
373
                if (self::$cachedStyles === null) {
374
                    // Clone the old style and apply style-array
375
                    $newStyle = clone $style;
376
                    $newStyle->applyFromArray($styleArray);
377
 
378
                    // Look for existing style we can use instead (reduce memory usage)
379
                    $existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode());
380
                } else {
381
                    // Style cache is stored by Style::getHashCode(). But calling this method is
382
                    // expensive. So we cache the php obj id -> hash.
383
                    $objId = spl_object_id($style);
384
 
385
                    // Look for the original HashCode
386
                    $styleHash = self::$cachedStyles['hashByObjId'][$objId] ?? null;
387
                    if ($styleHash === null) {
388
                        // This object_id is not cached, store the hashcode in case encounter again
389
                        $styleHash = self::$cachedStyles['hashByObjId'][$objId] = $style->getHashCode();
390
                    }
391
 
392
                    // Find existing style by hash.
393
                    $existingStyle = self::$cachedStyles['styleByHash'][$styleHash] ?? null;
394
 
395
                    if (!$existingStyle) {
396
                        // The old style combined with the new style array is not cached, so we create it now
397
                        $newStyle = clone $style;
398
                        $newStyle->applyFromArray($styleArray);
399
 
400
                        // Look for similar style in workbook to reduce memory usage
401
                        $existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode());
402
 
403
                        // Cache the new style by original hashcode
404
                        self::$cachedStyles['styleByHash'][$styleHash] = $existingStyle instanceof self ? $existingStyle : $newStyle;
405
                    }
406
                }
407
 
408
                if ($existingStyle) {
409
                    // there is already such cell Xf in our collection
410
                    $newXfIndexes[$oldXfIndex] = $existingStyle->getIndex();
411
                } else {
412
                    if (!isset($newStyle)) {
413
                        // Handle bug in PHPStan, see https://github.com/phpstan/phpstan/issues/5805
414
                        // $newStyle should always be defined.
415
                        // This block might not be needed in the future
416
                        // @codeCoverageIgnoreStart
417
                        $newStyle = clone $style;
418
                        $newStyle->applyFromArray($styleArray);
419
                        // @codeCoverageIgnoreEnd
420
                    }
421
 
422
                    // we don't have such a cell Xf, need to add
423
                    $workbook->addCellXf($newStyle);
424
                    $newXfIndexes[$oldXfIndex] = $newStyle->getIndex();
425
                }
426
            }
427
 
428
            // Loop through columns, rows, or cells again and update the XF index
429
            switch ($selectionType) {
430
                case 'COLUMN':
431
                    for ($col = $rangeStartIndexes[0]; $col <= $rangeEndIndexes[0]; ++$col) {
432
                        $columnDimension = $this->getActiveSheet()->getColumnDimensionByColumn($col);
433
                        $oldXfIndex = $columnDimension->getXfIndex();
434
                        $columnDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
435
                    }
436
 
437
                    // Disable caching of styles
438
                    self::$cachedStyles = null;
439
 
440
                    break;
441
                case 'ROW':
442
                    for ($row = $rangeStartIndexes[1]; $row <= $rangeEndIndexes[1]; ++$row) {
443
                        $rowDimension = $this->getActiveSheet()->getRowDimension($row);
444
                        // row without explicit style should be formatted based on default style
445
                        $oldXfIndex = $rowDimension->getXfIndex() ?? 0;
446
                        $rowDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
447
                    }
448
 
449
                    // Disable caching of styles
450
                    self::$cachedStyles = null;
451
 
452
                    break;
453
                case 'CELL':
454
                    for ($col = $rangeStartIndexes[0]; $col <= $rangeEndIndexes[0]; ++$col) {
455
                        for ($row = $rangeStartIndexes[1]; $row <= $rangeEndIndexes[1]; ++$row) {
456
                            $cell = $this->getActiveSheet()->getCell([$col, $row]);
457
                            $oldXfIndex = $cell->getXfIndex();
458
                            $cell->setXfIndex($newXfIndexes[$oldXfIndex]);
459
                        }
460
                    }
461
 
462
                    break;
463
            }
464
        } else {
465
            // not a supervisor, just apply the style array directly on style object
466
            if (isset($styleArray['fill'])) {
467
                $this->getFill()->applyFromArray($styleArray['fill']);
468
            }
469
            if (isset($styleArray['font'])) {
470
                $this->getFont()->applyFromArray($styleArray['font']);
471
            }
472
            if (isset($styleArray['borders'])) {
473
                $this->getBorders()->applyFromArray($styleArray['borders']);
474
            }
475
            if (isset($styleArray['alignment'])) {
476
                $this->getAlignment()->applyFromArray($styleArray['alignment']);
477
            }
478
            if (isset($styleArray['numberFormat'])) {
479
                $this->getNumberFormat()->applyFromArray($styleArray['numberFormat']);
480
            }
481
            if (isset($styleArray['protection'])) {
482
                $this->getProtection()->applyFromArray($styleArray['protection']);
483
            }
484
            if (isset($styleArray['quotePrefix'])) {
485
                $this->quotePrefix = $styleArray['quotePrefix'];
486
            }
487
        }
488
 
489
        return $this;
490
    }
491
 
492
    private function getOldXfIndexes(string $selectionType, array $rangeStart, array $rangeEnd, string $columnStart, string $columnEnd, array $styleArray): array
493
    {
494
        $oldXfIndexes = [];
495
        switch ($selectionType) {
496
            case 'COLUMN':
497
                for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
498
                    $oldXfIndexes[$this->getActiveSheet()->getColumnDimensionByColumn($col)->getXfIndex()] = true;
499
                }
500
                foreach ($this->getActiveSheet()->getColumnIterator($columnStart, $columnEnd) as $columnIterator) {
501
                    $cellIterator = $columnIterator->getCellIterator();
502
                    $cellIterator->setIterateOnlyExistingCells(true);
503
                    foreach ($cellIterator as $columnCell) {
504
                        $columnCell->getStyle()
505
                            ->applyFromArray($styleArray);
506
                    }
507
                }
508
 
509
                break;
510
            case 'ROW':
511
                for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
512
                    if ($this->getActiveSheet()->getRowDimension($row)->getXfIndex() === null) {
513
                        $oldXfIndexes[0] = true; // row without explicit style should be formatted based on default style
514
                    } else {
515
                        $oldXfIndexes[$this->getActiveSheet()->getRowDimension($row)->getXfIndex()] = true;
516
                    }
517
                }
518
                foreach ($this->getActiveSheet()->getRowIterator((int) $rangeStart[1], (int) $rangeEnd[1]) as $rowIterator) {
519
                    $cellIterator = $rowIterator->getCellIterator();
520
                    $cellIterator->setIterateOnlyExistingCells(true);
521
                    foreach ($cellIterator as $rowCell) {
522
                        $rowCell->getStyle()
523
                            ->applyFromArray($styleArray);
524
                    }
525
                }
526
 
527
                break;
528
            case 'CELL':
529
                for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
530
                    for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
531
                        $oldXfIndexes[$this->getActiveSheet()->getCell([$col, $row])->getXfIndex()] = true;
532
                    }
533
                }
534
 
535
                break;
536
        }
537
 
538
        return $oldXfIndexes;
539
    }
540
 
541
    /**
542
     * Get Fill.
543
     */
544
    public function getFill(): Fill
545
    {
546
        return $this->fill;
547
    }
548
 
549
    /**
550
     * Get Font.
551
     */
552
    public function getFont(): Font
553
    {
554
        return $this->font;
555
    }
556
 
557
    /**
558
     * Set font.
559
     *
560
     * @return $this
561
     */
562
    public function setFont(Font $font): static
563
    {
564
        $this->font = $font;
565
 
566
        return $this;
567
    }
568
 
569
    /**
570
     * Get Borders.
571
     */
572
    public function getBorders(): Borders
573
    {
574
        return $this->borders;
575
    }
576
 
577
    /**
578
     * Get Alignment.
579
     */
580
    public function getAlignment(): Alignment
581
    {
582
        return $this->alignment;
583
    }
584
 
585
    /**
586
     * Get Number Format.
587
     */
588
    public function getNumberFormat(): NumberFormat
589
    {
590
        return $this->numberFormat;
591
    }
592
 
593
    /**
594
     * Get Conditional Styles. Only used on supervisor.
595
     *
596
     * @return Conditional[]
597
     */
598
    public function getConditionalStyles(): array
599
    {
600
        return $this->getActiveSheet()->getConditionalStyles($this->getActiveCell());
601
    }
602
 
603
    /**
604
     * Set Conditional Styles. Only used on supervisor.
605
     *
606
     * @param Conditional[] $conditionalStyleArray Array of conditional styles
607
     *
608
     * @return $this
609
     */
610
    public function setConditionalStyles(array $conditionalStyleArray): static
611
    {
612
        $this->getActiveSheet()->setConditionalStyles($this->getSelectedCells(), $conditionalStyleArray);
613
 
614
        return $this;
615
    }
616
 
617
    /**
618
     * Get Protection.
619
     */
620
    public function getProtection(): Protection
621
    {
622
        return $this->protection;
623
    }
624
 
625
    /**
626
     * Get quote prefix.
627
     */
628
    public function getQuotePrefix(): bool
629
    {
630
        if ($this->isSupervisor) {
631
            return $this->getSharedComponent()->getQuotePrefix();
632
        }
633
 
634
        return $this->quotePrefix;
635
    }
636
 
637
    /**
638
     * Set quote prefix.
639
     *
640
     * @return $this
641
     */
642
    public function setQuotePrefix(bool $quotePrefix): static
643
    {
644
        if ($quotePrefix == '') {
645
            $quotePrefix = false;
646
        }
647
        if ($this->isSupervisor) {
648
            $styleArray = ['quotePrefix' => $quotePrefix];
649
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
650
        } else {
651
            $this->quotePrefix = (bool) $quotePrefix;
652
        }
653
 
654
        return $this;
655
    }
656
 
657
    /**
658
     * Get hash code.
659
     *
660
     * @return string Hash code
661
     */
662
    public function getHashCode(): string
663
    {
664
        return md5(
665
            $this->fill->getHashCode()
666
            . $this->font->getHashCode()
667
            . $this->borders->getHashCode()
668
            . $this->alignment->getHashCode()
669
            . $this->numberFormat->getHashCode()
670
            . $this->protection->getHashCode()
671
            . ($this->quotePrefix ? 't' : 'f')
672
            . __CLASS__
673
        );
674
    }
675
 
676
    /**
677
     * Get own index in style collection.
678
     */
679
    public function getIndex(): int
680
    {
681
        return $this->index;
682
    }
683
 
684
    /**
685
     * Set own index in style collection.
686
     */
687
    public function setIndex(int $index): void
688
    {
689
        $this->index = $index;
690
    }
691
 
692
    protected function exportArray1(): array
693
    {
694
        $exportedArray = [];
695
        $this->exportArray2($exportedArray, 'alignment', $this->getAlignment());
696
        $this->exportArray2($exportedArray, 'borders', $this->getBorders());
697
        $this->exportArray2($exportedArray, 'fill', $this->getFill());
698
        $this->exportArray2($exportedArray, 'font', $this->getFont());
699
        $this->exportArray2($exportedArray, 'numberFormat', $this->getNumberFormat());
700
        $this->exportArray2($exportedArray, 'protection', $this->getProtection());
701
        $this->exportArray2($exportedArray, 'quotePrefx', $this->getQuotePrefix());
702
 
703
        return $exportedArray;
704
    }
705
}