File manager - Edit - /var/www/html/portal/database/seeders/Concerns/ReadsObservatorioOds.php
Back
<?php namespace Database\Seeders\Concerns; use DOMDocument; use DOMXPath; use RuntimeException; use ZipArchive; /** * Leitura de planilhas .ods exportadas do antigo Observatório. */ trait ReadsObservatorioOds { /** Lê um valor de célula pela chave do cabeçalho. */ protected function cell(array $row, array $col, string $key): string { $index = $col[$key] ?? null; if ($index === null) { return ''; } return (string) ($row[$index] ?? ''); } protected function toBool(string $value): bool { return mb_strtolower(trim($value)) === 'sim'; } protected function nullable(string $value): ?string { $value = trim($value); if ($value === '' || strtoupper($value) === 'NULL') { return null; } return $value; } /** Lê um arquivo .ods retornando ['NomeDaPlanilha' => [linhas[colunas]]]. */ protected function readOds(string $path): array { if (!is_file($path)) { throw new RuntimeException("Arquivo não encontrado: {$path}"); } $zip = new ZipArchive(); if ($zip->open($path) !== true) { throw new RuntimeException("Não foi possível abrir: {$path}"); } $xml = $zip->getFromName('content.xml'); $zip->close(); $doc = new DOMDocument(); $doc->loadXML($xml); $ns = [ 'table' => 'urn:oasis:names:tc:opendocument:xmlns:table:1.0', 'text' => 'urn:oasis:names:tc:opendocument:xmlns:text:1.0', ]; $xp = new DOMXPath($doc); foreach ($ns as $prefix => $uri) { $xp->registerNamespace($prefix, $uri); } $sheets = []; foreach ($xp->query('//table:table') as $table) { $name = $table->getAttributeNS($ns['table'], 'name'); $rows = []; foreach ($xp->query('table:table-row', $table) as $rowNode) { $cells = []; foreach ($xp->query('table:table-cell', $rowNode) as $cellNode) { $repeat = (int) ($cellNode->getAttributeNS($ns['table'], 'number-columns-repeated') ?: 1); $repeat = min($repeat, 50); $parts = []; foreach ($xp->query('text:p', $cellNode) as $p) { $parts[] = $p->textContent; } $value = implode("\n", $parts); for ($i = 0; $i < $repeat; $i++) { $cells[] = $value; } } while ($cells && end($cells) === '') { array_pop($cells); } $rows[] = $cells; } while ($rows && count(end($rows)) === 0) { array_pop($rows); } $sheets[$name] = $rows; } return $sheets; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings