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

OptionsOutput::__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\GoogleCharts\Options\ChartOptionsInterface;
6
use CMEN\GoogleChartsBundle\Output\AbstractOptionsOutput;
7
use CMEN\GoogleChartsBundle\Output\DateOutputInterface;
8
9
/**
10
 * @author Christophe Meneses
11
 */
12
class OptionsOutput extends AbstractOptionsOutput
13
{
14
    /** @var DateOutputInterface */
15
    private $dateOutput;
16
17
    /**
18
     * OptionsOutput constructor.
19
     *
20
     * @param DateOutputInterface $dateOutput
21
     */
22 2
    public function __construct(DateOutputInterface $dateOutput)
23
    {
24 2
        $this->dateOutput = $dateOutput;
25 2
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 2
    public function draw(ChartOptionsInterface $options, $optionsName)
31
    {
32 2
        $this->removeRecursivelyNullValue($options);
33
34
        /* @var array $options */
35 2
        $this->removeRecursivelyEmptyArray($options);
36
37 2
        $options = $this->renameRecursivelyKeys($options);
38
39 2
        $js = "var $optionsName = {";
40
41 2
        end($options);
42 2
        $lastKey = key($options);
43 2
        foreach ($options as $optionKey => $optionValue) {
44 2
            $js .= '"'.$optionKey.'":';
45
46 2
            if (isset($optionValue['date'])) {
47 1
                $js .= $this->dateOutput->draw(new \DateTime($optionValue['date']));
48 1
            } else {
49 2
                $js .= json_encode($optionValue);
50
            }
51
52 2
            if ($optionKey != $lastKey) {
53 2
                $js .= ', ';
54 2
            }
55 2
        }
56 2
        $js .= "};\n";
57
58 2
        return $js;
59
    }
60
}
61