<?php
/**
* Flags anomalies in form fields for maintenance tasks.
* Dry-run mode included.
*
* @param array $form_data An associative array of form field data.
* @param array $thresholds An associative array defining anomaly thresholds for each field type.
* @param bool $dry_run Whether to only flag anomalies without actually performing any actions.
* @return array An array of flagged anomalies.
*/
function flagFormFieldAnomalies(array $form_data, array $thresholds, bool $dry_run = true): array
{
$anomalies = [];
foreach ($thresholds as $field_type => $thresholds_for_type) {
if (isset($form_data[$field_type])) {
$value = $form_data[$field_type];
foreach ($thresholds_for_type as $threshold_name => $threshold_value) {
if (is_numeric($threshold_value)) {
// Numeric anomaly detection
if ($value < $threshold_value) {
$anomalies[] = [
'field_type' => $field_type,
'field_name' => $threshold_name,
'value' => $value,
'threshold' => $threshold_value,
'message' => "Value '$value' is below threshold '$threshold_value' for field type '$field_type'."
];
} elseif ($value > $threshold_value) {
$anomalies[] = [
'field_type' => $field_type,
'field_name' => $threshold_name,
'value' => $value,
'threshold' => $threshold_value,
'message' => "Value '$value' is above threshold '$threshold_value' for field type '$field_type'."
];
}
} elseif ($threshold_name == 'string_length') {
// String length anomaly detection
if (strlen($value) < $threshold_value) {
$anomalies[] = [
'field_type' => $field_type,
'field_name' => $threshold_name,
'value' => $value,
'threshold' => $threshold_value,
'message' => "String length '$strlen_value' is below threshold '$threshold_value' for field type '$field_type'."
];
} elseif (strlen($value) > $threshold_value) {
$anomalies[] = [
'field_type' => $field_type,
'field_name' => $threshold_name,
'value' => $value,
'threshold' => $threshold_value,
'message' => "String length '$strlen_value' is above threshold '$threshold_value' for field type '$field_type'."
];
}
}
}
}
}
return $anomalies;
}
// Example usage:
/*
$form_data = [
'age' => 25,
'email' => 'test@example.com',
'description' => 'This is a test string.',
'quantity' => 100
];
$thresholds = [
'age' => [
'min' => 18,
'max' => 65
],
'email' => [
'string_length' => 5,
],
'quantity' => [
'min' => 1,
'max' => 1000
]
];
$anomalies = flagFormFieldAnomalies($form_data, $thresholds, true); // Run in flag mode
// $anomalies = flagFormFieldAnomalies($form_data, $thresholds, false); // Run in actual action mode
print_r($anomalies);
*/
?>
Add your comment