Completed
Push — master ( 32377d...16aae2 )
by ignace nyamagana
04:12
created

ValidatorTrait::filterEncoding()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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 16
    protected function filterIterable($iterable)
61
    {
62 16
        if (is_array($iterable) || $iterable instanceof Traversable) {
63 14
            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