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

Fill::getStartColor()   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
class Fill extends Supervisor
6
{
7
    // Fill types
8
    const FILL_NONE = 'none';
9
    const FILL_SOLID = 'solid';
10
    const FILL_GRADIENT_LINEAR = 'linear';
11
    const FILL_GRADIENT_PATH = 'path';
12
    const FILL_PATTERN_DARKDOWN = 'darkDown';
13
    const FILL_PATTERN_DARKGRAY = 'darkGray';
14
    const FILL_PATTERN_DARKGRID = 'darkGrid';
15
    const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
16
    const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
17
    const FILL_PATTERN_DARKUP = 'darkUp';
18
    const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
19
    const FILL_PATTERN_GRAY0625 = 'gray0625';
20
    const FILL_PATTERN_GRAY125 = 'gray125';
21
    const FILL_PATTERN_LIGHTDOWN = 'lightDown';
22
    const FILL_PATTERN_LIGHTGRAY = 'lightGray';
23
    const FILL_PATTERN_LIGHTGRID = 'lightGrid';
24
    const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
25
    const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
26
    const FILL_PATTERN_LIGHTUP = 'lightUp';
27
    const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
28
    const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
29
30
    public ?int $startcolorIndex = null;
31
32
    public ?int $endcolorIndex = null;
33
34
    /**
35
     * Fill type.
36
     */
37
    protected ?string $fillType = self::FILL_NONE;
38
39
    /**
40
     * Rotation.
41
     */
42
    protected float $rotation = 0.0;
43
44
    /**
45
     * Start color.
46
     */
47
    protected Color $startColor;
48
49
    /**
50
     * End color.
51
     */
52
    protected Color $endColor;
53
54
    private bool $colorChanged = false;
55
56
    /**
57
     * Create a new Fill.
58
     *
59
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
60
     *                                    Leave this value at default unless you understand exactly what
61
     *                                        its ramifications are
62
     * @param bool $isConditional Flag indicating if this is a conditional style or not
63
     *                                    Leave this value at default unless you understand exactly what
64
     *                                        its ramifications are
65
     */
66 10569
    public function __construct(bool $isSupervisor = false, bool $isConditional = false)
67
    {
68
        // Supervisor?
69 10569
        parent::__construct($isSupervisor);
70
71
        // Initialise values
72 10569
        if ($isConditional) {
73 402
            $this->fillType = null;
74
        }
75 10569
        $this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
76 10569
        $this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
77
78
        // bind parent if we are a supervisor
79 10569
        if ($isSupervisor) {
80 10505
            $this->startColor->bindParent($this, 'startColor');
81 10505
            $this->endColor->bindParent($this, 'endColor');
82
        }
83
    }
84
85
    /**
86
     * Get the shared style component for the currently active cell in currently active sheet.
87
     * Only used for style supervisor.
88
     */
89 46
    public function getSharedComponent(): self
90
    {
91
        /** @var Style $parent */
92 46
        $parent = $this->parent;
93
94 46
        return $parent->getSharedComponent()->getFill();
95
    }
96
97
    /**
98
     * Build style array from subcomponents.
99
     */
100 40
    public function getStyleArray(array $array): array
101
    {
102 40
        return ['fill' => $array];
103
    }
104
105
    /**
106
     * Apply styles from array.
107
     *
108
     * <code>
109
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
110
     *     [
111
     *         'fillType' => Fill::FILL_GRADIENT_LINEAR,
112
     *         'rotation' => 0.0,
113
     *         'startColor' => [
114
     *             'rgb' => '000000'
115
     *         ],
116
     *         'endColor' => [
117
     *             'argb' => 'FFFFFFFF'
118
     *         ]
119
     *     ]
120
     * );
121
     * </code>
122
     *
123
     * @param array $styleArray Array containing style information
124
     *
125
     * @return $this
126
     */
127 132
    public function applyFromArray(array $styleArray): static
128
    {
129 132
        if ($this->isSupervisor) {
130 2
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
131
        } else {
132 132
            if (isset($styleArray['fillType'])) {
133 110
                $this->setFillType($styleArray['fillType']);
134
            }
135 132
            if (isset($styleArray['rotation'])) {
136 43
                $this->setRotation($styleArray['rotation']);
137
            }
138 132
            if (isset($styleArray['startColor'])) {
139 75
                $this->getStartColor()->applyFromArray($styleArray['startColor']);
140
            }
141 132
            if (isset($styleArray['endColor'])) {
142 67
                $this->getEndColor()->applyFromArray($styleArray['endColor']);
143
            }
144 132
            if (isset($styleArray['color'])) {
145 41
                $this->getStartColor()->applyFromArray($styleArray['color']);
146 41
                $this->getEndColor()->applyFromArray($styleArray['color']);
147
            }
148
        }
149
150 132
        return $this;
151
    }
152
153
    /**
154
     * Get Fill Type.
155
     */
156 1789
    public function getFillType(): ?string
157
    {
158 1789
        if ($this->isSupervisor) {
159 34
            return $this->getSharedComponent()->getFillType();
160
        }
161
162 1789
        return $this->fillType;
163
    }
164
165
    /**
166
     * Set Fill Type.
167
     *
168
     * @param string $fillType Fill type, see self::FILL_*
169
     *
170
     * @return $this
171
     */
172 986
    public function setFillType(string $fillType): static
173
    {
174 986
        if ($this->isSupervisor) {
175 39
            $styleArray = $this->getStyleArray(['fillType' => $fillType]);
176 39
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
177
        } else {
178 986
            $this->fillType = $fillType;
179
        }
180
        $this->updateHashBeforeUse();
181 986
182
        return $this;
183
    }
184
185
    /**
186
     * Get Rotation.
187 1249
     */
188
    public function getRotation(): float
189 1249
    {
190 16
        if ($this->isSupervisor) {
191
            return $this->getSharedComponent()->getRotation();
192
        }
193 1249
194
        return $this->rotation;
195
    }
196
197
    /**
198
     * Set Rotation.
199
     *
200
     * @return $this
201 45
     */
202
    public function setRotation(float $angleInDegrees): static
203 45
    {
204 14
        if ($this->isSupervisor) {
205 14
            $styleArray = $this->getStyleArray(['rotation' => $angleInDegrees]);
206
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
207 45
        } else {
208
            $this->rotation = $angleInDegrees;
209
        }
210 45
        $this->updateHashBeforeUse();
211
212
        return $this;
213
    }
214
215
    /**
216 855
     * Get Start Color.
217
     */
218 855
    public function getStartColor(): Color
219
    {
220
        return $this->startColor;
221
    }
222
223
    /**
224
     * Set Start Color.
225
     *
226 19
     * @return $this
227
     */
228 19
    public function setStartColor(Color $color): static
229
    {
230 19
        $this->colorChanged = true;
231
        // make sure parameter is a real color and not a supervisor
232 19
        $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
233 18
234 18
        if ($this->isSupervisor) {
235
            $styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
236 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
237
        } else {
238
            $this->startColor = $color;
239 19
        }
240
        $this->updateHashBeforeUse();
241
242
        return $this;
243
    }
244
245 846
    /**
246
     * Get End Color.
247 846
     */
248
    public function getEndColor(): Color
249
    {
250
        return $this->endColor;
251
    }
252
253
    /**
254
     * Set End Color.
255 13
     *
256
     * @return $this
257 13
     */
258
    public function setEndColor(Color $color): static
259 13
    {
260
        $this->colorChanged = true;
261 13
        // make sure parameter is a real color and not a supervisor
262 12
        $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
263 12
264
        if ($this->isSupervisor) {
265 1
            $styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
266
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
267
        } else {
268 13
            $this->endColor = $color;
269
        }
270
        $this->updateHashBeforeUse();
271 1249
272
        return $this;
273 1249
    }
274 8
275
    public function getColorsChanged(): bool
276 1249
    {
277
        if ($this->isSupervisor) {
278
            $changed = $this->getSharedComponent()->colorChanged;
279 1249
        } else {
280
            $changed = $this->colorChanged;
281
        }
282
283
        return $changed || $this->startColor->getHasChanged() || $this->endColor->getHasChanged();
284
    }
285
286
    protected function updateHash(): void
287 1245
    {
288
        $this->md5Sum = md5(
289 1245
            $this->getFillType()
290 1
            . $this->getRotation()
291
            . ($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '')
292
            . ($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '')
293
            . ((string) $this->getColorsChanged())
294
            . __CLASS__
295 1245
        );
296 1245
    }
297 1245
298 1245
    /**
299 1245
     * Get hash code.
300 1245
     *
301 1245
     * @return string Hash code
302 1245
     */
303
    public function getHashCode(): string
304
    {
305 14
        if ($this->isSupervisor) {
306
            return $this->getSharedComponent()->getHashCode();
307 14
        }
308 14
309 14
        if ($this->updateMd5Sum) {
310 14
            $this->updateHash();
311 3
        }
312 3
313
        return $this->md5Sum;
314
    }
315 14
316
    protected function exportArray1(): array
317
    {
318
        $exportedArray = [];
319
        $this->exportArray2($exportedArray, 'fillType', $this->getFillType());
320
        $this->exportArray2($exportedArray, 'rotation', $this->getRotation());
321
        if ($this->getColorsChanged()) {
322
            $this->exportArray2($exportedArray, 'endColor', $this->getEndColor());
323
            $this->exportArray2($exportedArray, 'startColor', $this->getStartColor());
324
        }
325
326
        return $exportedArray;
327
    }
328
}
329