Completed
Push — develop ( 3560f1...5fce89 )
by Adrien
21:58 queued 15:06
created

Fill::getStyleArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
22
 * @category   PhpSpreadsheet
23
 *
24
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26
 */
27
class Fill extends Supervisor implements \PhpOffice\PhpSpreadsheet\IComparable
28
{
29
    /* Fill types */
30
    const FILL_NONE = 'none';
31
    const FILL_SOLID = 'solid';
32
    const FILL_GRADIENT_LINEAR = 'linear';
33
    const FILL_GRADIENT_PATH = 'path';
34
    const FILL_PATTERN_DARKDOWN = 'darkDown';
35
    const FILL_PATTERN_DARKGRAY = 'darkGray';
36
    const FILL_PATTERN_DARKGRID = 'darkGrid';
37
    const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
38
    const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
39
    const FILL_PATTERN_DARKUP = 'darkUp';
40
    const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
41
    const FILL_PATTERN_GRAY0625 = 'gray0625';
42
    const FILL_PATTERN_GRAY125 = 'gray125';
43
    const FILL_PATTERN_LIGHTDOWN = 'lightDown';
44
    const FILL_PATTERN_LIGHTGRAY = 'lightGray';
45
    const FILL_PATTERN_LIGHTGRID = 'lightGrid';
46
    const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
47
    const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
48
    const FILL_PATTERN_LIGHTUP = 'lightUp';
49
    const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
50
    const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
51
52
    /**
53
     * Fill type.
54
     *
55
     * @var string
56
     */
57
    protected $fillType = self::FILL_NONE;
58
59
    /**
60
     * Rotation.
61
     *
62
     * @var float
63
     */
64
    protected $rotation = 0;
65
66
    /**
67
     * Start color.
68
     *
69
     * @var Color
70
     */
71
    protected $startColor;
72
73
    /**
74
     * End color.
75
     *
76
     * @var Color
77
     */
78
    protected $endColor;
79
80
    /**
81
     * Create a new Fill.
82
     *
83
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
84
     *                                    Leave this value at default unless you understand exactly what
85
     *                                        its ramifications are
86
     * @param bool $isConditional Flag indicating if this is a conditional style or not
87
     *                                    Leave this value at default unless you understand exactly what
88
     *                                        its ramifications are
89
     */
90 73
    public function __construct($isSupervisor = false, $isConditional = false)
91
    {
92
        // Supervisor?
93 73
        parent::__construct($isSupervisor);
94
95
        // Initialise values
96 73
        if ($isConditional) {
97 2
            $this->fillType = null;
98
        }
99 73
        $this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
100 73
        $this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
101
102
        // bind parent if we are a supervisor
103 73
        if ($isSupervisor) {
104 73
            $this->startColor->bindParent($this, 'startColor');
105 73
            $this->endColor->bindParent($this, 'endColor');
106
        }
107 73
    }
108
109
    /**
110
     * Get the shared style component for the currently active cell in currently active sheet.
111
     * Only used for style supervisor.
112
     *
113
     * @return Fill
114
     */
115 1
    public function getSharedComponent()
116
    {
117 1
        return $this->parent->getSharedComponent()->getFill();
118
    }
119
120
    /**
121
     * Build style array from subcomponents.
122
     *
123
     * @param array $array
124
     *
125
     * @return array
126
     */
127 13
    public function getStyleArray($array)
128
    {
129 13
        return ['fill' => $array];
130
    }
131
132
    /**
133
     * Apply styles from array.
134
     *
135
     * <code>
136
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
137
     *        array(
138
     *            'type'       => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
139
     *            'rotation'   => 0,
140
     *            'startcolor' => array(
141
     *                'rgb' => '000000'
142
     *            ),
143
     *            'endcolor'   => array(
144
     *                'argb' => 'FFFFFFFF'
145
     *            )
146
     *        )
147
     * );
148
     * </code>
149
     *
150
     * @param array $pStyles Array containing style information
151
     *
152
     * @throws \PhpOffice\PhpSpreadsheet\Exception
153
     *
154
     * @return Fill
155
     */
156 17
    public function applyFromArray($pStyles = null)
157
    {
158 17
        if (is_array($pStyles)) {
159 17
            if ($this->isSupervisor) {
160
                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
161
            } else {
162 17
                if (isset($pStyles['type'])) {
163 17
                    $this->setFillType($pStyles['type']);
164
                }
165 17
                if (isset($pStyles['rotation'])) {
166 12
                    $this->setRotation($pStyles['rotation']);
167
                }
168 17
                if (isset($pStyles['startcolor'])) {
169 14
                    $this->getStartColor()->applyFromArray($pStyles['startcolor']);
170
                }
171 17
                if (isset($pStyles['endcolor'])) {
172 13
                    $this->getEndColor()->applyFromArray($pStyles['endcolor']);
173
                }
174 17
                if (isset($pStyles['color'])) {
175 4
                    $this->getStartColor()->applyFromArray($pStyles['color']);
176 17
                    $this->getEndColor()->applyFromArray($pStyles['color']);
177
                }
178
            }
179
        } else {
180
            throw new \PhpOffice\PhpSpreadsheet\Exception('Invalid style array passed.');
181
        }
182
183 17
        return $this;
184
    }
185
186
    /**
187
     * Get Fill Type.
188
     *
189
     * @return string
190
     */
191 71
    public function getFillType()
192
    {
193 71
        if ($this->isSupervisor) {
194 1
            return $this->getSharedComponent()->getFillType();
195
        }
196
197 71
        return $this->fillType;
198
    }
199
200
    /**
201
     * Set Fill Type.
202
     *
203
     * @param string $pValue Fill type
204
     *
205
     * @return Fill
206
     */
207 63 View Code Duplication
    public function setFillType($pValue = self::FILL_NONE)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
    {
209 63
        if ($this->isSupervisor) {
210 13
            $styleArray = $this->getStyleArray(['type' => $pValue]);
211 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
212
        } else {
213 63
            $this->fillType = $pValue;
214
        }
215
216 63
        return $this;
217
    }
218
219
    /**
220
     * Get Rotation.
221
     *
222
     * @return float
223
     */
224 69
    public function getRotation()
225
    {
226 69
        if ($this->isSupervisor) {
227
            return $this->getSharedComponent()->getRotation();
228
        }
229
230 69
        return $this->rotation;
231
    }
232
233
    /**
234
     * Set Rotation.
235
     *
236
     * @param float $pValue
237
     *
238
     * @return Fill
239
     */
240 12 View Code Duplication
    public function setRotation($pValue = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
    {
242 12
        if ($this->isSupervisor) {
243
            $styleArray = $this->getStyleArray(['rotation' => $pValue]);
244
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
245
        } else {
246 12
            $this->rotation = $pValue;
0 ignored issues
show
Documentation Bug introduced by
The property $rotation was declared of type double, but $pValue is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
247
        }
248
249 12
        return $this;
250
    }
251
252
    /**
253
     * Get Start Color.
254
     *
255
     * @return Color
256
     */
257 70
    public function getStartColor()
258
    {
259 70
        return $this->startColor;
260
    }
261
262
    /**
263
     * Set Start Color.
264
     *
265
     * @param Color $pValue
266
     *
267
     * @throws \PhpOffice\PhpSpreadsheet\Exception
268
     *
269
     * @return Fill
270
     */
271 1 View Code Duplication
    public function setStartColor(Color $pValue = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
272
    {
273
        // make sure parameter is a real color and not a supervisor
274 1
        $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
0 ignored issues
show
Bug introduced by
It seems like $pValue is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
275
276 1
        if ($this->isSupervisor) {
277 1
            $styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
278 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
279
        } else {
280
            $this->startColor = $color;
281
        }
282
283 1
        return $this;
284
    }
285
286
    /**
287
     * Get End Color.
288
     *
289
     * @return Color
290
     */
291 70
    public function getEndColor()
292
    {
293 70
        return $this->endColor;
294
    }
295
296
    /**
297
     * Set End Color.
298
     *
299
     * @param Color $pValue
300
     *
301
     * @throws \PhpOffice\PhpSpreadsheet\Exception
302
     *
303
     * @return Fill
304
     */
305 View Code Duplication
    public function setEndColor(Color $pValue = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
306
    {
307
        // make sure parameter is a real color and not a supervisor
308
        $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
0 ignored issues
show
Bug introduced by
It seems like $pValue is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
309
310
        if ($this->isSupervisor) {
311
            $styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
312
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
313
        } else {
314
            $this->endColor = $color;
315
        }
316
317
        return $this;
318
    }
319
320
    /**
321
     * Get hash code.
322
     *
323
     * @return string Hash code
324
     */
325 69
    public function getHashCode()
326
    {
327 69
        if ($this->isSupervisor) {
328
            return $this->getSharedComponent()->getHashCode();
329
        }
330
331 69
        return md5(
332 69
            $this->getFillType() .
333 69
            $this->getRotation() .
334 69
            $this->getStartColor()->getHashCode() .
335 69
            $this->getEndColor()->getHashCode() .
336 69
            __CLASS__
337
        );
338
    }
339
}
340