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