Completed
Push — develop ( f99eb8...c5339b )
by Adrien
31:45
created

DataSeriesValues   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 323
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 70.45%

Importance

Changes 0
Metric Value
dl 0
loc 323
ccs 62
cts 88
cp 0.7045
rs 9
c 0
b 0
f 0
wmc 35
lcom 1
cbo 5

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getDataType() 0 4 1
A setDataType() 0 9 2
A getDataSource() 0 4 1
A setDataSource() 0 10 2
A getPointMarker() 0 4 1
A setPointMarker() 0 6 1
A getFormatCode() 0 4 1
A setFormatCode() 0 6 1
A getPointCount() 0 4 1
A isMultiLevelSeries() 0 8 2
A multiLevelCount() 0 9 2
A getDataValues() 0 4 1
A getDataValue() 0 11 3
A setDataValues() 0 11 2
A stripNulls() 0 4 1
C refresh() 0 46 12
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Chart;
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 DataSeriesValues
28
{
29
    const DATASERIES_TYPE_STRING = 'String';
30
    const DATASERIES_TYPE_NUMBER = 'Number';
31
32
    private static $dataTypeValues = [
33
        self::DATASERIES_TYPE_STRING,
34
        self::DATASERIES_TYPE_NUMBER,
35
    ];
36
37
    /**
38
     * Series Data Type.
39
     *
40
     * @var string
41
     */
42
    private $dataType;
43
44
    /**
45
     * Series Data Source.
46
     *
47
     * @var string
48
     */
49
    private $dataSource;
50
51
    /**
52
     * Format Code.
53
     *
54
     * @var string
55
     */
56
    private $formatCode;
57
58
    /**
59
     * Series Point Marker.
60
     *
61
     * @var string
62
     */
63
    private $pointMarker;
64
65
    /**
66
     * Point Count (The number of datapoints in the dataseries).
67
     *
68
     * @var int
69
     */
70
    private $pointCount = 0;
71
72
    /**
73
     * Data Values.
74
     *
75
     * @var array of mixed
76
     */
77
    private $dataValues = [];
78
79
    /**
80
     * Create a new DataSeriesValues object.
81
     *
82
     * @param mixed $dataType
83
     * @param string $dataSource
84
     * @param null|mixed $formatCode
85
     * @param mixed $pointCount
86
     * @param mixed $dataValues
87
     * @param null|mixed $marker
88
     */
89 17
    public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null)
90
    {
91 17
        $this->setDataType($dataType);
92 17
        $this->dataSource = $dataSource;
93 17
        $this->formatCode = $formatCode;
94 17
        $this->pointCount = $pointCount;
95 17
        $this->dataValues = $dataValues;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dataValues of type * is incompatible with the declared type array of property $dataValues.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
96 17
        $this->pointMarker = $marker;
97 17
    }
98
99
    /**
100
     * Get Series Data Type.
101
     *
102
     * @return string
103
     */
104 1
    public function getDataType()
105
    {
106 1
        return $this->dataType;
107
    }
108
109
    /**
110
     * Set Series Data Type.
111
     *
112
     * @param string $dataType Datatype of this data series
113
     *                                Typical values are:
114
     *                                    \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues::DATASERIES_TYPE_STRING
115
     *                                        Normally used for axis point values
116
     *                                    \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues::DATASERIES_TYPE_NUMBER
117
     *                                        Normally used for chart data values
118
     *
119
     * @throws Exception
120
     *
121
     * @return DataSeriesValues
122
     */
123 17
    public function setDataType($dataType = self::DATASERIES_TYPE_NUMBER)
124
    {
125 17
        if (!in_array($dataType, self::$dataTypeValues)) {
126 1
            throw new Exception('Invalid datatype for chart data series values');
127
        }
128 17
        $this->dataType = $dataType;
129
130 17
        return $this;
131
    }
132
133
    /**
134
     * Get Series Data Source (formula).
135
     *
136
     * @return string
137
     */
138 13
    public function getDataSource()
139
    {
140 13
        return $this->dataSource;
141
    }
142
143
    /**
144
     * Set Series Data Source (formula).
145
     *
146
     * @param string $dataSource
147
     * @param mixed $refreshDataValues
148
     *
149
     * @return DataSeriesValues
150
     */
151
    public function setDataSource($dataSource = null, $refreshDataValues = true)
152
    {
153
        $this->dataSource = $dataSource;
154
155
        if ($refreshDataValues) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
156
            //    TO DO
157
        }
158
159
        return $this;
160
    }
161
162
    /**
163
     * Get Point Marker.
164
     *
165
     * @return string
166
     */
167 13
    public function getPointMarker()
168
    {
169 13
        return $this->pointMarker;
170
    }
171
172
    /**
173
     * Set Point Marker.
174
     *
175
     * @param string $marker
176
     *
177
     * @return DataSeriesValues
178
     */
179
    public function setPointMarker($marker = null)
180
    {
181
        $this->pointMarker = $marker;
182
183
        return $this;
184
    }
185
186
    /**
187
     * Get Series Format Code.
188
     *
189
     * @return string
190
     */
191 12
    public function getFormatCode()
192
    {
193 12
        return $this->formatCode;
194
    }
195
196
    /**
197
     * Set Series Format Code.
198
     *
199
     * @param string $formatCode
200
     *
201
     * @return DataSeriesValues
202
     */
203
    public function setFormatCode($formatCode = null)
204
    {
205
        $this->formatCode = $formatCode;
206
207
        return $this;
208
    }
209
210
    /**
211
     * Get Series Point Count.
212
     *
213
     * @return int
214
     */
215 13
    public function getPointCount()
216
    {
217 13
        return $this->pointCount;
218
    }
219
220
    /**
221
     * Identify if the Data Series is a multi-level or a simple series.
222
     *
223
     * @return bool|null
224
     */
225 13
    public function isMultiLevelSeries()
226
    {
227 13
        if (count($this->dataValues) > 0) {
228 13
            return is_array($this->dataValues[0]);
229
        }
230
231
        return null;
232
    }
233
234
    /**
235
     * Return the level count of a multi-level Data Series.
236
     *
237
     * @return int
238
     */
239 2
    public function multiLevelCount()
240
    {
241 2
        $levelCount = 0;
242 2
        foreach ($this->dataValues as $dataValueSet) {
243 2
            $levelCount = max($levelCount, count($dataValueSet));
244
        }
245
246 2
        return $levelCount;
247
    }
248
249
    /**
250
     * Get Series Data Values.
251
     *
252
     * @return array of mixed
253
     */
254 13
    public function getDataValues()
255
    {
256 13
        return $this->dataValues;
257
    }
258
259
    /**
260
     * Get the first Series Data value.
261
     *
262
     * @return mixed
263
     */
264
    public function getDataValue()
265
    {
266
        $count = count($this->dataValues);
267
        if ($count == 0) {
268
            return null;
269
        } elseif ($count == 1) {
270
            return $this->dataValues[0];
271
        }
272
273
        return $this->dataValues;
274
    }
275
276
    /**
277
     * Set Series Data Values.
278
     *
279
     * @param array $dataValues
280
     * @param bool $refreshDataSource
281
     *                    TRUE - refresh the value of dataSource based on the values of $dataValues
282
     *                    FALSE - don't change the value of dataSource
283
     *
284
     * @return DataSeriesValues
285
     */
286
    public function setDataValues($dataValues = [], $refreshDataSource = true)
287
    {
288
        $this->dataValues = \PhpOffice\PhpSpreadsheet\Calculation\Functions::flattenArray($dataValues);
289
        $this->pointCount = count($dataValues);
290
291
        if ($refreshDataSource) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
292
            //    TO DO
293
        }
294
295
        return $this;
296
    }
297
298
    private function stripNulls($var)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
299
    {
300
        return $var !== null;
301
    }
302
303 13
    public function refresh(\PhpOffice\PhpSpreadsheet\Worksheet $worksheet, $flatten = true)
304
    {
305 13
        if ($this->dataSource !== null) {
306 13
            $calcEngine = \PhpOffice\PhpSpreadsheet\Calculation::getInstance($worksheet->getParent());
307 13
            $newDataValues = \PhpOffice\PhpSpreadsheet\Calculation::unwrapResult(
308 13
                $calcEngine->_calculateFormulaValue(
309 13
                    '=' . $this->dataSource,
310 13
                    null,
311 13
                    $worksheet->getCell('A1')
312
                )
313
            );
314 13
            if ($flatten) {
315 13
                $this->dataValues = \PhpOffice\PhpSpreadsheet\Calculation\Functions::flattenArray($newDataValues);
316 13
                foreach ($this->dataValues as &$dataValue) {
317 13
                    if ((!empty($dataValue)) && ($dataValue[0] == '#')) {
318
                        $dataValue = 0.0;
319
                    }
320
                }
321 13
                unset($dataValue);
322
            } else {
323 13
                $cellRange = explode('!', $this->dataSource);
324 13
                if (count($cellRange) > 1) {
325 13
                    list(, $cellRange) = $cellRange;
326
                }
327
328 13
                $dimensions = \PhpOffice\PhpSpreadsheet\Cell::rangeDimension(str_replace('$', '', $cellRange));
329 13
                if (($dimensions[0] == 1) || ($dimensions[1] == 1)) {
330 12
                    $this->dataValues = \PhpOffice\PhpSpreadsheet\Calculation\Functions::flattenArray($newDataValues);
331
                } else {
332 2
                    $newArray = array_values(array_shift($newDataValues));
333 2
                    foreach ($newArray as $i => $newDataSet) {
334 2
                        $newArray[$i] = [$newDataSet];
335
                    }
336
337 2
                    foreach ($newDataValues as $newDataSet) {
338 2
                        $i = 0;
339 2
                        foreach ($newDataSet as $newDataVal) {
340 2
                            array_unshift($newArray[$i++], $newDataVal);
341
                        }
342
                    }
343 2
                    $this->dataValues = $newArray;
344
                }
345
            }
346 13
            $this->pointCount = count($this->dataValues);
347
        }
348 13
    }
349
}
350