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