FIX: Security en htm
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Incoviba\Service\Cartola;
|
namespace Incoviba\Service\Cartola;
|
||||||
|
|
||||||
|
use DOMDocument;
|
||||||
|
use DateTimeImmutable;
|
||||||
use Psr\Http\Message\UploadedFileInterface;
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
use PhpOffice\PhpSpreadsheet;
|
use PhpOffice\PhpSpreadsheet;
|
||||||
use Incoviba\Common\Define\Cartola\Banco;
|
use Incoviba\Common\Define\Cartola\Banco;
|
||||||
@ -9,9 +11,19 @@ class Security implements Banco
|
|||||||
{
|
{
|
||||||
public function process(UploadedFileInterface $file): array
|
public function process(UploadedFileInterface $file): array
|
||||||
{
|
{
|
||||||
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
$stream = $file->getStream();
|
||||||
|
$stream->seek(3);
|
||||||
|
if ($stream->read(strlen('table')) === 'table') {
|
||||||
|
return $this->processHtm($file);
|
||||||
|
}
|
||||||
|
return $this->processXls($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function processXls(UploadedFileInterface $file): array
|
||||||
|
{
|
||||||
$filename = '/tmp/cartola.xls';
|
$filename = '/tmp/cartola.xls';
|
||||||
$file->moveTo($filename);
|
$file->moveTo($filename);
|
||||||
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
||||||
$xlsx = $reader->load($filename);
|
$xlsx = $reader->load($filename);
|
||||||
$worksheet = $xlsx->getActiveSheet();
|
$worksheet = $xlsx->getActiveSheet();
|
||||||
$rows = $worksheet->getRowIterator();
|
$rows = $worksheet->getRowIterator();
|
||||||
@ -51,4 +63,44 @@ class Security implements Banco
|
|||||||
unlink($filename);
|
unlink($filename);
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
private function processHtm(UploadedFileInterface $file): array
|
||||||
|
{
|
||||||
|
$filename = '/tmp/cartola.htm';
|
||||||
|
$file->moveTo($filename);
|
||||||
|
|
||||||
|
$domDocument = new DOMDocument();
|
||||||
|
$domDocument->loadHTML('<body>' . file_get_contents($filename) . '</body>');
|
||||||
|
|
||||||
|
$tables = $domDocument->getElementsByTagName('table');
|
||||||
|
$table = $tables->item(4);
|
||||||
|
|
||||||
|
$columns = [];
|
||||||
|
$data = [];
|
||||||
|
foreach ($table->getElementsByTagName('tr')->getIterator() as $rowIndex => $row) {
|
||||||
|
if ($rowIndex === 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (str_contains($row->textContent, 'cargos')) {
|
||||||
|
foreach ($row->getElementsByTagName('td')->getIterator() as $cell) {
|
||||||
|
$columns []= trim($cell->textContent);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$rowData = [];
|
||||||
|
foreach ($row->getElementsByTagName('td')->getIterator() as $colIndex => $cell) {
|
||||||
|
$col = $columns[$colIndex];
|
||||||
|
$value = trim($cell->textContent);
|
||||||
|
if ($col === 'fecha') {
|
||||||
|
$value = DateTimeImmutable::createFromFormat('d/m/Y', $value)->format('Y-m-d');
|
||||||
|
}
|
||||||
|
if (in_array($col, ['cargos', 'abonos', 'saldos'])) {
|
||||||
|
$value = (float) str_replace(',', '.', $value);
|
||||||
|
}
|
||||||
|
$rowData[$col] = $value;
|
||||||
|
}
|
||||||
|
$data []= $rowData;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user