FIX: Security en htm
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Cartola;
|
||||
|
||||
use DOMDocument;
|
||||
use DateTimeImmutable;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use PhpOffice\PhpSpreadsheet;
|
||||
use Incoviba\Common\Define\Cartola\Banco;
|
||||
@ -9,9 +11,19 @@ class Security implements Banco
|
||||
{
|
||||
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';
|
||||
$file->moveTo($filename);
|
||||
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
||||
$xlsx = $reader->load($filename);
|
||||
$worksheet = $xlsx->getActiveSheet();
|
||||
$rows = $worksheet->getRowIterator();
|
||||
@ -51,4 +63,44 @@ class Security implements Banco
|
||||
unlink($filename);
|
||||
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