|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CMEN\GoogleChartsBundle\Output\Javascript; |
|
4
|
|
|
|
|
5
|
|
|
use CMEN\GoogleChartsBundle\Exception\GoogleChartsException; |
|
6
|
|
|
use CMEN\GoogleChartsBundle\GoogleCharts\Data; |
|
7
|
|
|
use CMEN\GoogleChartsBundle\Output\DataOutputInterface; |
|
8
|
|
|
use CMEN\GoogleChartsBundle\Output\DateOutputInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Christophe Meneses |
|
12
|
|
|
*/ |
|
13
|
|
|
class DataOutput implements DataOutputInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var DateOutputInterface */ |
|
16
|
|
|
private $dateOutput; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* DataOutput constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param DateOutputInterface $dateOutput |
|
22
|
|
|
*/ |
|
23
|
3 |
|
public function __construct(DateOutputInterface $dateOutput) |
|
24
|
|
|
{ |
|
25
|
3 |
|
$this->dateOutput = $dateOutput; |
|
26
|
3 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function draw(Data $data, $dataName) |
|
32
|
|
|
{ |
|
33
|
3 |
|
$arrayToDataTable = $data->getArrayToDataTable(); |
|
34
|
|
|
|
|
35
|
3 |
|
if (!$arrayToDataTable) { |
|
|
|
|
|
|
36
|
1 |
|
throw new GoogleChartsException( |
|
37
|
|
|
'There is no data for chart. Use method setArrayToDataTable() to provide data.' |
|
38
|
1 |
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
$js = "var $dataName = new google.visualization.arrayToDataTable(["; |
|
42
|
|
|
|
|
43
|
2 |
|
end($arrayToDataTable); |
|
44
|
2 |
|
$lastKeyRow = key($arrayToDataTable); |
|
45
|
2 |
|
foreach ($data->getArrayToDataTable() as $keyRow => $row) { |
|
46
|
2 |
|
$js .= '['; |
|
47
|
|
|
|
|
48
|
2 |
|
end($row); |
|
49
|
2 |
|
$lastKeyValue = key($row); |
|
50
|
2 |
|
foreach ($row as $key => $value) { |
|
51
|
2 |
|
if ($value instanceof \DateTimeInterface) { |
|
52
|
1 |
|
$js .= $this->dateOutput->draw($value); |
|
53
|
1 |
|
} else { |
|
54
|
2 |
|
$js .= json_encode($value); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
if ($key != $lastKeyValue) { |
|
58
|
2 |
|
$js .= ', '; |
|
59
|
2 |
|
} |
|
60
|
2 |
|
} |
|
61
|
2 |
|
$js .= ']'; |
|
62
|
|
|
|
|
63
|
2 |
|
if ($keyRow != $lastKeyRow) { |
|
64
|
2 |
|
$js .= ', '; |
|
65
|
2 |
|
} |
|
66
|
2 |
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
$data->isFirstRowIsData() ? $js .= '], true);' : $js .= '], false);'; |
|
69
|
|
|
|
|
70
|
2 |
|
return $js; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.