Completed
Pull Request — master (#376)
by ignace nyamagana
10:40
created

functions.php ➔ is_iterable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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