1. <?php
  2. /**
  3. * Flags anomalies in form fields for maintenance tasks.
  4. * Dry-run mode included.
  5. *
  6. * @param array $form_data An associative array of form field data.
  7. * @param array $thresholds An associative array defining anomaly thresholds for each field type.
  8. * @param bool $dry_run Whether to only flag anomalies without actually performing any actions.
  9. * @return array An array of flagged anomalies.
  10. */
  11. function flagFormFieldAnomalies(array $form_data, array $thresholds, bool $dry_run = true): array
  12. {
  13. $anomalies = [];
  14. foreach ($thresholds as $field_type => $thresholds_for_type) {
  15. if (isset($form_data[$field_type])) {
  16. $value = $form_data[$field_type];
  17. foreach ($thresholds_for_type as $threshold_name => $threshold_value) {
  18. if (is_numeric($threshold_value)) {
  19. // Numeric anomaly detection
  20. if ($value < $threshold_value) {
  21. $anomalies[] = [
  22. 'field_type' => $field_type,
  23. 'field_name' => $threshold_name,
  24. 'value' => $value,
  25. 'threshold' => $threshold_value,
  26. 'message' => "Value '$value' is below threshold '$threshold_value' for field type '$field_type'."
  27. ];
  28. } elseif ($value > $threshold_value) {
  29. $anomalies[] = [
  30. 'field_type' => $field_type,
  31. 'field_name' => $threshold_name,
  32. 'value' => $value,
  33. 'threshold' => $threshold_value,
  34. 'message' => "Value '$value' is above threshold '$threshold_value' for field type '$field_type'."
  35. ];
  36. }
  37. } elseif ($threshold_name == 'string_length') {
  38. // String length anomaly detection
  39. if (strlen($value) < $threshold_value) {
  40. $anomalies[] = [
  41. 'field_type' => $field_type,
  42. 'field_name' => $threshold_name,
  43. 'value' => $value,
  44. 'threshold' => $threshold_value,
  45. 'message' => "String length '$strlen_value' is below threshold '$threshold_value' for field type '$field_type'."
  46. ];
  47. } elseif (strlen($value) > $threshold_value) {
  48. $anomalies[] = [
  49. 'field_type' => $field_type,
  50. 'field_name' => $threshold_name,
  51. 'value' => $value,
  52. 'threshold' => $threshold_value,
  53. 'message' => "String length '$strlen_value' is above threshold '$threshold_value' for field type '$field_type'."
  54. ];
  55. }
  56. }
  57. }
  58. }
  59. }
  60. return $anomalies;
  61. }
  62. // Example usage:
  63. /*
  64. $form_data = [
  65. 'age' => 25,
  66. 'email' => 'test@example.com',
  67. 'description' => 'This is a test string.',
  68. 'quantity' => 100
  69. ];
  70. $thresholds = [
  71. 'age' => [
  72. 'min' => 18,
  73. 'max' => 65
  74. ],
  75. 'email' => [
  76. 'string_length' => 5,
  77. ],
  78. 'quantity' => [
  79. 'min' => 1,
  80. 'max' => 1000
  81. ]
  82. ];
  83. $anomalies = flagFormFieldAnomalies($form_data, $thresholds, true); // Run in flag mode
  84. // $anomalies = flagFormFieldAnomalies($form_data, $thresholds, false); // Run in actual action mode
  85. print_r($anomalies);
  86. */
  87. ?>

Add your comment