1. <?php
  2. /**
  3. * Matches patterns of lists for development purposes with error logging.
  4. *
  5. * @param array $patterns An array of patterns to match. Each pattern should be an array
  6. * containing:
  7. * - 'type': 'exact', 'contains', 'starts_with', 'ends_with'
  8. * - 'value': The value to match against.
  9. * - 'list_key': The key in the list to check (if applicable).
  10. * - 'regex': Optional regex for more flexible matching (if type is 'contains').
  11. * @param array $data The data containing the lists to check.
  12. * @return array An array of matches, where each match is an array containing the pattern,
  13. * the list key (if applicable), and the matched value.
  14. */
  15. function matchListPatterns(array $patterns, array $data): array
  16. {
  17. $matches = [];
  18. foreach ($patterns as $pattern) {
  19. $type = $pattern['type'];
  20. $value = $pattern['value'];
  21. $listKey = $pattern['list_key'] ?? null; //Use null coalesce to handle missing list_key
  22. $regex = $pattern['regex'] ?? null;
  23. if (!is_array($pattern) || !isset($pattern['type']) || !isset($pattern['value'])) {
  24. error_log("Invalid pattern format: " . print_r($pattern, true));
  25. continue; // Skip invalid patterns
  26. }
  27. if (!is_array($data)) {
  28. error_log("Data is not an array.");
  29. continue; // Skip if data is not an array
  30. }
  31. foreach ($data as $listKeyData => $list) {
  32. if ($listKey !== null && array_key_exists($listKey, $list)) {
  33. $listValue = $list[$listKey];
  34. } else {
  35. $listValue = $list; //Use the whole list if listKey is not specified
  36. }
  37. try {
  38. switch ($type) {
  39. case 'exact':
  40. if ($listValue === $value) {
  41. $matches[] = [
  42. 'pattern' => $pattern,
  43. 'list_key' => $listKey,
  44. 'matched_value' => $listValue,
  45. ];
  46. break;
  47. }
  48. break;
  49. case 'contains':
  50. if (strpos($listValue, $value) !== false) {
  51. $matches[] = [
  52. 'pattern' => $pattern,
  53. 'list_key' => $listKey,
  54. 'matched_value' => $listValue,
  55. ];
  56. break;
  57. }
  58. break;
  59. case 'starts_with':
  60. if (strpos($listValue, $value . '') === 0) {
  61. $matches[] = [
  62. 'pattern' => $pattern,
  63. 'list_key' => $listKey,
  64. 'matched_value' => $listValue,
  65. ];
  66. break;
  67. }
  68. break;
  69. case 'ends_with':
  70. if (substr($listValue, -strlen($value)) === $value) {
  71. $matches[] = [
  72. 'pattern' => $pattern,
  73. 'list_key' => $listKey,
  74. 'matched_value' => $listValue,
  75. ];
  76. break;
  77. }
  78. break;
  79. case 'regex':
  80. if (preg_match($regex, $listValue)) {
  81. $matches[] = [
  82. 'pattern' => $pattern,
  83. 'list_key' => $listKey,
  84. 'matched_value' => $listValue,
  85. ];
  86. break;
  87. }
  88. break;
  89. default:
  90. error_log("Invalid pattern type: " . $type);
  91. break;
  92. }
  93. } catch (Exception $e) {
  94. error_log("Error during pattern matching: " . $e->getMessage());
  95. }
  96. }
  97. }
  98. return $matches;
  99. }
  100. // Example usage (for development)
  101. $patterns = [
  102. ['type' => 'exact', 'value' => 'apple', 'list_key' => 'fruit'],
  103. ['type' => 'contains', 'value' => 'ing', 'list_key' => 'verb'],
  104. ['type' => 'starts_with', 'value' => 'b', 'list_key' => 'color'],

Add your comment