Completed
Push — master ( 03d152...127518 )
by ignace nyamagana
05:49
created

functions.php ➔ delimiter_detect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 3
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 1
rs 9.3142
c 0
b 0
f 0
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
    $stmt = (new Statement())->limit($limit)->where(function (array $record): bool {
63 6
        return count($record) > 1;
64 8
    });
65
    $reducer = function (array $result, string $delimiter) use ($csv, $stmt): array {
66 6
        $result[$delimiter] = count(iterator_to_array($stmt->process($csv->setDelimiter($delimiter)), false), COUNT_RECURSIVE);
67
68 6
        return $result;
69 8
    };
70 8
    $delimiter = $csv->getDelimiter();
71 8
    $header_offset = $csv->getHeaderOffset();
72 8
    $csv->setHeaderOffset(null);
73 8
    $stats = array_reduce($found, $reducer, array_fill_keys($delimiters, 0));
74 8
    $csv->setHeaderOffset($header_offset)->setDelimiter($delimiter);
75
76 8
    return $stats;
77
}
78
79
if (!function_exists('\is_iterable')) {
80
    /**
81
     * Tell whether the contents of the variable is iterable
82
     *
83
     * @see http://php.net/manual/en/function.is-iterable.php
84
     *
85
     * @param array|Traversable $iterable
86
     *
87
     * @return bool
88
     */
89
    function is_iterable($iterable): bool
90
    {
91 1
        return is_array($iterable) || $iterable instanceof Traversable;
92
    }
93
}
94