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
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
    public function __construct(bool $isSupervisor = false, bool $isConditional = false)
67
    {
68
        // Supervisor?
69
        parent::__construct($isSupervisor);
70
 
71
        // Initialise values
72
        if ($isConditional) {
73
            $this->fillType = null;
74
        }
75
        $this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
76
        $this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
77
 
78
        // bind parent if we are a supervisor
79
        if ($isSupervisor) {
80
            $this->startColor->bindParent($this, 'startColor');
81
            $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
    public function getSharedComponent(): self
90
    {
91
        /** @var Style $parent */
92
        $parent = $this->parent;
93
 
94
        return $parent->getSharedComponent()->getFill();
95
    }
96
 
97
    /**
98
     * Build style array from subcomponents.
99
     */
100
    public function getStyleArray(array $array): array
101
    {
102
        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
    public function applyFromArray(array $styleArray): static
128
    {
129
        if ($this->isSupervisor) {
130
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
131
        } else {
132
            if (isset($styleArray['fillType'])) {
133
                $this->setFillType($styleArray['fillType']);
134
            }
135
            if (isset($styleArray['rotation'])) {
136
                $this->setRotation($styleArray['rotation']);
137
            }
138
            if (isset($styleArray['startColor'])) {
139
                $this->getStartColor()->applyFromArray($styleArray['startColor']);
140
            }
141
            if (isset($styleArray['endColor'])) {
142
                $this->getEndColor()->applyFromArray($styleArray['endColor']);
143
            }
144
            if (isset($styleArray['color'])) {
145
                $this->getStartColor()->applyFromArray($styleArray['color']);
146
                $this->getEndColor()->applyFromArray($styleArray['color']);
147
            }
148
        }
149
 
150
        return $this;
151
    }
152
 
153
    /**
154
     * Get Fill Type.
155
     */
156
    public function getFillType(): ?string
157
    {
158
        if ($this->isSupervisor) {
159
            return $this->getSharedComponent()->getFillType();
160
        }
161
 
162
        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
    public function setFillType(string $fillType): static
173
    {
174
        if ($this->isSupervisor) {
175
            $styleArray = $this->getStyleArray(['fillType' => $fillType]);
176
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
177
        } else {
178
            $this->fillType = $fillType;
179
        }
180
 
181
        return $this;
182
    }
183
 
184
    /**
185
     * Get Rotation.
186
     */
187
    public function getRotation(): float
188
    {
189
        if ($this->isSupervisor) {
190
            return $this->getSharedComponent()->getRotation();
191
        }
192
 
193
        return $this->rotation;
194
    }
195
 
196
    /**
197
     * Set Rotation.
198
     *
199
     * @return $this
200
     */
201
    public function setRotation(float $angleInDegrees): static
202
    {
203
        if ($this->isSupervisor) {
204
            $styleArray = $this->getStyleArray(['rotation' => $angleInDegrees]);
205
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
206
        } else {
207
            $this->rotation = $angleInDegrees;
208
        }
209
 
210
        return $this;
211
    }
212
 
213
    /**
214
     * Get Start Color.
215
     */
216
    public function getStartColor(): Color
217
    {
218
        return $this->startColor;
219
    }
220
 
221
    /**
222
     * Set Start Color.
223
     *
224
     * @return $this
225
     */
226
    public function setStartColor(Color $color): static
227
    {
228
        $this->colorChanged = true;
229
        // make sure parameter is a real color and not a supervisor
230
        $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
231
 
232
        if ($this->isSupervisor) {
233
            $styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
234
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
235
        } else {
236
            $this->startColor = $color;
237
        }
238
 
239
        return $this;
240
    }
241
 
242
    /**
243
     * Get End Color.
244
     */
245
    public function getEndColor(): Color
246
    {
247
        return $this->endColor;
248
    }
249
 
250
    /**
251
     * Set End Color.
252
     *
253
     * @return $this
254
     */
255
    public function setEndColor(Color $color): static
256
    {
257
        $this->colorChanged = true;
258
        // make sure parameter is a real color and not a supervisor
259
        $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
260
 
261
        if ($this->isSupervisor) {
262
            $styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
263
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
264
        } else {
265
            $this->endColor = $color;
266
        }
267
 
268
        return $this;
269
    }
270
 
271
    public function getColorsChanged(): bool
272
    {
273
        if ($this->isSupervisor) {
274
            $changed = $this->getSharedComponent()->colorChanged;
275
        } else {
276
            $changed = $this->colorChanged;
277
        }
278
 
279
        return $changed || $this->startColor->getHasChanged() || $this->endColor->getHasChanged();
280
    }
281
 
282
    /**
283
     * Get hash code.
284
     *
285
     * @return string Hash code
286
     */
287
    public function getHashCode(): string
288
    {
289
        if ($this->isSupervisor) {
290
            return $this->getSharedComponent()->getHashCode();
291
        }
292
 
293
        // Note that we don't care about colours for fill type NONE, but could have duplicate NONEs with
294
        //  different hashes if we don't explicitly prevent this
295
        return md5(
296
            $this->getFillType()
297
            . $this->getRotation()
298
            . ($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '')
299
            . ($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '')
300
            . ((string) $this->getColorsChanged())
301
            . __CLASS__
302
        );
303
    }
304
 
305
    protected function exportArray1(): array
306
    {
307
        $exportedArray = [];
308
        $this->exportArray2($exportedArray, 'fillType', $this->getFillType());
309
        $this->exportArray2($exportedArray, 'rotation', $this->getRotation());
310
        if ($this->getColorsChanged()) {
311
            $this->exportArray2($exportedArray, 'endColor', $this->getEndColor());
312
            $this->exportArray2($exportedArray, 'startColor', $this->getStartColor());
313
        }
314
 
315
        return $exportedArray;
316
    }
317
}