Completed
Pull Request — master (#309)
by ignace nyamagana
02:31
created

functions.php ➔ is_empty_escape_char_supported()   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 0
dl 0
loc 4
ccs 0
cts 1
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
 * @author  Ignace Nyamagana Butera <[email protected]>
7
 * @license https://github.com/thephpleague/csv/blob/master/LICENSE (MIT License)
8
 * @version 9.1.5
9
 * @link    https://github.com/thephpleague/csv
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
declare(strict_types=1);
16
17
namespace League\Csv {
18
19
    use ReflectionClass;
20
    use Traversable;
21
    use function array_fill_keys;
22
    use function array_filter;
23
    use function array_reduce;
24
    use function array_unique;
25
    use function count;
26
    use function is_array;
27
    use function iterator_to_array;
28
    use function strpos;
29
30
    /**
31
     * Returns the BOM sequence found at the start of the string.
32
     *
33
     * If no valid BOM sequence is found an empty string is returned
34
     */
35
    function bom_match(string $str): string
36
    {
37 33
        static $list;
38
39 33
        $list = $list ?? (new ReflectionClass(ByteSequence::class))->getConstants();
40
41 33
        foreach ($list as $sequence) {
42 33
            if (0 === strpos($str, $sequence)) {
43 33
                return $sequence;
44
            }
45
        }
46
47 27
        return '';
48
    }
49
50
    /**
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
        $found = array_unique(array_filter($delimiters, function (string $value): bool {
64 18
            return 1 == strlen($value);
65 18
        }));
66
        $stmt = (new Statement())->limit($limit)->where(function (array $record): bool {
67 9
            return count($record) > 1;
68 12
        });
69
        $reducer = function (array $result, string $delimiter) use ($csv, $stmt): array {
70 9
            $result[$delimiter] = count(iterator_to_array($stmt->process($csv->setDelimiter($delimiter)), false), COUNT_RECURSIVE);
71
72 9
            return $result;
73 12
        };
74 12
        $delimiter = $csv->getDelimiter();
75 12
        $header_offset = $csv->getHeaderOffset();
76 12
        $csv->setHeaderOffset(null);
77 12
        $stats = array_reduce($found, $reducer, array_fill_keys($delimiters, 0));
78 12
        $csv->setHeaderOffset($header_offset)->setDelimiter($delimiter);
79
80 12
        return $stats;
81
    }
82
83
    /**
84
     * Tell whether the content of the variable is iterable.
85
     *
86
     * @see http://php.net/manual/en/function.is-iterable.php
87
     */
88
    function is_iterable($iterable): bool
89
    {
90 4
        return is_array($iterable) || $iterable instanceof Traversable;
91
    }
92
93
    /**
94
     * Tell whether the content of the variable is an int or null.
95
     *
96
     * @see https://wiki.php.net/rfc/nullable_types
97
     */
98
    function is_nullable_int($value): bool
99
    {
100 9
        return null === $value || is_int($value);
101
    }
102
103
    function is_empty_escape_char_supported(): bool
104
    {
105
        return PHP_VERSION_ID > 70400;
106
    }
107
}
108
109
namespace {
110
111
    use League\Csv;
112
113
    if (PHP_VERSION_ID < 70100 && !function_exists('\is_iterable')) {
114
        function is_iterable($iterable)
115
        {
116
            return Csv\is_iterable($iterable);
117
        }
118
    }
119
}
120