Passed
Push — master ( d265e3...e69457 )
by Christophe
05:00
created

DataOutput::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arrayToDataTable of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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