Completed
Push — master ( 6ca7fe...652926 )
by ignace nyamagana
03:13 queued 02:01
created

ValidatorTrait::filterNullableInteger()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 3
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 4
rs 9.2
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\LengthException;
18
use League\Csv\Exception\OutOfRangeException;
19
use TypeError;
20
21
/**
22
 *  A trait to validate csv control characters
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
     * Filter Csv control character
33
     *
34
     * @param string $char   Csv control character
35
     * @param string $type   Csv control character type
36
     * @param string $caller public API method calling the method
37
     *
38
     * @throws LengthException If the Csv control character is not one character only.
39
     *
40
     * @return string
41
     */
42 12
    protected function filterControl(string $char, string $type, string $caller): string
43
    {
44 12
        if (1 == strlen($char)) {
45 12
            return $char;
46
        }
47
48 2
        throw new LengthException(sprintf('%s() expects %s to be a single character %s given', $caller, $type, $char));
49
    }
50
51
52
    /**
53
     * Filter Header Offset
54
     *
55
     * @param int|null $value
56
     * @param int      $min_range
57
     * @param string   $error_message
58
     *
59
     * @throws TypError            if value is not a type integer or null
60
     * @throws OutOfRangeException if value is not in a valid int range
61
     */
62 18
    protected function filterNullableInteger($value, int $min_range, string $error_message)
63
    {
64 18
        if (null === $value) {
65 10
            return;
66
        }
67
68 10
        if (!is_int($value)) {
69 4
            throw new TypeError(sprintf('Expected argument to be null or a integer %s given', gettype($value)));
70
        }
71
72 6
        if ($value < $min_range) {
73 4
            throw new OutOfRangeException($error_message);
74
        }
75 4
    }
76
}
77