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

Border::getSharedComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.9666
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
7
class Border extends Supervisor
8
{
9
    // Border style
10
    const BORDER_NONE = 'none';
11
    const BORDER_DASHDOT = 'dashDot';
12
    const BORDER_DASHDOTDOT = 'dashDotDot';
13
    const BORDER_DASHED = 'dashed';
14
    const BORDER_DOTTED = 'dotted';
15
    const BORDER_DOUBLE = 'double';
16
    const BORDER_HAIR = 'hair';
17
    const BORDER_MEDIUM = 'medium';
18
    const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
19
    const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
20
    const BORDER_MEDIUMDASHED = 'mediumDashed';
21
    const BORDER_SLANTDASHDOT = 'slantDashDot';
22
    const BORDER_THICK = 'thick';
23
    const BORDER_THIN = 'thin';
24
    const BORDER_OMIT = 'omit'; // should be used only for Conditional
25
26
    /**
27
     * Border style.
28
     */
29
    protected string $borderStyle = self::BORDER_NONE;
30
31
    /**
32
     * Border color.
33
     */
34
    protected Color $color;
35
36
    public ?int $colorIndex = null;
37
38
    /**
39
     * Create a new Border.
40
     *
41
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
42
     *                                    Leave this value at default unless you understand exactly what
43
     *                                        its ramifications are
44
     */
45 10574
    public function __construct(bool $isSupervisor = false, bool $isConditional = false)
46
    {
47
        // Supervisor?
48 10574
        parent::__construct($isSupervisor);
49
50
        // Initialise values
51 10574
        $this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
52
53
        // bind parent if we are a supervisor
54 10574
        if ($isSupervisor) {
55 10505
            $this->color->bindParent($this, 'color');
56
        }
57 10574
        if ($isConditional) {
58 402
            $this->borderStyle = self::BORDER_OMIT;
59
        }
60
    }
61
62
    /**
63
     * Get the shared style component for the currently active cell in currently active sheet.
64
     * Only used for style supervisor.
65
     */
66 41
    public function getSharedComponent(): self
67
    {
68
        /** @var Style $parent */
69 41
        $parent = $this->parent;
70
71
        /** @var Borders $sharedComponent */
72 41
        $sharedComponent = $parent->getSharedComponent();
73
74 41
        return match ($this->parentPropertyName) {
75 37
            'bottom' => $sharedComponent->getBottom(),
76 12
            'diagonal' => $sharedComponent->getDiagonal(),
77 35
            'left' => $sharedComponent->getLeft(),
78 35
            'right' => $sharedComponent->getRight(),
79 36
            'top' => $sharedComponent->getTop(),
80 41
            default => throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.'),
81 41
        };
82
    }
83
84
    /**
85
     * Build style array from subcomponents.
86
     */
87 29
    public function getStyleArray(array $array): array
88
    {
89
        /** @var Style $parent */
90 29
        $parent = $this->parent;
91
92 29
        return $parent->getStyleArray([$this->parentPropertyName => $array]);
93
    }
94
95
    /**
96
     * Apply styles from array.
97
     *
98
     * <code>
99
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
100
     *        [
101
     *            'borderStyle' => Border::BORDER_DASHDOT,
102
     *            'color' => [
103
     *                'rgb' => '808080'
104
     *            ]
105
     *        ]
106
     * );
107
     * </code>
108
     *
109
     * @param array $styleArray Array containing style information
110
     *
111
     * @return $this
112
     */
113 93
    public function applyFromArray(array $styleArray): static
114
    {
115 93
        if ($this->isSupervisor) {
116 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
117
        } else {
118 93
            if (isset($styleArray['borderStyle'])) {
119 93
                $this->setBorderStyle($styleArray['borderStyle']);
120
            }
121 93
            if (isset($styleArray['color'])) {
122 69
                $this->getColor()->applyFromArray($styleArray['color']);
123
            }
124
        }
125
        $this->updateHashBeforeUse();
126 93
127
        return $this;
128
    }
129
130
    /**
131
     * Get Border style.
132 1050
     */
133
    public function getBorderStyle(): string
134 1050
    {
135 40
        if ($this->isSupervisor) {
136
            return $this->getSharedComponent()->getBorderStyle();
137
        }
138 1050
139
        return $this->borderStyle;
140
    }
141
142
    /**
143
     * Set Border style.
144
     *
145
     * @param bool|string $style When passing a boolean, FALSE equates Border::BORDER_NONE
146
     *                                and TRUE to Border::BORDER_MEDIUM
147
     *
148
     * @return $this
149 851
     */
150
    public function setBorderStyle(bool|string $style): static
151 851
    {
152 1
        if (empty($style)) {
153 851
            $style = self::BORDER_NONE;
154 1
        } elseif (is_bool($style)) {
155
            $style = self::BORDER_MEDIUM;
156
        }
157 851
158 29
        if ($this->isSupervisor) {
159 29
            $styleArray = $this->getStyleArray(['borderStyle' => $style]);
160
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
161 850
        } else {
162
            $this->borderStyle = $style;
163
        }
164 851
        $this->updateHashBeforeUse();
165
166
        return $this;
167
    }
168
169
    /**
170 806
     * Get Border Color.
171
     */
172 806
    public function getColor(): Color
173
    {
174
        return $this->color;
175
    }
176
177
    /**
178
     * Set Border Color.
179
     *
180 19
     * @return $this
181
     */
182
    public function setColor(Color $color): static
183 19
    {
184
        // make sure parameter is a real color and not a supervisor
185 19
        $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
186 18
187 18
        if ($this->isSupervisor) {
188
            $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
189 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
190
        } else {
191
            $this->color = $color;
192 19
        }
193
        $this->updateHashBeforeUse();
194
195
        return $this;
196
    }
197
198
    /**
199
     * Update Hash when something changes.
200 1245
     */
201
    protected function updateHash(): void
202 1245
    {
203 1
        $this->md5Sum = md5(
204
            $this->borderStyle
205
            . $this->color->getHashCode()
206 1245
            . __CLASS__
207 1245
        );
208 1245
        $this->updateMd5Sum = false;
209 1245
    }
210 1245
211
    /**
212
     * Get hash code.
213 14
     *
214
     * @return string Hash code
215 14
     */
216 14
    public function getHashCode(): string
217 14
    {
218
        if ($this->isSupervisor) {
219 14
            return $this->getSharedComponent()->getHashCode();
220
        }
221
222
        if ($this->updateMd5Sum) {
223
            $this->updateHash();
224
        }
225
226
        return $this->md5Sum;
227
    }
228
229
    protected function exportArray1(): array
230
    {
231
        $exportedArray = [];
232
        $this->exportArray2($exportedArray, 'borderStyle', $this->getBorderStyle());
233
        $this->exportArray2($exportedArray, 'color', $this->getColor());
234
235
        return $exportedArray;
236
    }
237
}
238