Completed
Push — develop ( 8cf911...93ccf7 )
by Adrien
33:03
created

Fill::setFillType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 11
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
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
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
24
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
25
 * @version    ##VERSION##, ##DATE##
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 62
    public function __construct($isSupervisor = false, $isConditional = false)
91
    {
92
        // Supervisor?
93 62
        parent::__construct($isSupervisor);
94
95
        // Initialise values
96 62
        if ($isConditional) {
97 2
            $this->fillType = null;
98
        }
99 62
        $this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
100 62
        $this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
101
102
        // bind parent if we are a supervisor
103 62
        if ($isSupervisor) {
104 62
            $this->startColor->bindParent($this, 'startColor');
105 62
            $this->endColor->bindParent($this, 'endColor');
106
        }
107 62
    }
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
    public function getSharedComponent()
116
    {
117
        return $this->parent->getSharedComponent()->getFill();
118
    }
119
120
    /**
121
     * Build style array from subcomponents
122
     *
123
     * @param array $array
124
     * @return array
125
     */
126 9
    public function getStyleArray($array)
127
    {
128 9
        return ['fill' => $array];
129
    }
130
131
    /**
132
     * Apply styles from array
133
     *
134
     * <code>
135
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
136
     *        array(
137
     *            'type'       => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
138
     *            'rotation'   => 0,
139
     *            'startcolor' => array(
140
     *                'rgb' => '000000'
141
     *            ),
142
     *            'endcolor'   => array(
143
     *                'argb' => 'FFFFFFFF'
144
     *            )
145
     *        )
146
     * );
147
     * </code>
148
     *
149
     * @param   array    $pStyles    Array containing style information
150
     * @throws  \PhpOffice\PhpSpreadsheet\Exception
151
     * @return Fill
152
     */
153 13
    public function applyFromArray($pStyles = null)
154
    {
155 13
        if (is_array($pStyles)) {
156 13
            if ($this->isSupervisor) {
157
                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
158
            } else {
159 13
                if (array_key_exists('type', $pStyles)) {
160 12
                    $this->setFillType($pStyles['type']);
161
                }
162 13
                if (array_key_exists('rotation', $pStyles)) {
163 9
                    $this->setRotation($pStyles['rotation']);
164
                }
165 13
                if (array_key_exists('startcolor', $pStyles)) {
166 10
                    $this->getStartColor()->applyFromArray($pStyles['startcolor']);
167
                }
168 13
                if (array_key_exists('endcolor', $pStyles)) {
169 10
                    $this->getEndColor()->applyFromArray($pStyles['endcolor']);
170
                }
171 13
                if (array_key_exists('color', $pStyles)) {
172 13
                    $this->getStartColor()->applyFromArray($pStyles['color']);
173
                }
174
            }
175
        } else {
176
            throw new \PhpOffice\PhpSpreadsheet\Exception('Invalid style array passed.');
177
        }
178
179 13
        return $this;
180
    }
181
182
    /**
183
     * Get Fill Type
184
     *
185
     * @return string
186
     */
187 60
    public function getFillType()
188
    {
189 60
        if ($this->isSupervisor) {
190
            return $this->getSharedComponent()->getFillType();
191
        }
192
193 60
        return $this->fillType;
194
    }
195
196
    /**
197
     * Set Fill Type
198
     *
199
     * @param string $pValue    Fill type
200
     * @return Fill
201
     */
202 59 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...
203
    {
204 59
        if ($this->isSupervisor) {
205 9
            $styleArray = $this->getStyleArray(['type' => $pValue]);
206 9
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
207
        } else {
208 59
            $this->fillType = $pValue;
209
        }
210
211 59
        return $this;
212
    }
213
214
    /**
215
     * Get Rotation
216
     *
217
     * @return float
218
     */
219 59
    public function getRotation()
220
    {
221 59
        if ($this->isSupervisor) {
222
            return $this->getSharedComponent()->getRotation();
223
        }
224
225 59
        return $this->rotation;
226
    }
227
228
    /**
229
     * Set Rotation
230
     *
231
     * @param float $pValue
232
     * @return Fill
233
     */
234 9 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...
235
    {
236 9
        if ($this->isSupervisor) {
237
            $styleArray = $this->getStyleArray(['rotation' => $pValue]);
238
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
239
        } else {
240 9
            $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...
241
        }
242
243 9
        return $this;
244
    }
245
246
    /**
247
     * Get Start Color
248
     *
249
     * @return Color
250
     */
251 60
    public function getStartColor()
252
    {
253 60
        return $this->startColor;
254
    }
255
256
    /**
257
     * Set Start Color
258
     *
259
     * @param   Color $pValue
260
     * @throws  \PhpOffice\PhpSpreadsheet\Exception
261
     * @return Fill
262
     */
263 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...
264
    {
265
        // make sure parameter is a real color and not a supervisor
266
        $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...
267
268
        if ($this->isSupervisor) {
269
            $styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
270
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
271
        } else {
272
            $this->startColor = $color;
273
        }
274
275
        return $this;
276
    }
277
278
    /**
279
     * Get End Color
280
     *
281
     * @return Color
282
     */
283 60
    public function getEndColor()
284
    {
285 60
        return $this->endColor;
286
    }
287
288
    /**
289
     * Set End Color
290
     *
291
     * @param    Color $pValue
292
     * @throws   \PhpOffice\PhpSpreadsheet\Exception
293
     * @return Fill
294
     */
295 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...
296
    {
297
        // make sure parameter is a real color and not a supervisor
298
        $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...
299
300
        if ($this->isSupervisor) {
301
            $styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
302
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
303
        } else {
304
            $this->endColor = $color;
305
        }
306
307
        return $this;
308
    }
309
310
    /**
311
     * Get hash code
312
     *
313
     * @return string    Hash code
314
     */
315 59
    public function getHashCode()
316
    {
317 59
        if ($this->isSupervisor) {
318
            return $this->getSharedComponent()->getHashCode();
319
        }
320
321 59
        return md5(
322 59
            $this->getFillType() .
323 59
            $this->getRotation() .
324 59
            $this->getStartColor()->getHashCode() .
325 59
            $this->getEndColor()->getHashCode() .
326 59
            __CLASS__
327
        );
328
    }
329
}
330