Passed
Push — dev ( 3ee8a0...9fe7d1 )
by Marcin
02:37
created

Validator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 79
ccs 20
cts 20
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertString() 0 3 1
A assertInt() 0 3 1
A assertType() 0 6 2
A assertIntRange() 0 12 4
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
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...
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