Completed
Pull Request — master (#178)
by ignace nyamagana
03:07
created

Validator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateInteger() 0 7 2
A validateString() 0 7 4
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 8.1.1
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
namespace League\Csv\Config;
14
15
use InvalidArgumentException;
16
17
/**
18
 *  A trait to configure and check CSV file and content
19
 *
20
 * @package League.csv
21
 * @since  6.0.0
22
 *
23
 */
24
trait Validator
25
{
26
    /**
27
     * Validate an integer
28
     *
29
     * @param int    $int
30
     * @param int    $minValue
31
     * @param string $errorMessage
32
     *
33
     * @throws InvalidArgumentException If the value is invalid
34
     *
35
     * @return int
36
     */
37
    protected static function validateInteger($int, $minValue, $errorMessage)
38
    {
39
        if (false === ($int = filter_var($int, FILTER_VALIDATE_INT, ['options' => ['min_range' => $minValue]]))) {
40
            throw new InvalidArgumentException($errorMessage);
41
        }
42
        return $int;
43
    }
44
45
    /**
46
     * validate a string
47
     *
48
     * @param mixed $str the value to evaluate as a string
49
     *
50
     * @throws InvalidArgumentException if the submitted data can not be converted to string
51
     *
52
     * @return string
53
     */
54
    protected static function validateString($str)
55
    {
56
        if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) {
57
            return (string) $str;
58
        }
59
        throw new InvalidArgumentException('Expected data must be a string or stringable');
60
    }
61
}
62