<?php
/**
* Checks constraints of data entries for migration.
*
* Handles potential errors gracefully, logging them and continuing with the next entry.
*
* @param array $data The data entry to validate.
* @param array $constraints An array of constraint rules. Each rule should be an associative array
* with keys like 'field', 'type', 'value', 'message'.
* Example: ['field' => 'email', 'type' => 'string', 'value' => 'email_regex', 'message' => 'Invalid email format']
*
* @return bool True if the entry passes all constraints, false otherwise.
*/
function validateDataEntry(array $data, array $constraints): bool
{
foreach ($constraints as $constraint) {
$field = $constraint['field'];
$type = $constraint['type'];
$value = $constraint['value'];
$message = $constraint['message'];
// Check if the field exists in the data. Important for robustness
if (!isset($data[$field])) {
error_log("Data validation error: Field '$field' missing from data.");
continue; // Skip to the next constraint
}
$dataValue = $data[$field];
switch ($type) {
case 'string':
if (!is_string($dataValue)) {
error_log("Data validation error: Field '$field' must be a string.");
return false;
}
break;
case 'integer':
if (!is_int($dataValue)) {
error_log("Data validation error: Field '$field' must be an integer.");
return false;
}
break;
case 'float':
if (!is_numeric($dataValue) || !is_float($dataValue)) {
error_log("Data validation error: Field '$field' must be a float.");
return false;
}
break;
case 'email':
if (!filter_var($dataValue, FILTER_VALIDATE_EMAIL)) {
error_log("Data validation error: Field '$field' is not a valid email address.");
return false;
}
break;
case 'regex':
if (!preg_match($value, $dataValue)) {
error_log("Data validation error: Field '$field' does not match the regex pattern.");
return false;
}
break;
case 'required':
if (empty($dataValue)) {
error_log("Data validation error: Field '$field' is required.");
return false;
}
break;
case 'min_length':
if (strlen($dataValue) < $constraint['min_length']) {
error_log("Data validation error: Field '$field' must be at least " . $constraint['min_length'] . " characters long.");
return false;
}
break;
default:
error_log("Data validation error: Unknown constraint type '$type' for field '$field'.");
return false;
}
}
return true; // All constraints passed
}
// Example Usage:
/*
$data = [
'id' => 123,
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'age' => 30,
'description' => 'This is a test string',
];
$constraints = [
['field' => 'id', 'type' => 'integer'],
['field' => 'name', 'type' => 'string'],
['field' => 'email', 'type' => 'email'],
['field' => 'age', 'type' => 'integer', 'min_length' => 1],
['field' => 'description', 'type' => 'string', 'required' => true],
['field' => 'phone', 'type' => 'string', 'message' => 'Phone number is required'], // Example of a missing field.
];
if (validateDataEntry($data, $constraints)) {
echo "Data entry is valid.\n";
} else {
echo "Data entry is invalid.\n";
}
*/
?>
Add your comment