Completed
Push — develop ( 257c3e...4e0344 )
by Adrien
26:37
created

DataSeriesValues::multiLevelCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Chart;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
8
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
9
10
class DataSeriesValues
11
{
12
    const DATASERIES_TYPE_STRING = 'String';
13
    const DATASERIES_TYPE_NUMBER = 'Number';
14
15
    private static $dataTypeValues = [
16
        self::DATASERIES_TYPE_STRING,
17
        self::DATASERIES_TYPE_NUMBER,
18
    ];
19
20
    /**
21
     * Series Data Type.
22
     *
23
     * @var string
24
     */
25
    private $dataType;
26
27
    /**
28
     * Series Data Source.
29
     *
30
     * @var string
31
     */
32
    private $dataSource;
33
34
    /**
35
     * Format Code.
36
     *
37
     * @var string
38
     */
39
    private $formatCode;
40
41
    /**
42
     * Series Point Marker.
43
     *
44
     * @var string
45
     */
46
    private $pointMarker;
47
48
    /**
49
     * Point Count (The number of datapoints in the dataseries).
50
     *
51
     * @var int
52
     */
53
    private $pointCount = 0;
54
55
    /**
56
     * Data Values.
57
     *
58
     * @var array of mixed
59
     */
60
    private $dataValues = [];
61
62
    /**
63
     * Fill color.
64
     *
65
     * @var string
66
     */
67
    private $fillColor;
68
69
    /**
70
     * Line Width.
71
     *
72
     * @var int
73
     */
74
    private $lineWidth = 12700;
75
76
    /**
77
     * Create a new DataSeriesValues object.
78
     *
79
     * @param string $dataType
80
     * @param string $dataSource
81
     * @param null|mixed $formatCode
82
     * @param int $pointCount
83
     * @param mixed $dataValues
84
     * @param null|mixed $marker
85
     * @param null|string $fillColor
86
     */
87 19
    public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null, $fillColor = null)
88
    {
89 19
        $this->setDataType($dataType);
90 19
        $this->dataSource = $dataSource;
91 19
        $this->formatCode = $formatCode;
92 19
        $this->pointCount = $pointCount;
93 19
        $this->dataValues = $dataValues;
94 19
        $this->pointMarker = $marker;
95 19
        $this->fillColor = $fillColor;
96 19
    }
97
98
    /**
99
     * Get Series Data Type.
100
     *
101
     * @return string
102
     */
103 1
    public function getDataType()
104
    {
105 1
        return $this->dataType;
106
    }
107
108
    /**
109
     * Set Series Data Type.
110
     *
111
     * @param string $dataType Datatype of this data series
112
     *                                Typical values are:
113
     *                                    DataSeriesValues::DATASERIES_TYPE_STRING
114
     *                                        Normally used for axis point values
115
     *                                    DataSeriesValues::DATASERIES_TYPE_NUMBER
116
     *                                        Normally used for chart data values
117
     *
118
     * @throws Exception
119
     *
120
     * @return DataSeriesValues
121
     */
122 19
    public function setDataType($dataType)
123
    {
124 19
        if (!in_array($dataType, self::$dataTypeValues)) {
125 1
            throw new Exception('Invalid datatype for chart data series values');
126
        }
127 19
        $this->dataType = $dataType;
128
129 19
        return $this;
130
    }
131
132
    /**
133
     * Get Series Data Source (formula).
134
     *
135
     * @return string
136
     */
137 13
    public function getDataSource()
138
    {
139 13
        return $this->dataSource;
140
    }
141
142
    /**
143
     * Set Series Data Source (formula).
144
     *
145
     * @param string $dataSource
146
     *
147
     * @return DataSeriesValues
148
     */
149
    public function setDataSource($dataSource)
150
    {
151
        $this->dataSource = $dataSource;
152
153
        return $this;
154
    }
155
156
    /**
157
     * Get Point Marker.
158
     *
159
     * @return string
160
     */
161 14
    public function getPointMarker()
162
    {
163 14
        return $this->pointMarker;
164
    }
165
166
    /**
167
     * Set Point Marker.
168
     *
169
     * @param string $marker
170
     *
171
     * @return DataSeriesValues
172
     */
173
    public function setPointMarker($marker)
174
    {
175
        $this->pointMarker = $marker;
176
177
        return $this;
178
    }
179
180
    /**
181
     * Get Series Format Code.
182
     *
183
     * @return string
184
     */
185 13
    public function getFormatCode()
186
    {
187 13
        return $this->formatCode;
188
    }
189
190
    /**
191
     * Set Series Format Code.
192
     *
193
     * @param string $formatCode
194
     *
195
     * @return DataSeriesValues
196
     */
197
    public function setFormatCode($formatCode)
198
    {
199
        $this->formatCode = $formatCode;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Get Series Point Count.
206
     *
207
     * @return int
208
     */
209 14
    public function getPointCount()
210
    {
211 14
        return $this->pointCount;
212
    }
213
214
    /**
215
     * Get fill color.
216
     *
217
     * @return string HEX color
218
     */
219 13
    public function getFillColor()
220
    {
221 13
        return $this->fillColor;
222
    }
223
224
    /**
225
     * Set fill color for series.
226
     *
227
     * @param string $color HEX color
228
     *
229
     * @return   DataSeriesValues
230
     */
231
    public function setFillColor($color)
232
    {
233
        if (!preg_match('/^[a-f0-9]{6}$/i', $color)) {
234
            throw new Exception('Invalid hex color for chart series');
235
        }
236
        $this->fillColor = $color;
237
238
        return $this;
239
    }
240
241
    /**
242
     * Get line width for series.
243
     *
244
     * @return int
245
     */
246 5
    public function getLineWidth()
247
    {
248 5
        return $this->lineWidth;
249
    }
250
251
    /**
252
     * Set line width for the series.
253
     *
254
     * @param int $width
255
     *
256
     * @return DataSeriesValues
257
     */
258 2
    public function setLineWidth($width)
259
    {
260 2
        $minWidth = 12700;
261 2
        $this->lineWidth = max($minWidth, $width);
262
263 2
        return $this;
264
    }
265
266
    /**
267
     * Identify if the Data Series is a multi-level or a simple series.
268
     *
269
     * @return null|bool
270
     */
271 13
    public function isMultiLevelSeries()
272
    {
273 13
        if (count($this->dataValues) > 0) {
274 13
            return is_array($this->dataValues[0]);
275
        }
276
277
        return null;
278
    }
279
280
    /**
281
     * Return the level count of a multi-level Data Series.
282
     *
283
     * @return int
284
     */
285 2
    public function multiLevelCount()
286
    {
287 2
        $levelCount = 0;
288 2
        foreach ($this->dataValues as $dataValueSet) {
289 2
            $levelCount = max($levelCount, count($dataValueSet));
290
        }
291
292 2
        return $levelCount;
293
    }
294
295
    /**
296
     * Get Series Data Values.
297
     *
298
     * @return array of mixed
299
     */
300 14
    public function getDataValues()
301
    {
302 14
        return $this->dataValues;
303
    }
304
305
    /**
306
     * Get the first Series Data value.
307
     *
308
     * @return mixed
309
     */
310 1
    public function getDataValue()
311
    {
312 1
        $count = count($this->dataValues);
313 1
        if ($count == 0) {
314 1
            return null;
315 1
        } elseif ($count == 1) {
316 1
            return $this->dataValues[0];
317
        }
318
319
        return $this->dataValues;
320
    }
321
322
    /**
323
     * Set Series Data Values.
324
     *
325
     * @param array $dataValues
326
     *
327
     * @return DataSeriesValues
328
     */
329
    public function setDataValues($dataValues)
330
    {
331
        $this->dataValues = Functions::flattenArray($dataValues);
332
        $this->pointCount = count($dataValues);
333
334
        return $this;
335
    }
336
337 14
    public function refresh(Worksheet $worksheet, $flatten = true)
338
    {
339 14
        if ($this->dataSource !== null) {
0 ignored issues
show
introduced by
The condition $this->dataSource !== null can never be false.
Loading history...
340 14
            $calcEngine = Calculation::getInstance($worksheet->getParent());
341 14
            $newDataValues = Calculation::unwrapResult(
342 14
                $calcEngine->_calculateFormulaValue(
343 14
                    '=' . $this->dataSource,
344 14
                    null,
345 14
                    $worksheet->getCell('A1')
346
                )
347
            );
348 14
            if ($flatten) {
349 14
                $this->dataValues = Functions::flattenArray($newDataValues);
1 ignored issue
show
Bug introduced by
It seems like $newDataValues can also be of type string; however, parameter $array of PhpOffice\PhpSpreadsheet...nctions::flattenArray() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

349
                $this->dataValues = Functions::flattenArray(/** @scrutinizer ignore-type */ $newDataValues);
Loading history...
350 14
                foreach ($this->dataValues as &$dataValue) {
351 14
                    if ((!empty($dataValue)) && ($dataValue[0] == '#')) {
352 14
                        $dataValue = 0.0;
353
                    }
354
                }
355 14
                unset($dataValue);
356
            } else {
357 14
                $cellRange = explode('!', $this->dataSource);
358 14
                if (count($cellRange) > 1) {
359 14
                    list(, $cellRange) = $cellRange;
360
                }
361
362 14
                $dimensions = Coordinate::rangeDimension(str_replace('$', '', $cellRange));
0 ignored issues
show
Bug introduced by
str_replace('$', '', $cellRange) of type string[] is incompatible with the type string expected by parameter $pRange of PhpOffice\PhpSpreadsheet...inate::rangeDimension(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

362
                $dimensions = Coordinate::rangeDimension(/** @scrutinizer ignore-type */ str_replace('$', '', $cellRange));
Loading history...
363 14
                if (($dimensions[0] == 1) || ($dimensions[1] == 1)) {
364 13
                    $this->dataValues = Functions::flattenArray($newDataValues);
365
                } else {
366 3
                    $newArray = array_values(array_shift($newDataValues));
1 ignored issue
show
Bug introduced by
It seems like $newDataValues can also be of type string; however, parameter $array of array_shift() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

366
                    $newArray = array_values(array_shift(/** @scrutinizer ignore-type */ $newDataValues));
Loading history...
367 3
                    foreach ($newArray as $i => $newDataSet) {
368 3
                        $newArray[$i] = [$newDataSet];
369
                    }
370
371 3
                    foreach ($newDataValues as $newDataSet) {
372 3
                        $i = 0;
373 3
                        foreach ($newDataSet as $newDataVal) {
374 3
                            array_unshift($newArray[$i++], $newDataVal);
375
                        }
376
                    }
377 3
                    $this->dataValues = $newArray;
378
                }
379
            }
380 14
            $this->pointCount = count($this->dataValues);
381
        }
382 14
    }
383
}
384