}> */ public static function parse(string $path): array { $reader = IOFactory::createReader(IOFactory::identify($path)); $reader->setReadDataOnly(true); $righe = $reader->load($path)->getSheet(0)->toArray(null, true, false, false); return self::parseRighe($righe); } /** * @param list> $righe */ public static function parseRighe(array $righe): array { $blocchi = []; $n = count($righe); for ($r = 0; $r < $n; $r++) { $titolo = $righe[$r][0] ?? null; if (! is_string($titolo) || trim($titolo) === '') { continue; } $intestazione = $r; $larghezze = self::larghezze($righe[$r]); if (! $larghezze && isset($righe[$r + 1])) { $intestazione = $r + 1; $larghezze = self::larghezze($righe[$r + 1]); } if (! $larghezze) { continue; } $prezzi = []; for ($rr = $intestazione + 1; $rr < $n; $rr++) { $altezza = $righe[$rr][0] ?? null; if (! is_numeric($altezza)) { break; } foreach ($larghezze as $colonna => $larghezza) { $prezzo = $righe[$rr][$colonna] ?? null; if (! is_numeric($prezzo)) { continue; } $prezzi[] = [ 'larghezza' => (int) round($larghezza), 'altezza' => (int) round((float) $altezza), 'prezzo' => round((float) $prezzo, 2), ]; } } if ($prezzi) { $blocchi[] = ['codice' => trim($titolo), 'prezzi' => $prezzi]; } $r = $rr - 1; } return $blocchi; } /** * Larghezze consecutive a partire dalla colonna B, indicizzate per colonna. * * @return array */ private static function larghezze(array $riga): array { $larghezze = []; for ($c = 1; $c < count($riga) && is_numeric($riga[$c]); $c++) { $larghezze[$c] = (float) $riga[$c]; } return $larghezze; } }