| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* League.Csv (https://csv.thephpleague.com) |
| 5 |
* |
| 6 |
* (c) Ignace Nyamagana Butera <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace League\Csv; |
| 15 |
|
| 16 |
use function array_fill_keys; |
| 17 |
use function array_filter; |
| 18 |
use function array_reduce; |
| 19 |
use function array_unique; |
| 20 |
use function count; |
| 21 |
use function iterator_to_array; |
| 22 |
use function strlen; |
| 23 |
use function strpos; |
| 24 |
use const COUNT_RECURSIVE; |
| 25 |
|
| 26 |
final class Info implements ByteSequence |
| 27 |
{ |
| 28 |
private const BOM_SEQUENCE_LIST = [ |
| 29 |
self::BOM_UTF32_BE, |
| 30 |
self::BOM_UTF32_LE, |
| 31 |
self::BOM_UTF16_BE, |
| 32 |
self::BOM_UTF16_LE, |
| 33 |
self::BOM_UTF8, |
| 34 |
]; |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns the BOM sequence found at the start of the string. |
| 38 |
* |
| 39 |
* If no valid BOM sequence is found an empty string is returned |
| 40 |
*/ |
| 41 |
public static function fetchBOMSequence(string $str): ?string |
| 42 |
{ |
| 43 |
foreach (self::BOM_SEQUENCE_LIST as $sequence) { |
| 44 |
if (0 === strpos($str, $sequence)) { |
| 45 |
return $sequence; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return null; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Detect Delimiters usage in a {@link Reader} object. |
| 54 |
* |
| 55 |
* Returns a associative array where each key represents |
| 56 |
* a submitted delimiter and each value the number CSV fields found |
| 57 |
* when processing at most $limit CSV records with the given delimiter |
| 58 |
* |
| 59 |
* @param string[] $delimiters |
| 60 |
* |
| 61 |
* @return array<string, int> |
| 62 |
*/ |
| 63 |
public static function getDelimiterStats(Reader $csv, array $delimiters, int $limit = 1): array |
| 64 |
{ |
| 65 |
$delimiterFilter = static fn (string $value): bool => 1 === strlen($value); |
| 66 |
|
| 67 |
$recordFilter = static fn (array $record): bool => 1 < count($record); |
| 68 |
|
| 69 |
$stmt = Statement::create()->offset(0)->limit($limit); |
| 70 |
|
| 71 |
$delimiterStats = static function (array $stats, string $delimiter) use ($csv, $stmt, $recordFilter): array { |
| 72 |
$csv->setDelimiter($delimiter); |
| 73 |
$foundRecords = array_filter( |
| 74 |
iterator_to_array($stmt->process($csv)->getRecords(), false), |
| 75 |
$recordFilter |
| 76 |
); |
| 77 |
|
| 78 |
$stats[$delimiter] = count($foundRecords, COUNT_RECURSIVE); |
| 79 |
|
| 80 |
return $stats; |
| 81 |
}; |
| 82 |
|
| 83 |
$currentDelimiter = $csv->getDelimiter(); |
| 84 |
$currentHeaderOffset = $csv->getHeaderOffset(); |
| 85 |
|
| 86 |
$csv->setHeaderOffset(null); |
| 87 |
|
| 88 |
$stats = array_reduce( |
| 89 |
array_unique(array_filter($delimiters, $delimiterFilter)), |
| 90 |
$delimiterStats, |
| 91 |
array_fill_keys($delimiters, 0) |
| 92 |
); |
| 93 |
|
| 94 |
$csv->setHeaderOffset($currentHeaderOffset); |
| 95 |
$csv->setDelimiter($currentDelimiter); |
| 96 |
|
| 97 |
return $stats; |
| 98 |
} |
| 99 |
} |
| 100 |
|