Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 213
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
StructureData
0.00% covered (danger)
0.00%
0 / 213
0.00% covered (danger)
0.00%
0 / 1
6972
0.00% covered (danger)
0.00%
0 / 1
 parse
0.00% covered (danger)
0.00%
0 / 213
0.00% covered (danger)
0.00%
0 / 1
6972
1<?php
2
3namespace App\Models;
4
5class StructureData
6{
7    function parse(array $rows, $groupByFilter, $aggregatedBy): array {
8        $structuredData = [];
9
10        if($groupByFilter == 1){
11            foreach ($rows as $row) {
12                $year = $row->year ?? null;
13                $month = $row->month ?? null;
14                $week = $row->week ?? null;
15                $commercial = $row->commercial ?? null;
16                $budgetType = $row->budget_type ?? null;
17    
18                $dynamicData = [];
19                foreach ($row as $key => $value) {
20                    if (!in_array($key, ['year', 'month', 'week', 'commercial', 'budget_type'])) {
21                        $dynamicData[$key] = $value;
22                    }
23                }
24    
25                if ($year !== null) {
26                    if (!isset($structuredData[$year])) {
27    
28                        $structuredData[$year] = $dynamicData + [
29                            'year' => $year,
30                            'commercials' => [],
31                        ];
32                    }
33    
34                    if ($commercial !== null) {
35
36                        if($aggregatedBy == 3){
37                            if (!isset($structuredData[$year]['commercials'][$commercial])) {
38                                $structuredData[$year]['commercials'][$commercial] = $dynamicData + [
39                                    'commercial' => $commercial,
40                                    'budget_types' => []
41                                ];
42                            }
43                            
44                            if ($budgetType !== null) {
45                                if (!isset($structuredData[$year]['commercials'][$commercial]['budget_types'][$budgetType])) {
46                                    $structuredData[$year]['commercials'][$commercial]['budget_types'][$budgetType] = $dynamicData + [
47                                        "name" => $budgetType
48                                    ];
49                                }
50                            }
51                        }else{
52                            if (!isset($structuredData[$year]['commercials'][$commercial])) {
53                                $structuredData[$year]['commercials'][$commercial] = $dynamicData + [
54                                    'commercial' => $commercial,
55                                    'months' => []
56                                ];
57                            }
58                        } 
59    
60                        if ($month !== null) {
61                            if (!isset($structuredData[$year]['commercials'][$commercial]['months'][$month])) {
62                                $structuredData[$year]['commercials'][$commercial]['months'][$month] = $dynamicData + [
63                                    'month' => $month,
64                                    'weeks' => [],
65                                ];
66                            }
67    
68                            if ($week !== null) {                            
69                                if (!isset($structuredData[$year]['commercials'][$commercial]['months'][$month]['weeks'][$week])) {
70    
71                                    $structuredData[$year]['commercials'][$commercial]['months'][$month]['weeks'][$week] = $dynamicData + [
72                                        'week' => $week,
73                                        "budget_types" => []
74                                    ];
75                                }
76                            }
77    
78                            if ($budgetType !== null) {
79                                if (!isset($structuredData[$year]['commercials'][$commercial]['months'][$month]['weeks'][$week]['budget_types'][$budgetType])) {
80                                    $structuredData[$year]['commercials'][$commercial]['months'][$month]['weeks'][$week]['budget_types'][$budgetType] = $dynamicData + [
81                                        "name" => $row->budget_type
82                                    ];
83                                }
84                            }
85                        }
86                    }
87                }
88            }
89    
90            foreach ($structuredData as &$yearData) {
91                
92                foreach ($yearData['commercials'] as &$commercialData) {
93                    
94                    if($aggregatedBy == 3){
95                        if(isset($commercialData['budget_types'])){
96                            $commercialData['budget_types'] = array_values($commercialData['budget_types']);
97                        }                        
98                    }
99
100                    if(isset($commercialData['months'])){
101                        $commercialData['months'] = array_values($commercialData['months']);
102
103                            foreach ($commercialData['months'] as &$monthData) {
104                            $monthData['weeks'] = array_values($monthData['weeks']);     
105                            foreach ($monthData['weeks'] as &$weekData) {
106                                $weekData['budget_types'] = array_values($weekData['budget_types']);
107                            }
108                        }
109                    }                    
110                }
111                $yearData['commercials'] = array_values($yearData['commercials']);
112            }
113    
114            return array_values($structuredData);
115        }
116
117        if($groupByFilter == 2){
118            foreach ($rows as $row) {
119                $year = $row->year ?? null;
120                $month = $row->month ?? null;
121                $week = $row->week ?? null;
122                $commercial = $row->commercial ?? null;
123                $budgetType = $row->budget_type ?? null;
124    
125                $dynamicData = [];
126                foreach ($row as $key => $value) {
127                    if (!in_array($key, ['year', 'month', 'week', 'commercial', 'budget_type'])) {
128                        $dynamicData[$key] = $value;
129                    }
130                }
131    
132                if ($year !== null) {
133                    if (!isset($structuredData[$year])) {
134    
135                        $structuredData[$year] = $dynamicData + [
136                            'year' => $year,
137                            'months' => [],
138                        ];
139                    }
140    
141                    if ($month !== null) {
142
143                        if($aggregatedBy == 2){
144                            if (!isset($structuredData[$year]['months'][$month])) {
145                                $structuredData[$year]['months'][$month] = $dynamicData + [
146                                    'month' => $month,                                    
147                                    'commercials' => [],
148                                ];
149                            }
150
151                            if ($commercial !== null) {                            
152                                if (!isset($structuredData[$year]['months'][$month]['commercials'][$commercial])) {
153                                    $structuredData[$year]['months'][$month]['commercials'][$commercial] = $dynamicData + [
154                                        'commercial' => $commercial,
155                                        "budget_types" => []
156                                    ];
157                                }
158
159                                if ($budgetType !== null) {
160                                    if (!isset($structuredData[$year]['months'][$month]['commercials'][$commercial]['budget_types'][$budgetType])) {
161                                        $structuredData[$year]['months'][$month]['commercials'][$commercial]['budget_types'][$budgetType] = $dynamicData + [
162                                            "name" => $row->budget_type
163                                        ];
164                                    }
165                                }
166                            }
167
168                        }elseif($aggregatedBy == 1){
169
170                            if (!isset($structuredData[$year]['months'][$month])) {
171                                $structuredData[$year]['months'][$month] = $dynamicData + [
172                                    'month' => $month,
173                                    'weeks' => [],
174                                ];                                
175                            }
176
177                            if($week !== null){
178                                if (!isset($structuredData[$year]['months'][$month]['weeks'][$week])) {
179                                    $structuredData[$year]['months'][$month]['weeks'][$week] = $dynamicData + [
180                                        'week' => $week,
181                                        'commercials' => [],
182                                    ];
183                                }
184
185                                if ($commercial !== null) {                            
186                                    if (!isset($structuredData[$year]['months'][$month]['weeks'][$week]['commercials'][$commercial])) {
187                                        $structuredData[$year]['months'][$month]['weeks'][$week]['commercials'][$commercial] = $dynamicData + [
188                                            'commercial' => $commercial,
189                                            "budget_types" => []
190                                        ];
191                                    }
192
193                                    if ($budgetType !== null) {
194                                        if (!isset($structuredData[$year]['months'][$month]['weeks'][$week]['commercials'][$commercial]['budget_types'][$budgetType])) {
195                                            $structuredData[$year]['months'][$month]['weeks'][$week]['commercials'][$commercial]['budget_types'][$budgetType] = $dynamicData + [
196                                                "name" => $row->budget_type
197                                            ];
198                                        }
199                                    }
200                                }
201                            }
202                        
203                        }else{
204                            if ($commercial !== null) {                            
205                                if (!isset($structuredData[$year]['months'][$month]['commercials'][$commercial])) {
206                                    $structuredData[$year]['months'][$month]['commercials'][$commercial] = $dynamicData + [
207                                        'commercial' => $commercial,
208                                        "budget_types" => []
209                                    ];
210                                }
211
212                                if ($budgetType !== null) {
213                                    if (!isset($structuredData[$year]['months'][$month]['commercials'][$commercial]['budget_types'][$budgetType])) {
214                                        $structuredData[$year]['months'][$month]['commercials'][$commercial]['budget_types'][$budgetType] = $dynamicData + [
215                                            "name" => $row->budget_type
216                                        ];
217                                    }
218                                }
219                            }
220                        } 
221                    }
222                    
223                    if($aggregatedBy == 3){
224                        if ($commercial !== null) {                            
225                            if (!isset($structuredData[$year]['commercials'][$commercial])) {
226                                $structuredData[$year]['commercials'][$commercial] = $dynamicData + [
227                                    'commercial' => $commercial,
228                                    "budget_types" => []
229                                ];
230                            }
231                        }
232
233                        if ($budgetType !== null) {
234                            if (!isset($structuredData[$year]['commercials'][$commercial]['budget_types'][$budgetType])) {
235                                $structuredData[$year]['commercials'][$commercial]['budget_types'][$budgetType] = $dynamicData + [
236                                    "name" => $row->budget_type
237                                ];
238                            }
239                        }
240                    }
241                }
242            }
243    
244            foreach ($structuredData as &$yearData) {
245
246                foreach ($yearData['months'] as &$monthData) {
247                    if($aggregatedBy  == 1){
248                        $monthData['weeks'] = array_values($monthData['weeks']);     
249                        foreach ($monthData['weeks'] as &$weekData) {
250                            $weekData['commercials'] = array_values($weekData['commercials']);   
251                            foreach ($weekData['commercials'] as &$commercialData) {
252                                $commercialData['budget_types'] = array_values($commercialData['budget_types']);
253                            }                        
254                        }
255                    }
256
257                    if($aggregatedBy == 2){
258                        $monthData['commercials'] = array_values($monthData['commercials']);   
259                        foreach ($monthData['commercials'] as &$commercialData) {
260                            $commercialData['budget_types'] = array_values($commercialData['budget_types']);
261                        }         
262                    }
263                }
264
265                if($aggregatedBy == 3){
266                    $yearData['commercials'] = array_values($yearData['commercials']);   
267                    foreach ($yearData['commercials'] as &$commercialData) {
268                        $commercialData['budget_types'] = array_values($commercialData['budget_types']);
269                    }         
270                }
271
272                $yearData['months'] = array_values($yearData['months']);
273            }
274    
275            return array_values($structuredData);
276        }
277
278        if($groupByFilter == 3){
279            foreach ($rows as $row) {
280                $year = $row->year ?? null;
281                $month = $row->month ?? null;
282                $week = $row->week ?? null;
283                $commercial = $row->commercial ?? null;
284                $budgetType = $row->budget_type ?? null;
285    
286                $dynamicData = [];
287                foreach ($row as $key => $value) {
288                    if (!in_array($key, ['year', 'budget_type', 'commercial', 'month', 'week'])) {
289                        $dynamicData[$key] = $value;
290                    }
291                }
292    
293                if ($year !== null) {
294                    if (!isset($structuredData[$year])) {
295    
296                        $structuredData[$year] = $dynamicData + [
297                            'year' => $year,
298                            'budget_types' => [],
299                        ];
300                    }
301    
302                    if ($budgetType !== null) {
303                        if (!isset($structuredData[$year]['budget_types'][$budgetType])) {
304                            $structuredData[$year]['budget_types'][$budgetType] = $dynamicData + [
305                                'budget_types' => $budgetType,
306                                'commercials' => []
307                            ];
308                        }         
309                        
310                        if($commercial !== null){
311                            if (!isset($structuredData[$year]['budget_types'][$budgetType]['commercials'][$commercial])) {
312                                $structuredData[$year]['budget_types'][$budgetType]['commercials'][$commercial] = $dynamicData + [
313                                    'commercial' => $commercial,
314                                    'months' => []
315                                ];
316                            }  
317
318                            if ($month !== null) {
319                                if (!isset($structuredData[$year]['budget_types'][$budgetType]['commercials'][$commercial]['months'][$month])) {
320                                    $structuredData[$year]['budget_types'][$budgetType]['commercials'][$commercial]['months'][$month] = $dynamicData + [
321                                        'month' => $month,
322                                        'weeks' => [],
323                                    ];
324                                }
325                                
326                                
327                                if ($week !== null) {                            
328                                    if (!isset($structuredData[$year]['budget_types'][$budgetType]['commercials'][$commercial]['months'][$month]['weeks'][$week])) {
329                                        $structuredData[$year]['budget_types'][$budgetType]['commercials'][$commercial]['months'][$month]['weeks'][$week] = $dynamicData + [
330                                            'week' => $week,
331                                        ];
332                                    }
333                                }
334                            }                               
335                        }                        
336                    }
337                }
338            }
339
340            foreach ($structuredData as &$yearData) {
341                                
342                foreach ($yearData['budget_types'] as &$budgetTypeData) {
343                    $budgetTypeData['commercials'] = array_values($budgetTypeData['commercials']);
344
345                    foreach ($budgetTypeData['commercials'] as &$commercialData) {
346                        $commercialData['months'] = array_values($commercialData['months']);     
347
348                        foreach ($commercialData['months'] as &$monthData) {
349                            $monthData['weeks'] = array_values($monthData['weeks']);
350                        }
351                    }
352                }
353                
354                $yearData['budget_types'] = array_values($yearData['budget_types']);
355            }
356    
357            return array_values($structuredData);
358        }
359    }
360}