Completed
Pull Request — master (#207)
by ignace nyamagana
09:14
created

ValidatorTrait::removeBOM()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 3
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\Config;
16
17
use InvalidArgumentException;
18
19
/**
20
 *  An abstract class to enable basic CSV manipulation
21
 *
22
 * @package League.csv
23
 * @since  9.0.0
24
 * @internal
25
 */
26
trait ValidatorTrait
27
{
28
    /**
29
     * Filter Csv control character
30
     *
31
     * @param string $char Csv control character
32
     * @param string $type Csv control character type
33
     *
34
     * @throws InvalidArgumentException If the Csv control character is not one character only.
35
     *
36
     * @return string
37
     */
38
    protected function filterControl(string $char, string $type)
39
    {
40
        if (1 == strlen($char)) {
41
            return $char;
42
        }
43
44
        throw new InvalidArgumentException(sprintf('The %s must be a single character', $type));
45
    }
46
47
    /**
48
     * Validate an integer
49
     *
50
     * @param int    $value
51
     * @param int    $min_value
52
     * @param string $error_message
53
     *
54
     * @throws InvalidArgumentException If the value is invalid
55
     *
56
     * @return int
57
     */
58
    protected function filterInteger(int $value, int $min_value, string $error_message): int
59
    {
60
        if ($value < $min_value) {
61
            throw new InvalidArgumentException($error_message);
62
        }
63
64
        return $value;
65
    }
66
67
    /**
68
     * Strip the BOM sequence from a record
69
     *
70
     * @param string[] $row
71
     * @param int      $bom_length
72
     * @param string   $enclosure
73
     *
74
     * @return string[]
75
     */
76
    protected function removeBOM(array $row, int $bom_length, string $enclosure): array
77
    {
78
        if (0 == $bom_length) {
79
            return $row;
80
        }
81
82
        $row[0] = mb_substr($row[0], $bom_length);
83
        if ($enclosure == mb_substr($row[0], 0, 1) && $enclosure == mb_substr($row[0], -1, 1)) {
84
            $row[0] = mb_substr($row[0], 1, -1);
85
        }
86
87
        return $row;
88
    }
89
}
90