239 lines
6.8 KiB
PHP
239 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace WizdomNetworks\WizeWeb\Utilities;
|
|
|
|
use WizdomNetworks\WizeWeb\Utilities\Logger;
|
|
use WizdomNetworks\WizeWeb\Utilities\ErrorHandler;
|
|
|
|
/**
|
|
* Validator Utility
|
|
*
|
|
* Provides methods for validating user input.
|
|
*/
|
|
class Validator
|
|
{
|
|
/**
|
|
* Check if a value is non-empty.
|
|
*
|
|
* @param string $value The value to check.
|
|
* @return bool True if the value is not empty, false otherwise.
|
|
*/
|
|
public static function isRequired(string $value): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Checking if value is required: $value");
|
|
|
|
$isValid = !empty(trim($value));
|
|
|
|
if (!$isValid) {
|
|
Logger::warning("[WARNING] Value is required but empty.");
|
|
}
|
|
|
|
return $isValid;
|
|
} catch (\Throwable $e) {
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate an email address.
|
|
*
|
|
* @param string $email The email address to validate.
|
|
* @return bool True if the email is valid, false otherwise.
|
|
*/
|
|
public static function isEmail(string $email): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Validating email address: $email");
|
|
|
|
$isValid = filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
|
|
|
|
if (!$isValid) {
|
|
Logger::warning("[WARNING] Invalid email address: $email");
|
|
}
|
|
|
|
return $isValid;
|
|
} catch (\Throwable $e) {
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate a URL.
|
|
*
|
|
* @param string $url The URL to validate.
|
|
* @return bool True if the URL is valid, false otherwise.
|
|
*/
|
|
public static function isURL(string $url): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Validating URL: $url");
|
|
|
|
$isValid = filter_var($url, FILTER_VALIDATE_URL) !== false;
|
|
|
|
if (!$isValid) {
|
|
Logger::warning("[WARNING] Invalid URL: $url");
|
|
}
|
|
|
|
return $isValid;
|
|
} catch (\Throwable $e) {
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a string matches a given regular expression.
|
|
*
|
|
* @param string $string The string to validate.
|
|
* @param string $pattern The regular expression to match.
|
|
* @return bool True if the string matches the pattern, false otherwise.
|
|
*/
|
|
public static function matchesRegex(string $string, string $pattern): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Validating string against regex: Pattern=$pattern");
|
|
|
|
$isValid = preg_match($pattern, $string) === 1;
|
|
|
|
if (!$isValid) {
|
|
Logger::warning("[WARNING] String does not match regex: $string");
|
|
}
|
|
|
|
return $isValid;
|
|
} catch (\Throwable $e) {
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a string has a minimum length.
|
|
*
|
|
* @param string $string The string to check.
|
|
* @param int $minLength The minimum length.
|
|
* @return bool True if the string meets the minimum length, false otherwise.
|
|
*/
|
|
public static function hasMinLength(string $string, int $minLength): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Checking if string has minimum length: $minLength");
|
|
|
|
$isValid = strlen(trim($string)) >= $minLength;
|
|
|
|
if (!$isValid) {
|
|
Logger::warning("[WARNING] String is shorter than minimum length: $string");
|
|
}
|
|
|
|
return $isValid;
|
|
} catch (\Throwable $e) {
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a string has a maximum length.
|
|
*
|
|
* @param string $string The string to check.
|
|
* @param int $maxLength The maximum length.
|
|
* @return bool True if the string meets the maximum length, false otherwise.
|
|
*/
|
|
public static function hasMaxLength(string $string, int $maxLength): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Checking if string has maximum length: $maxLength");
|
|
|
|
$isValid = strlen(trim($string)) <= $maxLength;
|
|
|
|
if (!$isValid) {
|
|
Logger::warning("[WARNING] String exceeds maximum length: $string");
|
|
}
|
|
|
|
return $isValid;
|
|
} catch (\Throwable $e) {
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate a string length.
|
|
*
|
|
* @param string $input The string to validate.
|
|
* @param int $min Minimum length.
|
|
* @param int $max Maximum length.
|
|
* @return bool True if the string length is valid, false otherwise.
|
|
*/
|
|
public static function validateStringLength(string $input, int $min, int $max): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Validating string length: Input='$input', Min=$min, Max=$max");
|
|
|
|
$length = strlen($input);
|
|
$isValid = $length >= $min && $length <= $max;
|
|
|
|
if (!$isValid) {
|
|
Logger::warning("[WARNING] Invalid string length: $length (Expected between $min and $max)");
|
|
}
|
|
|
|
return $isValid;
|
|
} catch (\Throwable $e) {
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate a boolean value.
|
|
*
|
|
* @param mixed $input The input to validate as a boolean.
|
|
* @return bool True if the input is a valid boolean, false otherwise.
|
|
*/
|
|
public static function validateBoolean($input): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Validating boolean input: $input");
|
|
|
|
$isValid = is_bool(filter_var($input, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
|
|
|
|
if (!$isValid) {
|
|
Logger::warning("[WARNING] Invalid boolean input: $input");
|
|
}
|
|
|
|
return $isValid;
|
|
} catch (\Throwable $e) {
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate a date format.
|
|
*
|
|
* @param string $date The date string to validate.
|
|
* @param string $format The expected date format (e.g., 'Y-m-d').
|
|
* @return bool True if the date matches the format, false otherwise.
|
|
*/
|
|
public static function validateDate(string $date, string $format = 'Y-m-d'): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Validating date: $date with format: $format");
|
|
|
|
$dateTime = \DateTime::createFromFormat($format, $date);
|
|
$isValid = $dateTime && $dateTime->format($format) === $date;
|
|
|
|
if (!$isValid) {
|
|
Logger::warning("[WARNING] Invalid date: $date (Expected format: $format)");
|
|
}
|
|
|
|
return $isValid;
|
|
} catch (\Throwable $e) {
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
}
|