ValidatorTrait   A
last analyzed

Coupling/Cohesion

Components 0
Dependencies 2

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterMinRange() 0 8 2
A filterIterable() 0 8 3
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
use TypeError;
21
22
/**
23
 *  A trait to validate properties
24
 *
25
 * @package  League.csv
26
 * @since    9.0.0
27
 * @author   Ignace Nyamagana Butera <[email protected]>
28
 * @internal Use to validate incoming data
29
 */
30
trait ValidatorTrait
31
{
32
    /**
33
     * Validate an integer minimal range
34
     *
35
     * @param int    $value
36
     * @param int    $min_value
37
     * @param string $error_message
38
     *
39
     * @throws OutOfRangeException If the value is invalid
40
     *
41
     * @return int
42
     */
43 22
    protected function filterMinRange(int $value, int $min_value, string $error_message): int
44
    {
45 22
        if ($value >= $min_value) {
46 18
            return $value;
47
        }
48
49 4
        throw new OutOfRangeException(sprintf($error_message, $value));
50
    }
51
52
    /**
53
     * Validate the argument given is an iterable
54
     *
55
     * @param array|Traversable $iterable
56
     * @param string            $caller   public API method calling the method
57
     *
58
     * @throws TypeError If the submitted value is not iterable
59
     *
60
     * @return array|Traversable
61
     */
62 6
    protected function filterIterable($iterable, string $caller)
63
    {
64 6
        if (is_array($iterable) || $iterable instanceof Traversable) {
65 4
            return $iterable;
66
        }
67
68 2
        throw new TypeError(sprintf('%s() expects argument passed to be iterable, %s given', $caller, gettype($iterable)));
69
    }
70
71
    /**
72
     * Filter Csv control character
73
     *
74
     * @param string $char   Csv control character
75
     * @param string $type   Csv control character type
76
     * @param string $caller public API method calling the method
77
     *
78
     * @throws InvalidArgumentException If the Csv control character is not one character only.
79
     *
80
     * @return string
81
     */
82 14
    protected function filterControl(string $char, string $type, string $caller): string
83
    {
84 14
        if (1 == strlen($char)) {
85 14
            return $char;
86
        }
87
88 2
        throw new InvalidArgumentException(sprintf('%s() expects %s to be a single character %s given', $caller, $type, $char));
89
    }
90
}
91