Completed
Push — master ( 8ee32c...65b5b3 )
by Marcin
29s queued 11s
created

Validator::assertString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcinOrlowski\ResponseBuilder;
5
6
/**
7
 * Laravel API Response Builder
8
 *
9
 * @package   MarcinOrlowski\ResponseBuilder
10
 *
11
 * @author    Marcin Orlowski <mail (#) marcinOrlowski (.) com>
12
 * @copyright 2016-2019 Marcin Orlowski
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
14
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
15
 */
16
class Validator
17
{
18
    /** @var string */
19
    public const TYPE_STRING = 'string';
20
21
    /** @var string */
22
    public const TYPE_INTEGER = 'integer';
23
24
    /** @var string */
25
    public const TYPE_BOOL = 'boolean';
26
27
    /**
28 44
     * Checks if given $val is of type integer
29
     *
30 44
     * @param string $key Name of the key to be used if exception is thrown.
31 3
     * @param mixed  $var Data to validated.
32 3
     *
33
     * @return void
34 41
     *
35
     * @throws \InvalidArgumentException
36
     */
37
    public static function assertInt(string $key, $var): void
38
    {
39
        self::assertType($key, $var, [self::TYPE_INTEGER]);
40
    }
41
42
    /**
43
     * Checks if given $val is of type string
44
     *
45
     * @param string $key Name of the key to be used if exception is thrown.
46
     * @param mixed  $var Data to validated.
47 35
     *
48
     * @return void
49 35
     *
50
     * @throws \InvalidArgumentException
51 34
     */
52 1
    public static function assertString(string $key, $var): void
53 1
    {
54
        self::assertType($key, $var, [self::TYPE_STRING]);
55
    }
56 33
57 4
    /**
58 4
     * @param string $key Name of the key to be used if exception is thrown.
59
     * @param mixed  $var Data to validated.
60 30
     * @param int    $min Min allowed value (inclusive)
61
     * @param int    $max Max allowed value (inclusive)
62
     *
63
     * @return void
64
     *
65
     * @throws \InvalidArgumentException
66
     * @throws \RuntimeException
67
     */
68
    public static function assertIntRange(string $key, $var, int $min, int $max): void
69
    {
70
        self::assertInt($key, $var);
71
72
        if ($min > $max) {
73
            throw new \RuntimeException(
74
                sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $key));
75
        }
76
77
        if (($min > $var) || ($var > $max)) {
78
            throw new \InvalidArgumentException(
79
                sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $key, $var, $min, $max));
80
        }
81
    }
82
83
    /**
84
     * Checks if $item (of name $key) is of type that is include in $allowed_types.
85
     *
86
     * @param string $key
87
     * @param mixed  $var
88
     * @param array[string]  $allowed_types
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[string] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
89
     *
90
     * @return void
91
     *
92
     * @throws \InvalidArgumentException
93
     */
94
    public static function assertType(string $key, $var, array $allowed_types): void
95
    {
96
        $type = gettype($var);
97
        if (!in_array($type, $allowed_types)) {
98
            $msg = sprintf('"%s" must be one of allowed types: %s (%s given)', $key, implode(', ', $allowed_types), gettype($var));
99
            throw new \InvalidArgumentException($msg);
100
        }
101
    }
102
}
103