<?php
/**
* Data Validation Functions for Staging Environments
* Minimal Dependencies
*/
/**
* Validates email address format.
*
* @param string $email The email address to validate.
* @return bool True if the email is valid, false otherwise.
*/
function validateEmail(string $email): bool
{
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
/**
* Validates string length.
*
* @param string $string The string to validate.
* @param int $minLength The minimum length of the string.
* @param int $maxLength The maximum length of the string.
* @return bool True if the string is within the specified length, false otherwise.
*/
function validateStringLength(string $string, int $minLength, int $maxLength): bool
{
return strlen($string) >= $minLength && strlen($string) <= $maxLength;
}
/**
* Validates numeric value.
*
* @param mixed $value The value to validate.
* @param float|int $min The minimum allowed value.
* @param float|int $max The maximum allowed value.
* @return bool True if the value is a number within the range, false otherwise.
*/
function validateNumeric(mixed $value, float|int $min, float|int $max): bool
{
if (!is_numeric($value)) {
return false;
}
return $value >= $min && $value <= $max;
}
/**
* Validates an array of data.
*
* @param array $data The array of data to validate.
* @param array $rules An associative array defining validation rules.
* Example: ['field1' => 'required|string:50', 'field2' => 'numeric:0-100']
* @return array An associative array containing validation errors. Empty array if valid.
*/
function validateData(array $data, array $rules): array
{
$errors = [];
foreach ($rules as $field => $rule) {
if (!isset($data[$field])) {
if ($rule === 'required') {
$errors[$field] = 'This field is required.';
}
} else {
switch ($rule) {
case 'required':
if (empty($data[$field])) {
$errors[$field] = 'This field is required.';
}
break;
case 'string':
if (!is_string($data[$field])) {
$errors[$field] = 'This field must be a string.';
} else {
$length = (int)substr($rule, strpos($rule, ':') + 1);
if (!validateStringLength($data[$field], 1, $length)) {
$errors[$field] = 'This field must be between ' . $minLength . ' and ' . $maxLength . ' characters.';
}
}
break;
case 'numeric':
$range = explode('-', substr($rule, strpos($rule, ':') + 1));
if (!is_numeric($data[$field])) {
$errors[$field] = 'This field must be a number.';
} else {
$min = (float)$range[0];
$max = (float)$range[1];
if (!validateNumeric($data[$field], $min, $max)) {
$errors[$field] = 'This field must be between ' . $min . ' and ' . $max . '.';
}
}
break;
case 'email':
if (!validateEmail($data[$field])) {
$errors[$field] = 'This field must be a valid email address.';
}
break;
default:
$errors[$field] = 'Invalid validation rule: ' . $rule;
}
}
}
return $errors;
}
//Example Usage
/*
$data = [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'age' => 30,
'city' => 'New York',
'zip' => 10001
];
$rules = [
'name' => 'required|string:50',
'email' => 'required|
Add your comment