Skip to content

Commit

Permalink
Improve delimiter_detect implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 15, 2019
1 parent ab47395 commit 99a8abf
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function bom_match(string $str): string
*/
function delimiter_detect(Reader $csv, array $delimiters, int $limit = 1): array
{
$filter = static function (string $value): bool {
$delimiter_filter = static function (string $value): bool {
return 1 === strlen($value);
};

Expand All @@ -73,25 +73,27 @@ function delimiter_detect(Reader $csv, array $delimiters, int $limit = 1): array

$stmt = (new Statement())->limit($limit);

$unique_delimiters = array_unique(array_filter($delimiters, $filter));

$reducer = static function (array $result, string $delimiter) use ($csv, $stmt, $record_filter): array {
$delimiter_stats = static function (array $stats, string $delimiter) use ($csv, $stmt, $record_filter): array {
$csv->setDelimiter($delimiter);
$found_records = array_filter(
iterator_to_array($stmt->process($csv), false),
$record_filter
);

$result[$delimiter] = count($found_records, COUNT_RECURSIVE);
$stats[$delimiter] = count($found_records, COUNT_RECURSIVE);

return $result;
return $stats;
};

$current_delimiter = $csv->getDelimiter();
$current_header_offset = $csv->getHeaderOffset();
$csv->setHeaderOffset(null);

$stats = array_reduce($unique_delimiters, $reducer, array_fill_keys($delimiters, 0));
$stats = array_reduce(
array_unique(array_filter($delimiters, $delimiter_filter)),
$delimiter_stats,
array_fill_keys($delimiters, 0)
);

$csv->setHeaderOffset($current_header_offset);
$csv->setDelimiter($current_delimiter);
Expand Down

0 comments on commit 99a8abf

Please sign in to comment.