Completed
Pull Request — master (#210)
by ignace nyamagana
02:27
created

ValidatorTrait::removeBOM()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 3
crap 4
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
/**
18
 *  An abstract class to enable basic CSV manipulation
19
 *
20
 * @package  League.csv
21
 * @since    9.0.0
22
 * @author   Ignace Nyamagana Butera <[email protected]>
23
 * @internal Use to validate incoming data
24
 */
25
trait ValidatorTrait
26
{
27
    /**
28
     * Filter Csv control character
29
     *
30
     * @param string $char Csv control character
31
     * @param string $type Csv control character type
32
     *
33
     * @throws Exception If the Csv control character is not one character only.
34
     *
35
     * @return string
36
     */
37 192
    protected function filterControl(string $char, string $type)
38
    {
39 192
        if (1 == strlen($char)) {
40 192
            return $char;
41
        }
42
43 6
        throw new Exception(sprintf('The %s must be a single character', $type));
44
    }
45
46
    /**
47
     * Validate an integer
48
     *
49
     * @param int    $value
50
     * @param int    $min_value
51
     * @param string $error_message
52
     *
53
     * @throws Exception If the value is invalid
54
     *
55
     * @return int
56
     */
57 78
    protected function filterInteger(int $value, int $min_value, string $error_message): int
58
    {
59 78
        if ($value < $min_value) {
60 10
            throw new Exception($error_message);
61
        }
62
63 70
        return $value;
64
    }
65
66
    /**
67
     * Validates the array to be used by the fetchAssoc method
68
     *
69
     * @param array $keys
70
     *
71
     * @throws Exception If the submitted array fails the assertion
72
     *
73
     * @return array
74
     */
75 32
    protected function filterHeader(array $keys): array
76
    {
77 32
        if (empty($keys)) {
78 2
            return $keys;
79
        }
80
81 30
        if ($keys !== array_unique(array_filter($keys, 'is_string'))) {
82 2
            throw new Exception('Use a flat array with unique string values');
83
        }
84
85 28
        return $keys;
86
    }
87
88
    /**
89
     * Strip the BOM sequence from a record
90
     *
91
     * @param string[] $row
92
     * @param int      $bom_length
93
     * @param string   $enclosure
94
     *
95
     * @return string[]
96
     */
97 28
    protected function removeBOM(array $row, int $bom_length, string $enclosure): array
98
    {
99 28
        if (0 == $bom_length) {
100 10
            return $row;
101
        }
102
103 18
        $row[0] = mb_substr($row[0], $bom_length);
104 18
        if ($enclosure == mb_substr($row[0], 0, 1) && $enclosure == mb_substr($row[0], -1, 1)) {
105 8
            $row[0] = mb_substr($row[0], 1, -1);
106
        }
107
108 18
        return $row;
109
    }
110
}
111