Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| Validator | |
100.00% |
22 / 22 |
|
100.00% |
6 / 6 |
19 | |
100.00% |
1 / 1 |
| isValid | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
8 | |||
| isType | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| hasLength | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| contains | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| matches | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasLimit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Validation |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Validation; |
| 16 | |
| 17 | use phpOMS\Utils\StringUtils; |
| 18 | |
| 19 | /** |
| 20 | * Validator class. |
| 21 | * |
| 22 | * @package phpOMS\Validation |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | final class Validator extends ValidatorAbstract |
| 28 | { |
| 29 | /** |
| 30 | * Validate variable based on multiple factors. |
| 31 | * |
| 32 | * @param mixed $var Variable to validate |
| 33 | * @param array $constraints Constraints for validation |
| 34 | * |
| 35 | * @return bool |
| 36 | * |
| 37 | * @throws \BadFunctionCallException this exception is thrown if the callback is not callable |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | */ |
| 41 | public static function isValid(mixed $var, array $constraints = null) : bool |
| 42 | { |
| 43 | if ($constraints === null) { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | foreach ($constraints as $test => $settings) { |
| 48 | $callback = StringUtils::endsWith($test, 'Not') ? \substr($test, 0, -3) : (string) $test; |
| 49 | |
| 50 | if (!\is_callable($callback)) { |
| 51 | throw new \BadFunctionCallException(); |
| 52 | } |
| 53 | |
| 54 | $valid = empty($settings) ? $callback($var) : $callback($var, ...$settings); |
| 55 | $valid = (StringUtils::endsWith($test, 'Not') ? !$valid : $valid); |
| 56 | |
| 57 | if (!$valid) { |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Validate variable by type. |
| 67 | * |
| 68 | * @param object|string $var Variable to validate |
| 69 | * @param string|string[] $constraint Array of allowed types |
| 70 | * |
| 71 | * @return bool |
| 72 | * |
| 73 | * @since 1.0.0 |
| 74 | */ |
| 75 | public static function isType(object | string $var, string | array $constraint) : bool |
| 76 | { |
| 77 | if (!\is_array($constraint)) { |
| 78 | $constraint = [$constraint]; |
| 79 | } |
| 80 | |
| 81 | foreach ($constraint as $value) { |
| 82 | if (!\is_a($var, $value, true)) { |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Validate variable by length. |
| 92 | * |
| 93 | * @param string $var Variable to validate |
| 94 | * @param int $min Min. length |
| 95 | * @param int $max Max. length |
| 96 | * |
| 97 | * @return bool |
| 98 | * |
| 99 | * @since 1.0.0 |
| 100 | */ |
| 101 | public static function hasLength(string $var, int $min = 0, int $max = \PHP_INT_MAX) : bool |
| 102 | { |
| 103 | $length = \strlen($var); |
| 104 | |
| 105 | return $length <= $max && $length >= $min; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Validate variable by substring. |
| 110 | * |
| 111 | * @param string $var Variable to validate |
| 112 | * @param array|string $substr Substring |
| 113 | * |
| 114 | * @return bool |
| 115 | * |
| 116 | * @since 1.0.0 |
| 117 | */ |
| 118 | public static function contains(string $var, string | array $substr) : bool |
| 119 | { |
| 120 | return \is_string($substr) ? \strpos($var, $substr) !== false : StringUtils::contains($var, $substr); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Validate variable by pattern. |
| 125 | * |
| 126 | * @param string $var Variable to validate |
| 127 | * @param string $pattern Pattern for validation |
| 128 | * |
| 129 | * @return bool |
| 130 | * |
| 131 | * @since 1.0.0 |
| 132 | */ |
| 133 | public static function matches(string $var, string $pattern) : bool |
| 134 | { |
| 135 | return \preg_match($pattern, $var) === 1; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Validate variable by interval. |
| 140 | * |
| 141 | * @param int|float $var Variable to validate |
| 142 | * @param int|float $min Min. value |
| 143 | * @param int|float $max Max. value |
| 144 | * |
| 145 | * @return bool |
| 146 | * |
| 147 | * @since 1.0.0 |
| 148 | */ |
| 149 | public static function hasLimit(int | float $var, int | float $min = 0, int | float $max = \PHP_INT_MAX) : bool |
| 150 | { |
| 151 | return $var <= $max && $var >= $min; |
| 152 | } |
| 153 | } |