OptionsOutput::draw()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 17
c 0
b 0
f 0
nc 7
nop 2
dl 0
loc 29
ccs 17
cts 17
cp 1
crap 5
rs 9.3888
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 4
    public function __construct(private readonly DateOutputInterface $dateOutput)
15
    {
16 4
    }
17
18 4
    public function draw(ChartOptionsInterface $options, string $optionsName): string
19
    {
20 4
        $this->removeRecursivelyNullValue($options);
21
22
        /* @var array $options */
23 4
        $this->removeRecursivelyEmptyArray($options);
24
25 4
        $options = $this->renameRecursivelyKeys($options);
26
27 4
        $js = "var $optionsName = {";
28 4
        $lastKey = array_key_last($options);
29 4
        foreach ($options as $optionKey => $optionValue) {
30 4
            $js .= '"'.$optionKey.'":';
31
32 4
            if (isset($optionValue['date'])) {
33 1
                $js .= $this->dateOutput->draw(new \DateTime($optionValue['date']));
34 4
            } elseif (in_array($optionKey, ['series', 'vAxes'])) {
35 2
                $js .= json_encode($optionValue, JSON_FORCE_OBJECT);
36
            } else {
37 4
                $js .= json_encode($optionValue);
38
            }
39
40 4
            if ($optionKey != $lastKey) {
41 4
                $js .= ', ';
42
            }
43
        }
44 4
        $js .= "};\n";
45
46 4
        return $js;
47
    }
48
}
49