ValidatorTrait   A
last analyzed

↳ Parent: Project

Coupling/Cohesion

Components 0
Dependencies 2

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterMinRange() 0 8 2
A filterIterable() 0 8 4
A filterControl() 0 8 2
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 124
    protected function filterMinRange(int $value, int $min_value, string $error_message): int
43
    {
44 124
        if ($value >= $min_value) {
45 114
            return $value;
46
        }
47
48 14
        throw new OutOfRangeException($error_message);
49
    }
50
51
    /**
52
     * Validate the argument given is an iterable
53
     *
54
     * @param array|Traversable $iterable
55
     *
56
     * @throws InvalidArgumentException If the submitted value is not iterable
57
     *
58
     * @return array|Traversable
59
     */
60 18
    protected function filterIterable($iterable)
61
    {
62 18
        if (is_array($iterable) || $iterable instanceof Traversable) {
63 16
            return $iterable;
64
        }
65
66 2
        throw new InvalidArgumentException(sprintf('Argument passed must be iterable, %s given', is_object($iterable) ? get_class($iterable) : gettype($iterable)));
67
    }
68
69
    /**
70
     * Filter Csv control character
71
     *
72
     * @param string $char Csv control character
73
     * @param string $type Csv control character type
74
     *
75
     * @throws InvalidArgumentException If the Csv control character is not one character only.
76
     *
77
     * @return string
78
     */
79 48
    protected function filterControl(string $char, string $type): string
80
    {
81 48
        if (1 == strlen($char)) {
82 46
            return $char;
83
        }
84
85 8
        throw new InvalidArgumentException(sprintf('%s must be a single character', $type));
86
    }
87
}
88