1. <?php
  2. /**
  3. * Matches user records against patterns defined in a configuration file.
  4. *
  5. * @param string $config_file Path to the configuration file.
  6. * @param array $user_records Array of user records (e.g., associative arrays).
  7. * @return array Associative array of matched user records, keyed by pattern name.
  8. */
  9. function matchUserRecords(string $config_file, array $user_records): array
  10. {
  11. // Load the configuration file.
  12. $config = loadConfig($config_file);
  13. if (!$config) {
  14. return []; // Return empty array if config loading fails.
  15. }
  16. $matched_users = [];
  17. // Iterate through the user records.
  18. foreach ($user_records as $user_id => $user) {
  19. // Iterate through the patterns in the configuration.
  20. foreach ($config['patterns'] as $pattern_name => $pattern) {
  21. if (matchesPattern($user, $pattern)) {
  22. // Add the user to the matched users array.
  23. if (!isset($matched_users[$pattern_name])) {
  24. $matched_users[$pattern_name] = [];
  25. }
  26. $matched_users[$pattern_name][] = $user;
  27. }
  28. }
  29. }
  30. return $matched_users;
  31. }
  32. /**
  33. * Loads the configuration file.
  34. *
  35. * @param string $config_file Path to the configuration file.
  36. * @return array|null Config array, or null on failure.
  37. */
  38. function loadConfig(string $config_file): ?array
  39. {
  40. // Check if the file exists.
  41. if (!file_exists($config_file)) {
  42. return null;
  43. }
  44. // Read the configuration file.
  45. $config = file_get_contents($config_file);
  46. if ($config === false) {
  47. return null;
  48. }
  49. // Decode the configuration file as an array.
  50. $config = json_decode($config, true);
  51. if ($config === null) {
  52. return null;
  53. }
  54. return $config;
  55. }
  56. /**
  57. * Checks if a user record matches a pattern.
  58. *
  59. * @param array $user User record.
  60. * @param array $pattern Pattern definition.
  61. * @return bool True if the user matches the pattern, false otherwise.
  62. */
  63. function matchesPattern(array $user, array $pattern): bool
  64. {
  65. // Check if the pattern is an array.
  66. if (!is_array($pattern)) {
  67. return false; // Invalid pattern format.
  68. }
  69. // Check if the user data matches the pattern criteria.
  70. foreach ($pattern['conditions'] as $field => $value) {
  71. // Check if the field exists in the user data.
  72. if (!isset($user[$field])) {
  73. return false; // Field not found in user data.
  74. }
  75. // Compare the field value with the pattern value.
  76. if ($user[$field] !== $value) {
  77. return false; // Field value does not match.
  78. }
  79. }
  80. return true; // User matches the pattern.
  81. }
  82. // Example usage (for testing):
  83. /*
  84. $user_records = [
  85. 1 => ['name' => 'Alice', 'age' => 30, 'city' => 'New York'],
  86. 2 => ['name' => 'Bob', 'age' => 25, 'city' => 'Los Angeles'],
  87. 3 => ['name' => 'Charlie', 'age' => 30, 'city' => 'Chicago'],
  88. ];
  89. $config_file = 'config.json'; // Create a config.json file with your patterns
  90. $matched_users = matchUserRecords($config_file, $user_records);
  91. print_r($matched_users);
  92. */
  93. ?>

Add your comment