ValidatorTrait   A
last analyzed

↳ Parent: Project

Coupling/Cohesion

Components 0
Dependencies 2

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 99
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 14
lcom 0
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A filterIterable() 0 8 4
A filterControl() 0 8 2
A filterColumnNames() 0 8 3
A filterMinRange() 0 8 2
A filterEncoding() 0 10 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
use League\Csv\Exception\OutOfRangeException;
19
use Traversable;
20
21
/**
22
 *  A trait to validate properties
23
 *
24
 * @package  League.csv
25
 * @since    9.0.0
26
 * @author   Ignace Nyamagana Butera <[email protected]>
27
 * @internal Use to validate incoming data
28
 */
29
trait ValidatorTrait
30
{
31
    /**
32
     * Validate an integer minimal range
33
     *
34
     * @param int    $value
35
     * @param int    $min_value
36
     * @param string $error_message
37
     *
38
     * @throws InvalidArgumentException If the value is invalid
39
     *
40
     * @return int
41
     */
42 98
    protected function filterMinRange(int $value, int $min_value, string $error_message): int
43
    {
44 98
        if ($value >= $min_value) {
45 90
            return $value;
46
        }
47
48 12
        throw new OutOfRangeException($error_message);
49
    }
50
51
    /**
52
     * Validate the argument given is an iterable
53
     *
54
     * @param array|Traversable $iterable
55
     * @param string            $method
56
     *
57
     * @throws InvalidArgumentException If the submitted value is not iterable
58
     *
59
     * @return array|Traversable
60
     */
61 16
    protected function filterIterable($iterable, string $method)
62
    {
63 16
        if (is_array($iterable) || $iterable instanceof Traversable) {
64 14
            return $iterable;
65
        }
66
67 2
        throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be iterable, %s given', $method, is_object($iterable) ? get_class($iterable) : gettype($iterable)));
68
    }
69
70
    /**
71
     * Filter Csv control character
72
     *
73
     * @param string $char   Csv control character
74
     * @param string $type   Csv control character type
75
     * @param string $method Method call
76
     *
77
     * @throws InvalidArgumentException If the Csv control character is not one character only.
78
     *
79
     * @return string
80
     */
81 56
    protected function filterControl(string $char, string $type, string $method)
82
    {
83 56
        if (1 == strlen($char)) {
84 54
            return $char;
85
        }
86
87 8
        throw new InvalidArgumentException(sprintf('%s: %s must be a single character', $method, $type));
88
    }
89
90
    /**
91
     * Validates the array to be used by the fetchAssoc method
92
     *
93
     * @param array $keys
94
     *
95
     * @throws InvalidArgumentException If the submitted array fails the assertion
96
     *
97
     * @return array
98
     */
99 44
    protected function filterColumnNames(array $keys): array
100
    {
101 44
        if (empty($keys) || $keys === array_unique(array_filter($keys, 'is_string'))) {
102 42
            return $keys;
103
        }
104
105 2
        throw new InvalidArgumentException('Use a flat array with unique string values');
106
    }
107
108
    /**
109
     * Filter encoding charset
110
     *
111
     * @param string $encoding
112
     *
113
     * @throws InvalidArgumentException if the charset is malformed
114
     *
115
     * @return string
116
     */
117 12
    protected function filterEncoding(string $encoding)
118
    {
119 12
        $encoding = strtoupper(str_replace('_', '-', $encoding));
120 12
        $test = filter_var($encoding, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
121 12
        if ($test === $encoding && $encoding != '') {
122 10
            return $encoding;
123
        }
124
125 2
        throw new InvalidArgumentException('Invalid Character Error');
126
    }
127
}
128