|
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 Variable to be asserted. |
|
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 $name Label or name of the variable to be used in exception message (if thrown). |
|
46
|
|
|
* @param mixed $var Variable to be asserted. |
|
47
|
35 |
|
* |
|
48
|
|
|
* @return void |
|
49
|
35 |
|
* |
|
50
|
|
|
* @throws \InvalidArgumentException |
|
51
|
34 |
|
*/ |
|
52
|
1 |
|
public static function assertString(string $name, $var): void |
|
53
|
1 |
|
{ |
|
54
|
|
|
self::assertType($name, $var, [self::TYPE_STRING]); |
|
55
|
|
|
} |
|
56
|
33 |
|
|
|
57
|
4 |
|
/** |
|
58
|
4 |
|
* @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
59
|
|
|
* @param mixed $var Variable to be asserted. |
|
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 $name, $var, int $min, int $max): void |
|
69
|
|
|
{ |
|
70
|
|
|
self::assertInt($name, $var); |
|
71
|
|
|
|
|
72
|
|
|
if ($min > $max) { |
|
73
|
|
|
throw new \RuntimeException( |
|
74
|
|
|
sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name)); |
|
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.', $name, $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 $name Label or name of the variable to be used in exception message (if thrown). |
|
87
|
|
|
* @param mixed $var Variable to be asserted. |
|
88
|
|
|
* @param array $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER] |
|
89
|
|
|
* |
|
90
|
|
|
* @return void |
|
91
|
|
|
* |
|
92
|
|
|
* @throws \InvalidArgumentException |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function assertType(string $name, $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)', $name, implode(', ', $allowed_types), gettype($var)); |
|
99
|
|
|
throw new \InvalidArgumentException($msg); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|