Files
breadcrumb-the-shire/lib/Service/Import/CsvReaderService.php
fs 25370a1a55 add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00

254 lines
7.2 KiB
PHP

<?php
namespace MintyPHP\Service\Import;
class CsvReaderService
{
private const DELIMITERS = [',', ';'];
/**
* @return array{ok:bool, delimiter?:string, headers?:array<int,string>, rows_total?:int, code?:string}
*/
public static function analyze(string $path, int $maxRows): array
{
if (!is_file($path) || !is_readable($path)) {
return ['ok' => false, 'code' => 'invalid_file_type'];
}
$delimiter = self::detectDelimiter($path);
$headerMeta = self::readHeader($path, $delimiter);
if (!$headerMeta['ok']) {
$code = isset($headerMeta['code']) ? (string) $headerMeta['code'] : 'invalid_csv_header';
return ['ok' => false, 'code' => $code];
}
$headers = $headerMeta['headers'] ?? [];
$headerLine = (int) ($headerMeta['header_line'] ?? 1);
if (!$headers) {
return ['ok' => false, 'code' => 'invalid_csv_header'];
}
$rowsTotal = self::countRows($path, $delimiter, $headers, $headerLine);
if ($rowsTotal <= 0) {
return ['ok' => false, 'code' => 'empty_csv'];
}
if ($rowsTotal > $maxRows) {
return ['ok' => false, 'code' => 'max_rows_exceeded'];
}
return [
'ok' => true,
'delimiter' => $delimiter,
'headers' => $headers,
'rows_total' => $rowsTotal,
];
}
/**
* @param array<int, string> $headers
*/
public static function streamRows(string $path, string $delimiter, array $headers, callable $callback): void
{
$headerMeta = self::readHeader($path, $delimiter);
if (!$headerMeta['ok']) {
return;
}
$headerLine = (int) ($headerMeta['header_line'] ?? 1);
$handle = @fopen($path, 'rb');
if (!$handle) {
return;
}
$lineNumber = 0;
$rowIndex = 0;
while (($row = fgetcsv($handle, 0, $delimiter, '"', '\\')) !== false) {
$lineNumber++;
if ($lineNumber <= $headerLine) {
continue;
}
$row = self::normalizeRowColumns($row, count($headers));
if (self::isEmptyRow($row)) {
continue;
}
$rowIndex++;
$assoc = [];
foreach ($headers as $index => $header) {
$assoc[$header] = self::cleanCell($row[$index] ?? '');
}
$callback($assoc, $lineNumber, $rowIndex);
}
fclose($handle);
}
private static function detectDelimiter(string $path): string
{
$sample = '';
$handle = @fopen($path, 'rb');
if ($handle) {
for ($i = 0; $i < 5; $i++) {
$line = fgets($handle);
if ($line === false) {
break;
}
$sample .= $line;
}
fclose($handle);
}
$bestDelimiter = ',';
$bestScore = -1;
foreach (self::DELIMITERS as $delimiter) {
$score = substr_count($sample, $delimiter);
if ($score > $bestScore) {
$bestScore = $score;
$bestDelimiter = $delimiter;
}
}
return $bestDelimiter;
}
/**
* @return array{ok:bool, headers?:array<int,string>, header_line?:int, code?:string}
*/
private static function readHeader(string $path, string $delimiter): array
{
$handle = @fopen($path, 'rb');
if (!$handle) {
return ['ok' => false, 'code' => 'invalid_csv_header'];
}
$lineNumber = 0;
while (($row = fgetcsv($handle, 0, $delimiter, '"', '\\')) !== false) {
$lineNumber++;
if (!is_array($row)) {
continue;
}
$row = self::normalizeRowColumns($row, count($row));
if (self::isEmptyRow($row)) {
continue;
}
$headers = [];
foreach ($row as $index => $cell) {
$value = self::cleanCell((string) $cell);
if ($index === 0) {
$value = self::stripBom($value);
}
$headers[] = trim($value);
}
fclose($handle);
if (!$headers) {
return ['ok' => false, 'code' => 'invalid_csv_header'];
}
foreach ($headers as $headerValue) {
if ($headerValue === '') {
return ['ok' => false, 'code' => 'invalid_csv_header'];
}
}
$normalizedKeys = array_map([self::class, 'lower'], $headers);
if (count(array_unique($normalizedKeys)) !== count($normalizedKeys)) {
return ['ok' => false, 'code' => 'invalid_csv_header'];
}
return ['ok' => true, 'headers' => $headers, 'header_line' => $lineNumber];
}
fclose($handle);
return ['ok' => false, 'code' => 'empty_csv'];
}
/**
* @param array<int, mixed> $row
* @return array<int, string>
*/
private static function normalizeRowColumns(array $row, int $size): array
{
$normalized = [];
foreach ($row as $cell) {
$normalized[] = self::cleanCell((string) $cell);
}
$current = count($normalized);
if ($current < $size) {
$normalized = array_pad($normalized, $size, '');
} elseif ($current > $size) {
$normalized = array_slice($normalized, 0, $size);
}
return $normalized;
}
/**
* @param array<int, string> $headers
*/
private static function countRows(string $path, string $delimiter, array $headers, int $headerLine): int
{
$count = 0;
$handle = @fopen($path, 'rb');
if (!$handle) {
return 0;
}
$lineNumber = 0;
while (($row = fgetcsv($handle, 0, $delimiter, '"', '\\')) !== false) {
$lineNumber++;
if ($lineNumber <= $headerLine) {
continue;
}
if (!is_array($row)) {
continue;
}
$row = self::normalizeRowColumns($row, count($headers));
if (self::isEmptyRow($row)) {
continue;
}
$count++;
}
fclose($handle);
return $count;
}
private static function isEmptyRow(array $row): bool
{
foreach ($row as $cell) {
if (trim((string) $cell) !== '') {
return false;
}
}
return true;
}
private static function stripBom(string $value): string
{
if (str_starts_with($value, "\xEF\xBB\xBF")) {
return substr($value, 3);
}
return $value;
}
private static function cleanCell(string $value): string
{
$value = str_replace("\0", '', $value);
$value = self::stripBom($value);
return trim($value);
}
private static function lower(string $value): string
{
if (function_exists('mb_strtolower')) {
return mb_strtolower($value, 'UTF-8');
}
return strtolower($value);
}
}