$max) { $message = $message ?? sprintf("[Assertion failed] - Value must be between %s and %s", $min, $max); throw new AdminException($message); } } /** * 断言字符串匹配正则表达式 * * @throws \core\exception\handler\AdminException */ public static function matchesRegex(string $value, string $pattern, string $message = null): void { if (!preg_match($pattern, $value)) { $message = $message ?? sprintf("[Assertion failed] - Value does not match pattern %s", $pattern); throw new AdminException($message); } } /** * 断言两个值相等(松散比较 ==) * * @throws \core\exception\handler\AdminException */ public static function equals(mixed $actual, mixed $expected, string $message = null): void { if ($actual != $expected) { $message = $message ?? sprintf("[Assertion failed] - Expected %s, got %s", var_export($expected, true), var_export($actual, true)); throw new AdminException($message); } } /** * 断言两个值严格相等(===) * * @throws \core\exception\handler\AdminException */ public static function strictEquals(mixed $actual, mixed $expected, string $message = null): void { if ($actual !== $expected) { $message = $message ?? sprintf("[Assertion failed] - Expected strict %s, got %s", var_export($expected, true), var_export($actual, true)); throw new AdminException($message); } } /** * 断言值为有效的电子邮件格式 * * @throws \core\exception\handler\AdminException */ public static function isEmail(string $value, string $message = "[Assertion failed] - Value is not a valid email address"): void { if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { throw new AdminException($message); } } /** * 断言值为有效的URL格式 * * @throws \core\exception\handler\AdminException */ public static function isUrl(string $value, string $message = "[Assertion failed] - Value is not a valid URL"): void { if (!filter_var($value, FILTER_VALIDATE_URL)) { throw new AdminException($message); } } /** * 断言目录存在 * * @throws \core\exception\handler\AdminException */ public static function directoryExists(string $path, string $message = null): void { if (!is_dir($path)) { $message = $message ?? sprintf("[Assertion failed] - Directory %s does not exist", $path); throw new AdminException($message); } } /** * 断言文件存在 * * @throws \core\exception\handler\AdminException */ public static function fileExists(string $path, string $message = null): void { if (!file_exists($path)) { $message = $message ?? sprintf("[Assertion failed] - File %s does not exist", $path); throw new AdminException($message); } } }