1. <?php
  2. /**
  3. * Data Validation Functions for Staging Environments
  4. * Minimal Dependencies
  5. */
  6. /**
  7. * Validates email address format.
  8. *
  9. * @param string $email The email address to validate.
  10. * @return bool True if the email is valid, false otherwise.
  11. */
  12. function validateEmail(string $email): bool
  13. {
  14. return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
  15. }
  16. /**
  17. * Validates string length.
  18. *
  19. * @param string $string The string to validate.
  20. * @param int $minLength The minimum length of the string.
  21. * @param int $maxLength The maximum length of the string.
  22. * @return bool True if the string is within the specified length, false otherwise.
  23. */
  24. function validateStringLength(string $string, int $minLength, int $maxLength): bool
  25. {
  26. return strlen($string) >= $minLength && strlen($string) <= $maxLength;
  27. }
  28. /**
  29. * Validates numeric value.
  30. *
  31. * @param mixed $value The value to validate.
  32. * @param float|int $min The minimum allowed value.
  33. * @param float|int $max The maximum allowed value.
  34. * @return bool True if the value is a number within the range, false otherwise.
  35. */
  36. function validateNumeric(mixed $value, float|int $min, float|int $max): bool
  37. {
  38. if (!is_numeric($value)) {
  39. return false;
  40. }
  41. return $value >= $min && $value <= $max;
  42. }
  43. /**
  44. * Validates an array of data.
  45. *
  46. * @param array $data The array of data to validate.
  47. * @param array $rules An associative array defining validation rules.
  48. * Example: ['field1' => 'required|string:50', 'field2' => 'numeric:0-100']
  49. * @return array An associative array containing validation errors. Empty array if valid.
  50. */
  51. function validateData(array $data, array $rules): array
  52. {
  53. $errors = [];
  54. foreach ($rules as $field => $rule) {
  55. if (!isset($data[$field])) {
  56. if ($rule === 'required') {
  57. $errors[$field] = 'This field is required.';
  58. }
  59. } else {
  60. switch ($rule) {
  61. case 'required':
  62. if (empty($data[$field])) {
  63. $errors[$field] = 'This field is required.';
  64. }
  65. break;
  66. case 'string':
  67. if (!is_string($data[$field])) {
  68. $errors[$field] = 'This field must be a string.';
  69. } else {
  70. $length = (int)substr($rule, strpos($rule, ':') + 1);
  71. if (!validateStringLength($data[$field], 1, $length)) {
  72. $errors[$field] = 'This field must be between ' . $minLength . ' and ' . $maxLength . ' characters.';
  73. }
  74. }
  75. break;
  76. case 'numeric':
  77. $range = explode('-', substr($rule, strpos($rule, ':') + 1));
  78. if (!is_numeric($data[$field])) {
  79. $errors[$field] = 'This field must be a number.';
  80. } else {
  81. $min = (float)$range[0];
  82. $max = (float)$range[1];
  83. if (!validateNumeric($data[$field], $min, $max)) {
  84. $errors[$field] = 'This field must be between ' . $min . ' and ' . $max . '.';
  85. }
  86. }
  87. break;
  88. case 'email':
  89. if (!validateEmail($data[$field])) {
  90. $errors[$field] = 'This field must be a valid email address.';
  91. }
  92. break;
  93. default:
  94. $errors[$field] = 'Invalid validation rule: ' . $rule;
  95. }
  96. }
  97. }
  98. return $errors;
  99. }
  100. //Example Usage
  101. /*
  102. $data = [
  103. 'name' => 'John Doe',
  104. 'email' => 'john.doe@example.com',
  105. 'age' => 30,
  106. 'city' => 'New York',
  107. 'zip' => 10001
  108. ];
  109. $rules = [
  110. 'name' => 'required|string:50',
  111. 'email' => 'required|

Add your comment