@@ -15,88 +15,88 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Validator |
| 17 | 17 | { |
| 18 | - /** @var string */ |
|
| 19 | - public const TYPE_STRING = 'string'; |
|
| 18 | + /** @var string */ |
|
| 19 | + public const TYPE_STRING = 'string'; |
|
| 20 | 20 | |
| 21 | - /** @var string */ |
|
| 22 | - public const TYPE_INTEGER = 'integer'; |
|
| 21 | + /** @var string */ |
|
| 22 | + public const TYPE_INTEGER = 'integer'; |
|
| 23 | 23 | |
| 24 | - /** @var string */ |
|
| 25 | - public const TYPE_BOOL = 'boolean'; |
|
| 24 | + /** @var string */ |
|
| 25 | + public const TYPE_BOOL = 'boolean'; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Checks if given $val is of type integer |
|
| 29 | - * |
|
| 30 | - * @param string $key Name of the key to be used if exception is thrown. |
|
| 31 | - * @param mixed $var Variable to be asserted. |
|
| 32 | - * |
|
| 33 | - * @return void |
|
| 34 | - * |
|
| 35 | - * @throws \InvalidArgumentException |
|
| 36 | - */ |
|
| 37 | - public static function assertInt(string $key, $var): void |
|
| 38 | - { |
|
| 39 | - self::assertType($key, $var, [self::TYPE_INTEGER]); |
|
| 40 | - } |
|
| 27 | + /** |
|
| 28 | + * Checks if given $val is of type integer |
|
| 29 | + * |
|
| 30 | + * @param string $key Name of the key to be used if exception is thrown. |
|
| 31 | + * @param mixed $var Variable to be asserted. |
|
| 32 | + * |
|
| 33 | + * @return void |
|
| 34 | + * |
|
| 35 | + * @throws \InvalidArgumentException |
|
| 36 | + */ |
|
| 37 | + public static function assertInt(string $key, $var): void |
|
| 38 | + { |
|
| 39 | + self::assertType($key, $var, [self::TYPE_INTEGER]); |
|
| 40 | + } |
|
| 41 | 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 | - * |
|
| 48 | - * @return void |
|
| 49 | - * |
|
| 50 | - * @throws \InvalidArgumentException |
|
| 51 | - */ |
|
| 52 | - public static function assertString(string $name, $var): void |
|
| 53 | - { |
|
| 54 | - self::assertType($name, $var, [self::TYPE_STRING]); |
|
| 55 | - } |
|
| 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 | + * |
|
| 48 | + * @return void |
|
| 49 | + * |
|
| 50 | + * @throws \InvalidArgumentException |
|
| 51 | + */ |
|
| 52 | + public static function assertString(string $name, $var): void |
|
| 53 | + { |
|
| 54 | + self::assertType($name, $var, [self::TYPE_STRING]); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @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 | - * @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); |
|
| 57 | + /** |
|
| 58 | + * @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 | + * @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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 102 | } |