<?php
 namespace App\Reports\Generation; class WriteFile { private \App\Reports\Generation\Generate $reportGeneration; public function __construct(\App\Reports\Generation\Generate $reportGeneration) { $this->reportGeneration = $reportGeneration; } public function write() :void { switch ($this->reportGeneration->format) { case 'xlsx': { $writeReportFile = new \App\Reports\Generation\WriteFile\Xlsx(); break; } default: { throw new \Exception("Invalid/missing format for report output `{$this->reportGeneration->format}`."); } } $writeReportFile->dbTable = $this->reportGeneration->getTableManager()->getTableName(); $writeReportFile->columns = $this->getOrderedColumnsNames(); $writeReportFile->locationDir = $this->reportGeneration->getLocationDir(); $writeReportFile->fileName = $this->reportGeneration->fileName; $writeReportFile->process(); } public function getOrderedColumnsNames() :array { $finalColumnsList = []; $stdCols = $this->reportGeneration->stdColumns; $custCols = []; foreach ($this->reportGeneration->customColumns as $custColCode=>$colDet) { if (!key_exists('hidden', $colDet) or !$colDet['hidden']) { $custCols[$custColCode] = $colDet['name']; } } foreach($this->reportGeneration->columnsOrder as $colCode) { if (key_exists($colCode, $stdCols)) { $finalColumnsList[$colCode] = $stdCols[$colCode]; unset($stdCols[$colCode]); } elseif (key_exists($colCode, $custCols)) { $finalColumnsList[$colCode] = $custCols[$colCode]; unset($custCols[$colCode]); } } $finalColumnsList = $finalColumnsList + $stdCols + $custCols; return $finalColumnsList; } } 