1. <?php
  2. /**
  3. * Verifies the integrity of metadata for batch processing.
  4. * Handles potential edge cases like missing fields, incorrect data types, and invalid values.
  5. *
  6. * @param array $metadata An array of metadata for each item in the batch.
  7. * @param array $requiredFields An array of required metadata fields.
  8. * @return array An array of errors found during validation. Empty array if validation passes.
  9. */
  10. function validateMetadata(array $metadata, array $requiredFields): array
  11. {
  12. $errors = [];
  13. foreach ($metadata as $index => $item) {
  14. $itemErrors = [];
  15. foreach ($requiredFields as $field) {
  16. if (!isset($item[$field])) {
  17. $itemErrors[] = "Missing required field: " . $field;
  18. } elseif (empty($item[$field])) {
  19. $itemErrors[] = "Field '" . $field . "' cannot be empty.";
  20. } else {
  21. // Type validation and value checks
  22. $expectedType = null;
  23. $allowedValues = [];
  24. switch ($field) {
  25. case 'id':
  26. $expectedType = 'int';
  27. break;
  28. case 'status':
  29. $expectedType = 'string';
  30. $allowedValues = ['pending', 'processing', 'completed', 'failed'];
  31. break;
  32. case 'timestamp':
  33. $expectedType = 'int'; // Unix timestamp
  34. break;
  35. case 'data_type':
  36. $expectedType = 'string';
  37. $allowedValues = ['image', 'video', 'audio', 'text'];
  38. break;
  39. default:
  40. // Add more field type and value checks as needed
  41. break;
  42. }
  43. if ($expectedType !== null) {
  44. if (!is_int($item[$field]) && $expectedType === 'int') {
  45. $itemErrors[] = "Field '" . $field . "' must be an integer.";
  46. } elseif (!is_string($item[$field]) && $expectedType === 'string') {
  47. $itemErrors[] = "Field '" . $field . "' must be a string.";
  48. } elseif (!is_numeric($item[$field]) && $expectedType === 'float') {
  49. $itemErrors[] = "Field '" . $field . "' must be a number.";
  50. }
  51. }
  52. if (is_array($allowedValues) && !in_array($item[$field], $allowedValues)) {
  53. $itemErrors[] = "Field '" . $field . "' has an invalid value. Allowed values: " . implode(', ', $allowedValues);
  54. }
  55. }
  56. }
  57. if (!empty($itemErrors)) {
  58. $errors[] = [
  59. 'index' => $index,
  60. 'errors' => $itemErrors,
  61. ];
  62. }
  63. }
  64. return $errors;
  65. }
  66. // Example Usage (replace with your actual data)
  67. //$metadata = [
  68. // ['id' => 1, 'status' => 'processing', 'timestamp' => 1678886400, 'data_type' => 'image'],
  69. // ['id' => 2, 'status' => 'completed', 'timestamp' => 1678886460, 'data_type' => 'video'],
  70. // ['status' => '', 'timestamp' => 1678886520, 'data_type' => 'audio'], //Missing id and empty status
  71. // ['id' => 'abc', 'status' => 'pending', 'timestamp' => 1678886580, 'data_type' => 'text'], //Invalid id type
  72. // ['id' => 3, 'status' => 'invalid_status', 'timestamp' => 1678886640, 'data_type' => 'image'], //Invalid status
  73. //];
  74. //$requiredFields = ['id', 'status', 'timestamp', 'data_type'];
  75. // $validationErrors = validateMetadata($metadata, $requiredFields);
  76. // if (empty($validationErrors)) {
  77. // echo "Metadata is valid.\n";
  78. // } else {
  79. // echo "Validation errors:\n";
  80. // foreach ($validationErrors as $error) {
  81. // echo "Index: " . $error['index'] . ", Errors: " . implode(", ", $error['errors']) . "\n";
  82. // }
  83. // }

Add your comment