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
|
|
|
public const TYPE_STRING = 'string'; |
19
|
|
|
public const TYPE_INTEGER = 'integer'; |
20
|
|
|
public const TYPE_BOOL = 'boolean'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Checks if given $val is of type integer |
24
|
|
|
* |
25
|
|
|
* @param string $key Name of the key to be used if exception is thrown. |
26
|
|
|
* @param mixed $var Data to validated. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
* |
30
|
|
|
* @throws \InvalidArgumentException |
31
|
|
|
*/ |
32
|
45 |
|
public static function assertInt(string $key, $var): void |
33
|
|
|
{ |
34
|
45 |
|
self::assertType($key, $var, [self::TYPE_INTEGER]); |
35
|
42 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Checks if given $val is of type string |
39
|
|
|
* |
40
|
|
|
* @param string $key Name of the key to be used if exception is thrown. |
41
|
|
|
* @param mixed $var Data to validated. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
* |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
*/ |
47
|
12 |
|
public static function assertString(string $key, $var): void |
48
|
|
|
{ |
49
|
12 |
|
self::assertType($key, $var, [self::TYPE_STRING]); |
50
|
9 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $key Name of the key to be used if exception is thrown. |
54
|
|
|
* @param mixed $var Data to validated. |
55
|
|
|
* @param int $min Min allowed value (inclusive) |
56
|
|
|
* @param int $max Max allowed value (inclusive) |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
* |
60
|
|
|
* @throws \InvalidArgumentException |
61
|
|
|
* @throws \RuntimeException |
62
|
|
|
*/ |
63
|
35 |
|
public static function assertIntRange(string $key, $var, int $min, int $max): void |
64
|
|
|
{ |
65
|
35 |
|
self::assertInt($key, $var); |
66
|
|
|
|
67
|
34 |
|
if ($min > $max) { |
68
|
1 |
|
throw new \RuntimeException( |
69
|
1 |
|
sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $key)); |
70
|
|
|
} |
71
|
|
|
|
72
|
33 |
|
if (($min > $var) || ($var > $max)) { |
73
|
4 |
|
throw new \InvalidArgumentException( |
74
|
4 |
|
sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $key, $var, $min, $max)); |
75
|
|
|
} |
76
|
30 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Checks if $item (of name $key) is of type that is include in $allowed_types. |
80
|
|
|
* |
81
|
|
|
* @param string $key |
82
|
|
|
* @param mixed $var |
83
|
|
|
* @param array[string] $allowed_types |
|
|
|
|
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
* |
87
|
|
|
* @throws \InvalidArgumentException |
88
|
|
|
*/ |
89
|
48 |
|
public static function assertType(string $key, $var, array $allowed_types): void |
90
|
|
|
{ |
91
|
48 |
|
$type = gettype($var); |
92
|
48 |
|
if (!in_array($type, $allowed_types)) { |
93
|
7 |
|
$msg = sprintf('"%s" must be one of allowed types: %s (%s given)', $key, implode(', ', $allowed_types), gettype($var)); |
94
|
7 |
|
throw new \InvalidArgumentException($msg); |
95
|
|
|
} |
96
|
44 |
|
} |
97
|
|
|
} |
98
|
|
|
|