<?php
namespace Modules\Invoices\SupplierInvoices\ImportStatements\Model; abstract class XlsxAbstract extends GeneralAbstract { protected $headers = []; protected $detectedFormat = null; abstract protected function getCleanedRows(Array $rawRows):array; public function parse():void { if (!is_array($this->headers)) { throw new \Modules\Invoices\SupplierInvoices\ImportStatements\Model\Exception("Supported headers not set."); } parent::parse(); } protected function getProccesedRows($rawRows):array { $this->detectedFormat = $this->detectFormat($rawRows); if ($this->detectedFormat === null) { throw new \Modules\Invoices\SupplierInvoices\ImportStatements\Model\Exception("The format could not be detected."); } $cleanedRows = $this->getCleanedRows($rawRows); $assocRows = []; foreach ($cleanedRows as $row) { $assocRows[] = array_combine(array_keys($this->headers[$this->detectedFormat]), $row); } return $assocRows; } protected function getRawRows():array { $excelObj = \PhpOffice\PhpSpreadsheet\IOFactory::load($this->fullFileName); $objWorksheets = $excelObj->getAllSheets(); $rows = []; foreach ($objWorksheets as $objWorksheet) { $rows = array_merge($rows, $objWorksheet->toArray()); } return $rows; } public function changeToPhpDateFormat($dateValue) { if ($dateValue instanceof \DateTime) { return $dateValue->format('Y-m-d'); } if (is_numeric($dateValue)) { $unixDate = ($dateValue - 25569) * 86400; return date("Y-m-d", $unixDate); } if (is_string($dateValue)) { $dateObj = \Engine\DateTime::createFromFormat('d/m/y', $dateValue); if ($dateObj->isValid()) { return $dateObj->format("Y-m-d"); } } if (is_string($dateValue)) { $dateObj = \Engine\DateTime::createFromFormat('n/j/Y', $dateValue); if ($dateObj->isValid()) { return $dateObj->format("Y-m-d"); } } throw new Exception("Unknown date value found."); } abstract protected function detectFormat(Array $rawRows):?string; }