Completed
Push — master ( ef1cb2...87bc1a )
by ignace nyamagana
03:23
created

ValidatorTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 63
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterControl() 0 8 2
A filterInteger() 0 8 2
A filterColumnNames() 0 12 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;
16
17
use League\Csv\Exception\InvalidArgumentException;
18
19
/**
20
 *  An abstract class to enable basic CSV manipulation
21
 *
22
 * @package  League.csv
23
 * @since    9.0.0
24
 * @author   Ignace Nyamagana Butera <[email protected]>
25
 * @internal Use to validate incoming data
26
 */
27
trait ValidatorTrait
28
{
29
    /**
30
     * Filter Csv control character
31
     *
32
     * @param string $char Csv control character
33
     * @param string $type Csv control character type
34
     *
35
     * @throws InvalidArgumentException If the Csv control character is not one character only.
36
     *
37
     * @return string
38
     */
39 6
    protected function filterControl(string $char, string $type)
40
    {
41 6
        if (1 == strlen($char)) {
42 6
            return $char;
43
        }
44
45 6
        throw new InvalidArgumentException(sprintf('The %s must be a single character', $type));
46
    }
47
48
    /**
49
     * Validate an integer
50
     *
51
     * @param int    $value
52
     * @param int    $min_value
53
     * @param string $error_message
54
     *
55
     * @throws InvalidArgumentException If the value is invalid
56
     *
57
     * @return int
58
     */
59 80
    protected function filterInteger(int $value, int $min_value, string $error_message): int
60
    {
61 80
        if ($value < $min_value) {
62 10
            throw new InvalidArgumentException($error_message);
63
        }
64
65 72
        return $value;
66
    }
67
68
    /**
69
     * Validates the array to be used by the fetchAssoc method
70
     *
71
     * @param array $keys
72
     *
73
     * @throws InvalidArgumentException If the submitted array fails the assertion
74
     *
75
     * @return array
76
     */
77 36
    protected function filterColumnNames(array $keys): array
78
    {
79 36
        if (empty($keys)) {
80 2
            return $keys;
81
        }
82
83 34
        if ($keys !== array_unique(array_filter($keys, 'is_string'))) {
84 2
            throw new InvalidArgumentException('Use a flat array with unique string values');
85
        }
86
87 32
        return $keys;
88
    }
89
}
90