instances added god may help
This commit is contained in:
@@ -9,14 +9,14 @@ class CsvReaderService
|
||||
/**
|
||||
* @return array{ok:bool, delimiter?:string, headers?:array<int,string>, rows_total?:int, code?:string}
|
||||
*/
|
||||
public static function analyze(string $path, int $maxRows): array
|
||||
public 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);
|
||||
$delimiter = $this->detectDelimiter($path);
|
||||
$headerMeta = $this->readHeader($path, $delimiter);
|
||||
if (!$headerMeta['ok']) {
|
||||
$code = isset($headerMeta['code']) ? (string) $headerMeta['code'] : 'invalid_csv_header';
|
||||
return ['ok' => false, 'code' => $code];
|
||||
@@ -28,7 +28,7 @@ class CsvReaderService
|
||||
return ['ok' => false, 'code' => 'invalid_csv_header'];
|
||||
}
|
||||
|
||||
$rowsTotal = self::countRows($path, $delimiter, $headers, $headerLine);
|
||||
$rowsTotal = $this->countRows($path, $delimiter, $headers, $headerLine);
|
||||
if ($rowsTotal <= 0) {
|
||||
return ['ok' => false, 'code' => 'empty_csv'];
|
||||
}
|
||||
@@ -47,9 +47,9 @@ class CsvReaderService
|
||||
/**
|
||||
* @param array<int, string> $headers
|
||||
*/
|
||||
public static function streamRows(string $path, string $delimiter, array $headers, callable $callback): void
|
||||
public function streamRows(string $path, string $delimiter, array $headers, callable $callback): void
|
||||
{
|
||||
$headerMeta = self::readHeader($path, $delimiter);
|
||||
$headerMeta = $this->readHeader($path, $delimiter);
|
||||
if (!$headerMeta['ok']) {
|
||||
return;
|
||||
}
|
||||
@@ -68,15 +68,15 @@ class CsvReaderService
|
||||
continue;
|
||||
}
|
||||
|
||||
$row = self::normalizeRowColumns($row, count($headers));
|
||||
if (self::isEmptyRow($row)) {
|
||||
$row = $this->normalizeRowColumns($row, count($headers));
|
||||
if ($this->isEmptyRow($row)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rowIndex++;
|
||||
$assoc = [];
|
||||
foreach ($headers as $index => $header) {
|
||||
$assoc[$header] = self::cleanCell($row[$index] ?? '');
|
||||
$assoc[$header] = $this->cleanCell($row[$index] ?? '');
|
||||
}
|
||||
$callback($assoc, $lineNumber, $rowIndex);
|
||||
}
|
||||
@@ -84,7 +84,7 @@ class CsvReaderService
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
private static function detectDelimiter(string $path): string
|
||||
private function detectDelimiter(string $path): string
|
||||
{
|
||||
$sample = '';
|
||||
$handle = @fopen($path, 'rb');
|
||||
@@ -115,7 +115,7 @@ class CsvReaderService
|
||||
/**
|
||||
* @return array{ok:bool, headers?:array<int,string>, header_line?:int, code?:string}
|
||||
*/
|
||||
private static function readHeader(string $path, string $delimiter): array
|
||||
private function readHeader(string $path, string $delimiter): array
|
||||
{
|
||||
$handle = @fopen($path, 'rb');
|
||||
if (!$handle) {
|
||||
@@ -128,16 +128,16 @@ class CsvReaderService
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
$row = self::normalizeRowColumns($row, count($row));
|
||||
if (self::isEmptyRow($row)) {
|
||||
$row = $this->normalizeRowColumns($row, count($row));
|
||||
if ($this->isEmptyRow($row)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$headers = [];
|
||||
foreach ($row as $index => $cell) {
|
||||
$value = self::cleanCell((string) $cell);
|
||||
$value = $this->cleanCell((string) $cell);
|
||||
if ($index === 0) {
|
||||
$value = self::stripBom($value);
|
||||
$value = $this->stripBom($value);
|
||||
}
|
||||
$headers[] = trim($value);
|
||||
}
|
||||
@@ -154,7 +154,7 @@ class CsvReaderService
|
||||
}
|
||||
}
|
||||
|
||||
$normalizedKeys = array_map([self::class, 'lower'], $headers);
|
||||
$normalizedKeys = array_map([$this, 'lower'], $headers);
|
||||
if (count(array_unique($normalizedKeys)) !== count($normalizedKeys)) {
|
||||
return ['ok' => false, 'code' => 'invalid_csv_header'];
|
||||
}
|
||||
@@ -170,11 +170,11 @@ class CsvReaderService
|
||||
* @param array<int, mixed> $row
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private static function normalizeRowColumns(array $row, int $size): array
|
||||
private function normalizeRowColumns(array $row, int $size): array
|
||||
{
|
||||
$normalized = [];
|
||||
foreach ($row as $cell) {
|
||||
$normalized[] = self::cleanCell((string) $cell);
|
||||
$normalized[] = $this->cleanCell((string) $cell);
|
||||
}
|
||||
|
||||
$current = count($normalized);
|
||||
@@ -190,7 +190,7 @@ class CsvReaderService
|
||||
/**
|
||||
* @param array<int, string> $headers
|
||||
*/
|
||||
private static function countRows(string $path, string $delimiter, array $headers, int $headerLine): int
|
||||
private function countRows(string $path, string $delimiter, array $headers, int $headerLine): int
|
||||
{
|
||||
$count = 0;
|
||||
$handle = @fopen($path, 'rb');
|
||||
@@ -207,8 +207,8 @@ class CsvReaderService
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
$row = self::normalizeRowColumns($row, count($headers));
|
||||
if (self::isEmptyRow($row)) {
|
||||
$row = $this->normalizeRowColumns($row, count($headers));
|
||||
if ($this->isEmptyRow($row)) {
|
||||
continue;
|
||||
}
|
||||
$count++;
|
||||
@@ -218,7 +218,7 @@ class CsvReaderService
|
||||
return $count;
|
||||
}
|
||||
|
||||
private static function isEmptyRow(array $row): bool
|
||||
private function isEmptyRow(array $row): bool
|
||||
{
|
||||
foreach ($row as $cell) {
|
||||
if (trim((string) $cell) !== '') {
|
||||
@@ -228,7 +228,7 @@ class CsvReaderService
|
||||
return true;
|
||||
}
|
||||
|
||||
private static function stripBom(string $value): string
|
||||
private function stripBom(string $value): string
|
||||
{
|
||||
if (str_starts_with($value, "\xEF\xBB\xBF")) {
|
||||
return substr($value, 3);
|
||||
@@ -236,14 +236,14 @@ class CsvReaderService
|
||||
return $value;
|
||||
}
|
||||
|
||||
private static function cleanCell(string $value): string
|
||||
private function cleanCell(string $value): string
|
||||
{
|
||||
$value = str_replace("\0", '', $value);
|
||||
$value = self::stripBom($value);
|
||||
$value = $this->stripBom($value);
|
||||
return trim($value);
|
||||
}
|
||||
|
||||
private static function lower(string $value): string
|
||||
private function lower(string $value): string
|
||||
{
|
||||
if (function_exists('mb_strtolower')) {
|
||||
return mb_strtolower($value, 'UTF-8');
|
||||
|
||||
Reference in New Issue
Block a user