1. <?php
  2. /**
  3. * Checks constraints of data entries for migration.
  4. *
  5. * Handles potential errors gracefully, logging them and continuing with the next entry.
  6. *
  7. * @param array $data The data entry to validate.
  8. * @param array $constraints An array of constraint rules. Each rule should be an associative array
  9. * with keys like 'field', 'type', 'value', 'message'.
  10. * Example: ['field' => 'email', 'type' => 'string', 'value' => 'email_regex', 'message' => 'Invalid email format']
  11. *
  12. * @return bool True if the entry passes all constraints, false otherwise.
  13. */
  14. function validateDataEntry(array $data, array $constraints): bool
  15. {
  16. foreach ($constraints as $constraint) {
  17. $field = $constraint['field'];
  18. $type = $constraint['type'];
  19. $value = $constraint['value'];
  20. $message = $constraint['message'];
  21. // Check if the field exists in the data. Important for robustness
  22. if (!isset($data[$field])) {
  23. error_log("Data validation error: Field '$field' missing from data.");
  24. continue; // Skip to the next constraint
  25. }
  26. $dataValue = $data[$field];
  27. switch ($type) {
  28. case 'string':
  29. if (!is_string($dataValue)) {
  30. error_log("Data validation error: Field '$field' must be a string.");
  31. return false;
  32. }
  33. break;
  34. case 'integer':
  35. if (!is_int($dataValue)) {
  36. error_log("Data validation error: Field '$field' must be an integer.");
  37. return false;
  38. }
  39. break;
  40. case 'float':
  41. if (!is_numeric($dataValue) || !is_float($dataValue)) {
  42. error_log("Data validation error: Field '$field' must be a float.");
  43. return false;
  44. }
  45. break;
  46. case 'email':
  47. if (!filter_var($dataValue, FILTER_VALIDATE_EMAIL)) {
  48. error_log("Data validation error: Field '$field' is not a valid email address.");
  49. return false;
  50. }
  51. break;
  52. case 'regex':
  53. if (!preg_match($value, $dataValue)) {
  54. error_log("Data validation error: Field '$field' does not match the regex pattern.");
  55. return false;
  56. }
  57. break;
  58. case 'required':
  59. if (empty($dataValue)) {
  60. error_log("Data validation error: Field '$field' is required.");
  61. return false;
  62. }
  63. break;
  64. case 'min_length':
  65. if (strlen($dataValue) < $constraint['min_length']) {
  66. error_log("Data validation error: Field '$field' must be at least " . $constraint['min_length'] . " characters long.");
  67. return false;
  68. }
  69. break;
  70. default:
  71. error_log("Data validation error: Unknown constraint type '$type' for field '$field'.");
  72. return false;
  73. }
  74. }
  75. return true; // All constraints passed
  76. }
  77. // Example Usage:
  78. /*
  79. $data = [
  80. 'id' => 123,
  81. 'name' => 'John Doe',
  82. 'email' => 'john.doe@example.com',
  83. 'age' => 30,
  84. 'description' => 'This is a test string',
  85. ];
  86. $constraints = [
  87. ['field' => 'id', 'type' => 'integer'],
  88. ['field' => 'name', 'type' => 'string'],
  89. ['field' => 'email', 'type' => 'email'],
  90. ['field' => 'age', 'type' => 'integer', 'min_length' => 1],
  91. ['field' => 'description', 'type' => 'string', 'required' => true],
  92. ['field' => 'phone', 'type' => 'string', 'message' => 'Phone number is required'], // Example of a missing field.
  93. ];
  94. if (validateDataEntry($data, $constraints)) {
  95. echo "Data entry is valid.\n";
  96. } else {
  97. echo "Data entry is invalid.\n";
  98. }
  99. */
  100. ?>

Add your comment