AbstractChartOutput::checkElementsId()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
nc 4
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 4
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
    public function __construct(
14
        /**
15
         * Version of Google Charts used.
16
         */
17
        protected string $version,
18
        /**
19
         * Locale to customize currencies, dates, and numbers.
20
         */
21
        protected string $language,
22
    ) {
23 14
    }
24
25
    /**
26
     * @return $this
27
     */
28
    public function setLanguage(string $language): ChartOutputInterface
29
    {
30
        $this->language = $language;
31
32
        return $this;
33
    }
34
35
    /**
36
     * Checks if all elements ID are string and same number as charts.
37
     *
38
     * @param Chart[]  $charts
39
     * @param string[] $elementsID
40
     *
41
     * @throws GoogleChartsException
42
     */
43 2
    protected function checkElementsId(array $charts, array $elementsID): void
44
    {
45 2
        if (count($charts) != count($elementsID)) {
46 1
            throw new GoogleChartsException('Array charts and array HTML elements ID do not have the same number of element.');
47
        }
48
49 1
        foreach ($elementsID as $elementId) {
50 1
            if (!is_string($elementId)) {
51 1
                throw new GoogleChartsException('A string is expected for HTML element ID.');
52
            }
53
        }
54
    }
55
56
    /**
57
     * Checks if all elements of an array are instances of Chart. Throws an exception if not.
58
     *
59
     * @param Chart[] $charts
60
     *
61
     * @throws GoogleChartsException
62
     */
63 4
    protected function checkChartsTypes(array $charts): void
64
    {
65 4
        foreach ($charts as $chart) {
66 4
            if (!$chart instanceof Chart) {
67 1
                throw new GoogleChartsException('An array of Chart is expected.');
68
            }
69
        }
70
    }
71
}
72