|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the League.csv library |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT |
|
6
|
|
|
* @link https://github.com/thephpleague/csv/ |
|
7
|
|
|
* @version 9.0.0 |
|
8
|
|
|
* @package League.csv |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace League\Csv; |
|
16
|
|
|
|
|
17
|
|
|
use ReflectionClass; |
|
18
|
|
|
use Traversable; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Returns the BOM sequence found at the start of the string |
|
22
|
|
|
* |
|
23
|
|
|
* If no valid BOM sequence is found an empty string is returned |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $str |
|
26
|
|
|
* |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
function bom_match(string $str): string |
|
30
|
|
|
{ |
|
31
|
22 |
|
static $list; |
|
32
|
|
|
|
|
33
|
22 |
|
$list = $list ?? (new ReflectionClass(ByteSequence::class))->getConstants(); |
|
34
|
|
|
|
|
35
|
22 |
|
foreach ($list as $sequence) { |
|
36
|
22 |
|
if (0 === strpos($str, $sequence)) { |
|
37
|
22 |
|
return $sequence; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
18 |
|
return ''; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Detect Delimiters usage in a CSV object |
|
46
|
|
|
* |
|
47
|
|
|
* Returns a associative array where each key represents |
|
48
|
|
|
* a submitted delimiter and each value the number CSV fields found |
|
49
|
|
|
* when processing at most $limit CSV records with the given delimiter |
|
50
|
|
|
* |
|
51
|
|
|
* @param Reader $csv the CSV object |
|
52
|
|
|
* @param string[] $delimiters the delimiters to consider |
|
53
|
|
|
* @param int $limit Detection is made using up to $limit CSV records |
|
54
|
|
|
* |
|
55
|
|
|
* @return int[] |
|
56
|
|
|
*/ |
|
57
|
|
|
function delimiter_detect(Reader $csv, array $delimiters, int $limit = 1): array |
|
58
|
|
|
{ |
|
59
|
|
|
$found = array_unique(array_filter($delimiters, function (string $value): bool { |
|
60
|
12 |
|
return 1 == strlen($value); |
|
61
|
12 |
|
})); |
|
62
|
|
|
|
|
63
|
|
|
$stmt = (new Statement())->limit($limit)->where(function (array $record): bool { |
|
64
|
6 |
|
return count($record) > 1; |
|
65
|
8 |
|
}); |
|
66
|
|
|
|
|
67
|
|
|
$reducer = function (array $result, string $delimiter) use ($csv, $stmt): array { |
|
68
|
6 |
|
$records = $stmt->process($csv->setDelimiter($delimiter)); |
|
69
|
6 |
|
$result[$delimiter] = count(iterator_to_array($records, false), COUNT_RECURSIVE); |
|
70
|
|
|
|
|
71
|
6 |
|
return $result; |
|
72
|
8 |
|
}; |
|
73
|
|
|
|
|
74
|
8 |
|
$delimiter = $csv->getDelimiter(); |
|
75
|
8 |
|
$header_offset = $csv->getHeaderOffset(); |
|
76
|
8 |
|
$csv->setHeaderOffset(null); |
|
77
|
8 |
|
$stats = array_reduce($found, $reducer, array_fill_keys($delimiters, 0)); |
|
78
|
8 |
|
$csv->setHeaderOffset($header_offset)->setDelimiter($delimiter); |
|
79
|
|
|
|
|
80
|
8 |
|
return $stats; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if (!function_exists('\is_iterable')) { |
|
85
|
|
|
/** |
|
86
|
|
|
* Tell whether the contents of the variable is iterable |
|
87
|
|
|
* |
|
88
|
|
|
* @see http://php.net/manual/en/function.is-iterable.php |
|
89
|
|
|
* |
|
90
|
|
|
* @param array|Traversable $iterable |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
function is_iterable($iterable): bool |
|
95
|
|
|
{ |
|
96
|
1 |
|
return is_array($iterable) || $iterable instanceof Traversable; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|