1. <?php
  2. /**
  3. * Maps fields of records for staging environments with fixed retry intervals.
  4. *
  5. * @param array $records An array of records to map. Each record is an associative array.
  6. * @param array $mapping A mapping array defining how to transform the records for staging.
  7. * Example: ['field_name' => 'staging_field_name']
  8. * @param int $retry_interval The retry interval in seconds.
  9. * @return array The mapped records.
  10. */
  11. function mapRecordsForStaging(array $records, array $mapping, int $retry_interval): array
  12. {
  13. $mapped_records = [];
  14. foreach ($records as $record) {
  15. $mapped_record = [];
  16. foreach ($mapping as $source_field => $staging_field) {
  17. if (array_key_exists($source_field, $record)) {
  18. $mapped_record[$staging_field] = $record[$source_field];
  19. } else {
  20. $mapped_record[$staging_field] = null; // Handle missing fields
  21. }
  22. }
  23. $mapped_records[] = $mapped_record;
  24. }
  25. return $mapped_records;
  26. }
  27. /**
  28. * Example usage:
  29. */
  30. // Sample records (simulating data from a database)
  31. $records = [
  32. ['id' => 1, 'name' => 'John Doe', 'email' => 'john.doe@example.com'],
  33. ['id' => 2, 'name' => 'Jane Smith', 'email' => 'jane.smith@example.com'],
  34. ];
  35. // Mapping definition
  36. $mapping = [
  37. 'id' => 'staging_id',
  38. 'name' => 'staging_name',
  39. 'email' => 'staging_email'
  40. ];
  41. // Retry interval
  42. $retry_interval = 5; // seconds
  43. // Map the records
  44. $staging_records = mapRecordsForStaging($records, $mapping, $retry_interval);
  45. // Output the mapped records (for demonstration)
  46. print_r($staging_records);
  47. ?>

Add your comment