1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EnergieProduction\Chart; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use EnergieProduction\Subsets; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
Class Chart { |
10
|
|
|
|
11
|
|
|
protected $subsetList; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* [__construct description] |
15
|
|
|
* @param \Illuminate\Support\Collection $subsetList |
16
|
|
|
*/ |
17
|
|
|
public function __construct(Collection $subsetList) |
18
|
|
|
{ |
19
|
|
|
$this->subsetList = $subsetList; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* [addSubset description] |
24
|
|
|
* @param string $subset |
25
|
|
|
* @param \Closure $closure |
26
|
|
|
*/ |
27
|
|
|
public function addSubset($subset, Closure $closure) |
28
|
|
|
{ |
29
|
|
|
if (str_contains($subset, '.')) { |
30
|
|
|
$this->addCascadeSubset($subset, $closure); |
31
|
|
|
} |
32
|
|
|
else { |
33
|
|
|
$this->addSimpleSubset($subset, $closure); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* [render description] |
39
|
|
|
* @return json [description] |
40
|
|
|
*/ |
41
|
|
|
public function render() |
42
|
|
|
{ |
43
|
|
|
$formatedChart = []; |
44
|
|
|
|
45
|
|
|
foreach ($this->subsetList as $subset) { |
46
|
|
|
|
47
|
|
|
$render = $subset->render(); |
48
|
|
|
|
49
|
|
|
if ($subset->cascade) { |
50
|
|
|
|
51
|
|
|
$tmpArray = []; |
52
|
|
|
|
53
|
|
|
$this->dotNotationToArray($tmpArray, $subset->cascade, $render); |
54
|
|
|
|
55
|
|
|
$render = $tmpArray; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$formatedChart = array_merge_recursive($formatedChart, $render); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return json_encode($formatedChart); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* [addSimpleSubset description] |
66
|
|
|
* @param string $subset |
67
|
|
|
* @param \Closure $closure |
68
|
|
|
*/ |
69
|
|
|
protected function addSimpleSubset($subset, Closure $closure) |
70
|
|
|
{ |
71
|
|
|
$subset = $this->makeSubsetClass($subset); |
72
|
|
|
|
73
|
|
|
$this->callFunc($closure, $subset); |
74
|
|
|
|
75
|
|
|
$this->subsetList->push($subset); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* [addCascadeSubset description] |
80
|
|
|
* @param string $cascadeSubsetNotation |
81
|
|
|
* @param \Closure $closure |
82
|
|
|
*/ |
83
|
|
|
protected function addCascadeSubset($cascadeSubsetNotation, Closure $closure) |
84
|
|
|
{ |
85
|
|
|
$subsetsList = explode('.', $cascadeSubsetNotation); |
86
|
|
|
|
87
|
|
|
$subset = $this->makeSubsetClass(end($subsetsList)); |
88
|
|
|
|
89
|
|
|
$subset->setCascade($cascadeSubsetNotation); |
90
|
|
|
|
91
|
|
|
$this->callFunc($closure, $subset); |
92
|
|
|
|
93
|
|
|
$this->subsetList->push($subset); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* [callFunc description] |
98
|
|
|
* @param \Closure $closure |
99
|
|
|
* @param [type] $class |
|
|
|
|
100
|
|
|
* @return [type |
|
|
|
|
101
|
|
|
*/ |
102
|
|
|
protected function callFunc(Closure $closure, $class) |
103
|
|
|
{ |
104
|
|
|
return call_user_func($closure, $class); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* [makeSubsetClass description] |
109
|
|
|
* @param [type] $subset |
|
|
|
|
110
|
|
|
* @return [type] |
|
|
|
|
111
|
|
|
*/ |
112
|
|
|
protected function makeSubsetClass($subset) |
113
|
|
|
{ |
114
|
|
|
$class = "Service\\Subsets\\" . ucfirst($subset); |
115
|
|
|
|
116
|
|
|
return new $class; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* [dotNotationToArray description] |
121
|
|
|
* @param array &$arr |
122
|
|
|
* @param string $path |
123
|
|
|
* @param array $val |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
protected function dotNotationToArray(array &$arr, $path, $val) |
127
|
|
|
{ |
128
|
|
|
$loc = &$arr; |
129
|
|
|
|
130
|
|
|
$path = explode('.', $path); |
131
|
|
|
|
132
|
|
|
array_pop($path); |
133
|
|
|
|
134
|
|
|
foreach($path as $step) |
135
|
|
|
{ |
136
|
|
|
$loc = &$loc[$step]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $loc = $val; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.