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 ReflectionClass; |
17
|
|
|
use function array_fill_keys; |
18
|
|
|
use function array_filter; |
19
|
|
|
use function array_reduce; |
20
|
|
|
use function array_unique; |
21
|
|
|
use function count; |
22
|
|
|
use function iterator_to_array; |
23
|
|
|
use function rsort; |
24
|
|
|
use function strlen; |
25
|
|
|
use function strpos; |
26
|
|
|
use const COUNT_RECURSIVE; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns the BOM sequence found at the start of the string. |
30
|
|
|
* |
31
|
|
|
* If no valid BOM sequence is found an empty string is returned |
32
|
|
|
*/ |
33
|
|
|
function bom_match(string $str): string |
34
|
|
|
{ |
35
|
51 |
|
static $list; |
36
|
51 |
|
if (null === $list) { |
37
|
3 |
|
$list = (new ReflectionClass(ByteSequence::class))->getConstants(); |
38
|
|
|
|
39
|
3 |
|
rsort($list); |
40
|
|
|
} |
41
|
|
|
|
42
|
51 |
|
foreach ($list as $sequence) { |
43
|
51 |
|
if (0 === strpos($str, $sequence)) { |
44
|
18 |
|
return $sequence; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
33 |
|
return ''; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Detect Delimiters usage in a {@link Reader} object. |
53
|
|
|
* |
54
|
|
|
* Returns a associative array where each key represents |
55
|
|
|
* a submitted delimiter and each value the number CSV fields found |
56
|
|
|
* when processing at most $limit CSV records with the given delimiter |
57
|
|
|
* |
58
|
|
|
* @param string[] $delimiters |
59
|
|
|
* |
60
|
|
|
* @return int[] |
61
|
|
|
*/ |
62
|
|
|
function delimiter_detect(Reader $csv, array $delimiters, int $limit = 1): array |
63
|
|
|
{ |
64
|
|
|
$delimiter_filter = static function (string $value): bool { |
65
|
18 |
|
return 1 === strlen($value); |
66
|
21 |
|
}; |
67
|
|
|
|
68
|
|
|
$record_filter = static function (array $record): bool { |
69
|
12 |
|
return 1 < count($record); |
70
|
21 |
|
}; |
71
|
|
|
|
72
|
21 |
|
$stmt = Statement::create(null, 0, $limit); |
73
|
|
|
|
74
|
|
|
$delimiter_stats = static function (array $stats, string $delimiter) use ($csv, $stmt, $record_filter): array { |
75
|
12 |
|
$csv->setDelimiter($delimiter); |
76
|
12 |
|
$found_records = array_filter( |
77
|
12 |
|
iterator_to_array($stmt->process($csv)->getRecords(), false), |
78
|
12 |
|
$record_filter |
79
|
|
|
); |
80
|
|
|
|
81
|
12 |
|
$stats[$delimiter] = count($found_records, COUNT_RECURSIVE); |
82
|
|
|
|
83
|
12 |
|
return $stats; |
84
|
18 |
|
}; |
85
|
|
|
|
86
|
18 |
|
$current_delimiter = $csv->getDelimiter(); |
87
|
18 |
|
$current_header_offset = $csv->getHeaderOffset(); |
88
|
18 |
|
$csv->setHeaderOffset(null); |
89
|
|
|
|
90
|
18 |
|
$stats = array_reduce( |
91
|
18 |
|
array_unique(array_filter($delimiters, $delimiter_filter)), |
92
|
15 |
|
$delimiter_stats, |
93
|
15 |
|
array_fill_keys($delimiters, 0) |
94
|
|
|
); |
95
|
|
|
|
96
|
15 |
|
$csv->setHeaderOffset($current_header_offset); |
97
|
15 |
|
$csv->setDelimiter($current_delimiter); |
98
|
|
|
|
99
|
15 |
|
return $stats; |
100
|
|
|
} |
101
|
|
|
|