Passed
Push — master ( b07b23...350b39 )
by Christophe
08:37 queued 05:48
created

AbstractChartOutput::checkElementsId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.0312
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
     * AbstractChartOutput constructor.
29
     *
30
     * @param string $version
31
     * @param string $language
32
     */
33 13
    public function __construct($version, $language)
34
    {
35 13
        $this->version = $version;
36 13
        $this->language = $language;
37 13
    }
38
39
    /**
40
     * @param string $language
41
     *
42
     * @return $this
43
     */
44
    public function setLanguage($language)
45
    {
46
        $this->language = $language;
47
48
        return $this;
49
    }
50
51
    /**
52
     * Checks if all elements ID are string and same number as charts.
53
     *
54
     * @param Chart[]  $charts
55
     * @param string[] $elementsID
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 1
        }
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
     * @throws GoogleChartsException
78
     */
79 3
    protected function checkChartsTypes(array $charts)
80
    {
81 3
        foreach ($charts as $chart) {
82 3
            if (!$chart instanceof Chart) {
83 1
                throw new GoogleChartsException('An array of Chart is expected.');
84
            }
85 3
        }
86 2
    }
87
}
88