1. <?php
  2. /**
  3. * Formats API endpoint output for data migration.
  4. *
  5. * @param array $data The data to format.
  6. * @param string $endpoint_name The name of the endpoint (for logging/debugging).
  7. * @return string Formatted output.
  8. */
  9. function formatApiOutput(array $data, string $endpoint_name): string
  10. {
  11. // Use json_encode for a standard JSON output
  12. $json_data = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  13. // Check if JSON encoding was successful
  14. if ($json_data === false) {
  15. return "Error encoding JSON: " . json_last_error_msg(); //Handle JSON encoding errors
  16. }
  17. // Return the formatted JSON data
  18. return "Endpoint: $endpoint_name\n" . $json_data;
  19. }
  20. /**
  21. * Example Usage
  22. */
  23. // Sample data (replace with your actual data)
  24. $migration_data = [
  25. "id" => 123,
  26. "name" => "Example Record",
  27. "created_at" => "2023-10-27 10:00:00",
  28. "status" => "active",
  29. "details" => [
  30. "field1" => "value1",
  31. "field2" => "value2"
  32. ]
  33. ];
  34. // Format the output for a specific endpoint
  35. $endpoint = "migrate_records";
  36. $formatted_output = formatApiOutput($migration_data, $endpoint);
  37. //Output the formatted output
  38. echo $formatted_output . "\n";
  39. ?>

Add your comment