-
Notifications
You must be signed in to change notification settings - Fork 143
/
DataTable.php
executable file
·578 lines (526 loc) · 17.1 KB
/
DataTable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<?php namespace Khill\Lavacharts\Configs;
/**
* DataTable Object
*
* The DataTable object is used to hold the data passed into a visualization.
* A DataTable is a basic two-dimensional table. All data in each column must
* have the same data type. Each column has a descriptor that includes its data
* type, a label for that column (which might be displayed by a visualization),
* and an ID, which can be used to refer to a specific column (as an alternative
* to using column indexes). The DataTable object also supports a map of
* arbitrary properties assigned to a specific value, a row, a column, or the
* whole DataTable. Visualizations can use these to support additional features;
* for example, the Table visualization uses custom properties to let you assign
* arbitrary class names or styles to individual cells.
*
*
* @package Lavacharts
* @subpackage Configs
* @author Kevin Hill <kevinkhill@gmail.com>
* @copyright (c) 2015, KHill Designs
* @link http://github.com/kevinkhill/lavacharts GitHub Repository Page
* @link http://lavacharts.com Official Docs Site
* @license http://opensource.org/licenses/MIT MIT
*/
use Carbon\Carbon;
use Khill\Lavacharts\Utils;
use Khill\Lavacharts\Formats\Format;
use Khill\Lavacharts\Exceptions\InvalidDate;
use Khill\Lavacharts\Exceptions\InvalidCellCount;
use Khill\Lavacharts\Exceptions\InvalidConfigValue;
use Khill\Lavacharts\Exceptions\InvalidConfigProperty;
use Khill\Lavacharts\Exceptions\InvalidColumnDefinition;
use Khill\Lavacharts\Exceptions\InvalidColumnIndex;
use Khill\Lavacharts\Exceptions\InvalidRowDefinition;
use Khill\Lavacharts\Exceptions\InvalidRowProperty;
class DataTable
{
/**
* Timezone for dealing with datetime and Carbon objects
*
* @var string
*/
public $timezone;
/**
* Timezone for dealing with datetime and Carbon objects
*
* @var string
*/
public $dateTimeFormat;
/**
* Holds the information defining the columns.
*
* @var array
*/
private $cols = array();
/**
* Holds the information defining each row.
*
* @var array
*/
private $rows = array();
/**
* Holds the formatting information for each column.
*
* @var array
*/
private $formats = array();
/**
* Valid column types.
*
* @var array
*/
private $colCellTypes = array(
'string',
'number',
'bool',
'date',
'datetime',
'timeofday'
);
/**
* Valid column descriptions
*
* @var array
*/
private $colCellDesc = array(
'type',
'label',
'id',
'role',
'pattern'
);
public function __construct($timezone = null)
{
$this->setTimezone($timezone);
}
public function setTimezone($timezone)
{
// get PHP.ini setting
$default_timezone = date_default_timezone_get();
if (Utils::nonEmptyString($timezone)) {
$this->timezone = $timezone;
} elseif (Utils::nonEmptyString($default_timezone)) {
$this->timezone = $default_timezone;
} else {
$this->timezone = 'America/Los_Angeles';
}
date_default_timezone_set($this->timezone);
return $this;
}
/**
* Sets the format to be used by Carbon::createFromFormat()
*
* This method is used to set the format to be used to parse a string
* passed to a cell in a date column, that was parsed incorrectly by Carbon::parse()
*/
public function setDateTimeFormat($dateTimeFormat)
{
if (Utils::nonEmptyString($dateTimeFormat)) {
$this->dateTimeFormat = $dateTimeFormat;
} else {
throw new InvalidConfigValue(
__FUNCTION__,
'string'
);
}
return $this;
}
/**
* Adds a column to the DataTable
*
* First signature has the following parameters:
* type - A string with the data type of the values of the column.
* The type can be one of the following: 'string' 'number' 'bool' 'date'
* 'datetime' 'timeofday'.
*
* optLabel - [Optional] A string with the label of the column. The column
* label is typically displayed as part of the visualization, for example as
* a column header in a table, or as a legend label in a pie chart. If not
* value is specified, an empty string is assigned.
* optId - [Optional] A string with a unique identifier for the column. If
* not value is specified, an empty string is assigned.
*
*
* @param string|array Column type or an array describing the column.
* @param string A label for the column. (Optional)
* @param string An ID for the column. (Optional)
* @param Format A column formatter object. (Optional)
* @throws InvalidConfigValue
* @throws InvalidConfigProperty
* @return DataTable
*/
public function addColumn($typeOrDescArr, $optLabel = '', $optId = '', Format $formatter = null)
{
if (is_array($typeOrDescArr)) {
$this->addColumnFromArray($typeOrDescArr);
} elseif (is_string($typeOrDescArr)) {
$this->addColumnFromStrings($typeOrDescArr, $optLabel, $optId, $formatter);
} else {
throw new InvalidConfigValue(
__FUNCTION__,
'string or array'
);
}
return $this;
}
/**
* Adds multiple columns to the DataTable
*
* @param array $arrOfCols Array of columns to batch add to the DataTable.
* @throws InvalidConfigValue
* @return DataTable
*/
public function addColumns($arrOfCols)
{
if (Utils::arrayIsMulti($arrOfCols)) {
foreach ($arrOfCols as $col) {
$this->addColumnFromArray($col);
}
} else {
throw new InvalidConfigValue(
__FUNCTION__,
'array of arrays'
);
}
return $this;
}
/**
* Supplemental function to add a string column with less params.
*
* @param string A label for the column.
* @param Format A column formatter object. (Optional)
* @throws InvalidConfigValue
* @throws InvalidConfigProperty
* @return DataTable
*/
public function addStringColumn($optLabel, Format $formatter = null)
{
return $this->addColumn('string', $optLabel, 'col_' . count($this->cols) + 1, $formatter);
}
/**
* Supplemental function to add a date column with less params.
*
* @param string A label for the column.
* @param Format A column formatter object. (Optional)
* @throws InvalidConfigValue
* @throws InvalidConfigProperty
* @return DataTable
*/
public function addDateColumn($optLabel, Format $formatter = null)
{
return $this->addColumn('date', $optLabel, 'col_' . count($this->cols) + 1, $formatter);
}
/**
* Supplemental function to add a number column with less params.
*
* @param string A label for the column.
* @param Format A column formatter object. (Optional)
* @throws InvalidConfigValue
* @throws InvalidConfigProperty
* @return DataTable
*/
public function addNumberColumn($optLabel, Format $formatter = null)
{
return $this->addColumn('number', $optLabel, 'col_' . count($this->cols) + 1, $formatter);
}
/**
* Supplemental function to add columns from an array.
*
* @param array $colDefArray
* @throws InvalidColumnDefinition
* @return DataTable
*/
private function addColumnFromArray($colDefArray)
{
if (Utils::arrayValuesCheck($colDefArray, 'string') && Utils::between(1, count($colDefArray), 4, true)) {
call_user_func_array(array($this, 'addColumnFromStrings'), $colDefArray);
} else {
throw new InvalidColumnDefinition($colDefArray);
}
return $this;
}
/**
* Supplemental function to add columns from strings.
*
* @param array $type
* @param array $label
* @param array $id
* @param array $format
* @throws InvalidConfigValue
* @return DataTable
*/
private function addColumnFromStrings($type, $label = '', $id = '', $format = null)
{
$colIndex = $this->getNumberOfColumns();
if (in_array($type, $this->colCellTypes)) {
if (Utils::nonEmptyString($type)) {
$descArray['type'] = $type;
} else {
throw new InvalidConfigValue(
__FUNCTION__,
'string'
);
}
if (Utils::nonEmptyString($label)) {
$descArray['label'] = $label;
}
if (Utils::nonEmptyString($id)) {
$descArray['id'] = $id;
}
if (! is_null($format)) {
$this->formats[$colIndex] = $format;
}
} else {
throw new InvalidConfigProperty(
__FUNCTION__,
'string',
Utils::arrayToPipedString($this->colCellTypes)
);
}
$this->cols[$colIndex] = $descArray;
}
/**
* Sets the format of the column.
*
* @param int $colIndex
* @param Format $formatter
* @throws InvalidColumnIndex
* @return DataTable
*/
public function formatColumn($colIndex, Format $formatter)
{
if (is_int($colIndex) && $colIndex > 0) {
$this->formats[$colIndex] = $formatter->toArray();
} else {
throw new InvalidColumnIndex($colIndex);
}
return $this;
}
/**
* Sets the format of multiple columns.
*
* @param array $colFormatArr
* @return DataTable
*/
public function formatColumns($colFormatArr)
{
if (is_array($colFormatArr) && ! empty($colFormatArr)) {
foreach ($colFormatArr as $index => $format) {
if (is_subclass_of($format, 'Format')) {
$this->formats[$colIndex] = $format->toArray();
}
}
}
return $this;
}
/**
* Add a row to the DataTable
*
* Each cell in the table is described by an array with the following properties:
*
* v [Optional] The cell value. The data type should match the column data type.
* If null, the whole object should be empty and have neither v nor f properties.
*
* f [Optional] A string version of the v value, formatted for display. The
* values should match, so if you specify Date(2008, 0, 1) for v, you should
* specify "January 1, 2008" or some such string for this property. This value
* is not checked against the v value. The visualization will not use this value
* for calculation, only as a label for display. If omitted, a string version
* of v will be used.
*
* p [Optional] An object that is a map of custom values applied to the cell.
* These values can be of any JavaScript type. If your visualization supports
* any cell-level properties, it will describe them; otherwise, this property
* will be ignored. Example: p:{style: 'border: 1px solid green;'}.
*
*
* Cells in the row array should be in the same order as their column descriptions
* in cols. To indicate a null cell, you can specify null, leave a blank for
* a cell in an array, or omit trailing array members. So, to indicate a row
* with null for the first two cells, you would specify [null, null, {cell_val}].
*
* @param mixed $optCellArray Array of values or DataCells.
*
* @throws InvalidCellCount
*
* @return DataTable
*/
public function addRow($optCellArray = null)
{
$props = array(
'v',
'f',
'p'
);
if (is_null($optCellArray)) {
for ($a = 0; $a < count($this->cols); $a++) {
$tmp[] = array('v' => null);
}
$this->rows[] = array('c' => $tmp);
} else {
if (is_array($optCellArray)) {
if (Utils::arrayIsMulti($optCellArray)) {
foreach ($optCellArray as $prop => $value) {
if (in_array($value, $props)) {
$rowVals[] = array($prop => $value);
} else {
throw new InvalidRowProperty;
}
}
$this->rows[] = array('c' => $rowVals);
} else {
if (count($optCellArray) <= count($this->cols)) {
for ($b = 0; $b < count($this->cols); $b++) {
if (isset($optCellArray[$b])) {
if ($this->cols[$b]['type'] == 'date') {
$rowVals[] = array('v' => $this->parseDate($optCellArray[$b]));
} else {
$rowVals[] = array('v' => $optCellArray[$b]);
}
} else {
$rowVals[] = array('v' => null);
}
}
$this->rows[] = array('c' => $rowVals);
} else {
throw new InvalidCellCount(count($optCellArray), count($this->cols));
}
}
} else {
throw new InvalidRowDefinition($optCellArray);
}
}
return $this;
}
/**
* Adds multiple rows to the DataTable.
*
* @see addRow()
* @param array Multi-dimensional array of rows.
*
* @return DataTable
*/
public function addRows($arrayOfRows)
{
if (Utils::arrayIsMulti($arrayOfRows)) {
foreach ($arrayOfRows as $row) {
$this->addRow($row);
}
} else {
throw new InvalidConfigValue(
__FUNCTION__,
'array of arrays'
);
}
return $this;
}
/**
* Returns the number of columns in the DataTable
*
* @return int
*/
public function getNumberOfColumns()
{
return count($this->cols);
}
/**
* Returns the number of rows in the DataTable
*
* @return int
*/
public function getNumberOfRows()
{
return count($this->rows);
}
/**
* Returns the column array from the DataTable
*
* @return array
*/
public function getColumns()
{
return $this->cols;
}
/**
* Returns the rows array from the DataTable
*
* @return array
*/
public function getRows()
{
return $this->rows;
}
/**
* Returns the formats array from the DataTable
*
* @return array
*/
public function getFormats()
{
return $this->formats;
}
/**
* Boolean value if there are defined formatters
*
* @return bool
*/
public function hasFormats()
{
return count($this->formats) > 0 ? true : false;
}
/**
* Convert the DataTable to JSON
*
* @return string JSON representation of the DataTable.
*/
public function toJson()
{
return json_encode(array(
'cols' => $this->cols,
'rows' => $this->rows,
));
}
/**
* Either passes the Carbon instance or parses a datetime string.
*
* @param Carbon|string $date
* @return string Javscript date declaration
*/
private function parseDate($date)
{
if (is_a($date, 'Carbon\Carbon')) {
$carbonDate = $date;
} elseif (Utils::nonEmptyString($date)) {
try {
if (Utils::nonEmptyString($this->dateTimeFormat)) {
$carbonDate = Carbon::createFromFormat($this->dateTimeFormat, $date);
} else {
$carbonDate = Carbon::parse($date);
}
} catch (\Exception $e) {
throw new InvalidDate;
}
} else {
throw new InvalidDate;
}
return $this->carbonToJsString($carbonDate);
}
/**
* Outputs the Carbon object as a valid javascript Date string.
*
* @return string Javscript date declaration
*/
private function carbonToJsString(Carbon $c)
{
return sprintf(
'Date(%d,%d,%d,%d,%d,%d)',
isset($c->year) ? $c->year : 'null',
isset($c->month) ? $c->month - 1 : 'null', //silly javascript
isset($c->day) ? $c->day : 'null',
isset($c->hour) ? $c->hour : 'null',
isset($c->minute) ? $c->minute : 'null',
isset($c->second) ? $c->second : 'null'
);
}
}