55 lines
1.9 KiB
PHP
55 lines
1.9 KiB
PHP
|
<?php
|
||
|
namespace Incoviba\Service\Cartola;
|
||
|
|
||
|
use Psr\Http\Message\UploadedFileInterface;
|
||
|
use PhpOffice\PhpSpreadsheet;
|
||
|
use Incoviba\Common\Define\Cartola\Banco;
|
||
|
|
||
|
class Security implements Banco
|
||
|
{
|
||
|
public function process(UploadedFileInterface $file): array
|
||
|
{
|
||
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
||
|
$filename = '/tmp/cartola.xls';
|
||
|
$file->moveTo($filename);
|
||
|
$xlsx = $reader->load($filename);
|
||
|
$worksheet = $xlsx->getActiveSheet();
|
||
|
$rows = $worksheet->getRowIterator();
|
||
|
$dataFound = false;
|
||
|
$columns = [];
|
||
|
$data = [];
|
||
|
foreach ($rows as $row) {
|
||
|
$cells = $row->getCellIterator();
|
||
|
$rowData = [];
|
||
|
foreach ($cells as $cell) {
|
||
|
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() === "fecha ") {
|
||
|
$cols = $row->getColumnIterator();
|
||
|
foreach ($cols as $col) {
|
||
|
$columns[$col->getColumn()] = trim($col->getCalculatedValue());
|
||
|
}
|
||
|
$dataFound = true;
|
||
|
break;
|
||
|
}
|
||
|
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() === null) {
|
||
|
$dataFound = false;
|
||
|
break;
|
||
|
}
|
||
|
if (!$dataFound) {
|
||
|
break;
|
||
|
}
|
||
|
$col = $columns[$cell->getColumn()];
|
||
|
$value = $cell->getCalculatedValue();
|
||
|
if ($col === 'fecha') {
|
||
|
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cell->getValue(), 'America/Santiago')->format('Y-m-d');
|
||
|
}
|
||
|
$rowData[$col] = $value;
|
||
|
}
|
||
|
if (count($rowData) > 0) {
|
||
|
$data []= $rowData;
|
||
|
}
|
||
|
}
|
||
|
unlink($filename);
|
||
|
return $data;
|
||
|
}
|
||
|
}
|