Passed
Pull Request — master (#38)
by Christophe
05:14
created

AbstractChartOutput::__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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace CMEN\GoogleChartsBundle\Output;
4
5
use CMEN\GoogleChartsBundle\Exception\GoogleChartsException;
6
use CMEN\GoogleChartsBundle\GoogleCharts\Chart;
7
8
/**
9
 * @author Christophe Meneses
10
 */
11
abstract class AbstractChartOutput implements ChartOutputInterface
12
{
13
    /**
14
     * Version of Google Charts used.
15
     *
16
     * @var string
17
     */
18
    protected $version;
19
20
    /**
21
     * Locale to customize currencies, dates, and numbers.
22
     *
23
     * @var string
24
     */
25
    protected $language;
26
27
    /**
28
     * @param string $version
29
     * @param string $language
30
     */
31 14
    public function __construct($version, $language)
32
    {
33 14
        $this->version = $version;
34 14
        $this->language = $language;
35 14
    }
36
37
    /**
38
     * @param string $language
39
     *
40
     * @return $this
41
     */
42
    public function setLanguage($language)
43
    {
44
        $this->language = $language;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Checks if all elements ID are string and same number as charts.
51
     *
52
     * @param Chart[]  $charts
53
     * @param string[] $elementsID
54
     *
55
     * @return void
56
     *
57
     * @throws GoogleChartsException
58
     */
59 2
    protected function checkElementsId(array $charts, array $elementsID)
60
    {
61 2
        if (count($charts) != count($elementsID)) {
62 1
            throw new GoogleChartsException('Array charts and array HTML elements ID do not have the same number of element.');
63
        }
64
65 1
        foreach ($elementsID as $elementId) {
66 1
            if (!is_string($elementId)) {
67 1
                throw new GoogleChartsException('A string is expected for HTML element ID.');
68
            }
69
        }
70
    }
71
72
    /**
73
     * Checks if all elements of an array are instances of Chart. Throws an exception if not.
74
     *
75
     * @param Chart[] $charts
76
     *
77
     * @return void
78
     *
79
     * @throws GoogleChartsException
80
     */
81 4
    protected function checkChartsTypes(array $charts)
82
    {
83 4
        foreach ($charts as $chart) {
84 4
            if (!$chart instanceof Chart) {
85 4
                throw new GoogleChartsException('An array of Chart is expected.');
86
            }
87
        }
88 3
    }
89
}
90