Completed
Push — dev ( 724ebd...8f66c7 )
by Marcin
15s queued 10s
created

Validator::assertIsIntRange()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 3
nop 4
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 4
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
    /** @var string */
28
    public const TYPE_ARRAY = 'array';
29
30
    /** @var string */
31
    public const TYPE_OBJECT = 'object';
32
33
    /** @var string */
34
    public const TYPE_NULL = 'NULL';
35
36
    /**
37
     * Checks if given $val is of type boolean
38
     *
39
     * @param string $key Name of the key to be used if exception is thrown.
40
     * @param mixed  $var Variable to be asserted.
41
     *
42
     * @return void
43
     *
44
     * @throws \InvalidArgumentException
45
     */
46 2
    public static function assertIsBool(string $key, $var): void
47
    {
48 2
        self::assertIsType($key, $var, [self::TYPE_BOOL]);
49 1
    }
50
51
    /**
52
     * Checks if given $val is of type integer
53
     *
54
     * @param string $key Name of the key to be used if exception is thrown.
55
     * @param mixed  $var Variable to be asserted.
56
     *
57
     * @return void
58
     *
59
     * @throws \InvalidArgumentException
60
     */
61 106
    public static function assertIsInt(string $key, $var): void
62
    {
63 106
        self::assertIsType($key, $var, [self::TYPE_INTEGER]);
64 106
    }
65
66
    /**
67
     * Checks if given $val is of type array
68
     *
69
     * @param string $key Name of the key to be used if exception is thrown.
70
     * @param mixed  $var Variable to be asserted.
71
     *
72
     * @return void
73
     *
74
     * @throws \InvalidArgumentException
75
     */
76 2
    public static function assertIsArray(string $key, $var): void
77
    {
78 2
        self::assertIsType($key, $var, [self::TYPE_ARRAY]);
79 1
    }
80
81
    /**
82
     * Checks if given $val is an object
83
     *
84
     * @param string $key Name of the key to be used if exception is thrown.
85
     * @param mixed  $var Variable to be asserted.
86
     *
87
     * @return void
88
     *
89
     * @throws \InvalidArgumentException
90
     */
91 14
    public static function assertIsObject(string $key, $var): void
92
    {
93 14
        self::assertIsType($key, $var, [self::TYPE_OBJECT]);
94 13
    }
95
96
    /**
97
     * Checks if given $val is of type string
98
     *
99
     * @param string $name Label or name of the variable to be used in exception message (if thrown).
100
     * @param mixed  $var  Variable to be asserted.
101
     *
102
     * @return void
103
     *
104
     * @throws \InvalidArgumentException
105
     */
106 16
    public static function assertIsString(string $name, $var): void
107
    {
108 16
        self::assertIsType($name, $var, [self::TYPE_STRING]);
109 13
    }
110
111
    /**
112
     * @param string $name Label or name of the variable to be used in exception message (if thrown).
113
     * @param mixed  $var  Variable to be asserted.
114
     * @param int    $min  Min allowed value (inclusive)
115
     * @param int    $max  Max allowed value (inclusive)
116
     *
117
     * @return void
118
     *
119
     * @throws \InvalidArgumentException
120
     * @throws \RuntimeException
121
     */
122 106
    public static function assertIsIntRange(string $name, $var, int $min, int $max): void
123
    {
124 106
        self::assertIsInt($name, $var);
125
126 106
        if ($min > $max) {
127 1
            throw new \RuntimeException(
128 1
                sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name));
129
        }
130
131 106
        if (($min > $var) || ($var > $max)) {
132 6
            throw new \InvalidArgumentException(
133 6
                sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max));
134
        }
135 106
    }
136
137
    /**
138
     * Checks if $item (of name $key) is of type that is include in $allowed_types.
139
     *
140
     * @param string $name          Label or name of the variable to be used in exception message (if thrown).
141
     * @param mixed  $var           Variable to be asserted.
142
     * @param array  $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER]
143
     *
144
     * @return void
145
     *
146
     * @throws \InvalidArgumentException
147
     */
148 106
    public static function assertIsType(string $name, $var, array $allowed_types): void
149
    {
150 106
        $type = gettype($var);
151 106
        if (!in_array($type, $allowed_types)) {
152 13
            throw new \InvalidArgumentException(
153 13
                sprintf('"%s" must be one of allowed types: %s (%s given)',
154 13
                    $name, implode(', ', $allowed_types), gettype($var))
155
            );
156
        }
157 106
    }
158
159
    /**
160
     * Ensures given $http_code is valid code for error response.
161
     *
162
     * @param int $http_code
163
     */
164 19
    public static function assertErrorHttpCode(int $http_code): void
165
    {
166 19
        self::assertIsInt('http_code', $http_code);
167 19
        self::assertIsIntRange('http_code', $http_code,
168 19
            ResponseBuilder::ERROR_HTTP_CODE_MIN, ResponseBuilder::ERROR_HTTP_CODE_MAX);
169 19
    }
170
171
    /**
172
     * Ensures given $http_code is valid for response indicating sucessful operation.
173
     *
174
     * @param int $http_code
175
     */
176 18
    public static function assertOkHttpCode(int $http_code): void
177
    {
178 18
        self::assertIsInt('http_code', $http_code);
179 18
        self::assertIsIntRange('http_code', $http_code, 200, 299);
180 16
    }
181
182
    /**
183
     * Ensures $obj is instance of $cls.
184
     *
185
     * @param string $name
186
     * @param object $obj
187
     * @param string $cls
188
     */
189 4
    public static function assertInstanceOf(string $name, object $obj, string $cls): void
190
    {
191 4
        if (!($obj instanceof $cls)) {
192 1
            throw new \InvalidArgumentException(
193 1
                sprintf('"%s" must be instance of "%s".', $name, $cls)
194
            );
195
        }
196 3
    }
197
}
198