$rules) { $rules = preg_split('/\|/', $rules); foreach ($rules as $rule) { if (static::checkRule($haystack->{$target}, $rule)) { continue; } static::$lastFailed = $target . '.' . $rule; return false; } } return true; } /** * Checks if a rule is met. * * @param mixed $subject The value to verify. * @param string $rule The rule to test. * * @return bool * @throws Exception If the rule is not callable. */ public static function checkRule(mixed $subject, string $rule): bool { $arguments = preg_split('/[:,]/', $rule); $rule = [static::class, $arguments[0]]; $arguments[0] = $subject; if (is_callable($rule)) { return call_user_func_array($rule, $arguments); } throw new Exception('Bad rule: "' . preg_split('/::/', $rule)[1] . '"'); } /** * Verifies the rule in a negative way. * * @param mixed $subject The value to verify. * @param mixed $rule The rule to test. * * @return bool */ public static function not(mixed $subject, ...$rule): bool { return !static::checkRule($subject, join(':', $rule)); } /** * Checks if the value is defined/exists. * * @param mixed $subject The value to check. * * @return bool */ public static function exists(mixed $subject): bool { return isset($subject); } /** * Checks if the value is defined and not empty. * * @param mixed $subject The value to check. * * @return bool */ public static function required(mixed $subject): bool { return isset($subject) && !empty($subject); } /** * Checks if the value is numeric. * * @param mixed $subject The value to check. * * @return bool */ public static function number(mixed $subject): bool { return is_numeric($subject); } /** * Checks if the value is an integer. * * @param mixed $subject The value to check. * * @return bool */ public static function int(mixed $subject): bool { return filter_var($subject, FILTER_VALIDATE_INT) !== false; } /** * Checks if the value is a float. * * @param mixed $subject The value to check. * * @return bool */ public static function float(mixed $subject): bool { return filter_var($subject, FILTER_VALIDATE_FLOAT) !== false; } /** * Checks if the value is a boolean. * * @param mixed $subject The value to check. * * @return bool */ public static function bool(mixed $subject): bool { return filter_var($subject, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null; } /** * Checks if the value is a valid email address. * * @param mixed $subject The value to check. * * @return bool */ public static function email(mixed $subject): bool { return filter_var($subject, FILTER_VALIDATE_EMAIL) !== false; } /** * Checks if the value is a valid URL. * * @param mixed $subject The value to check. * * @return bool */ public static function url(mixed $subject): bool { return filter_var($subject, FILTER_VALIDATE_URL) !== false; } /** * Checks if the value is present in a list of allowed values. * * @param mixed $subject The value to check. * @param mixed ...$values A variable number of allowed values. * * @return bool */ public static function enum(mixed $subject, ...$values): bool { return in_array($subject, $values); } }