<?php
/**
* Formats API endpoint output for data migration.
*
* @param array $data The data to format.
* @param string $endpoint_name The name of the endpoint (for logging/debugging).
* @return string Formatted output.
*/
function formatApiOutput(array $data, string $endpoint_name): string
{
// Use json_encode for a standard JSON output
$json_data = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// Check if JSON encoding was successful
if ($json_data === false) {
return "Error encoding JSON: " . json_last_error_msg(); //Handle JSON encoding errors
}
// Return the formatted JSON data
return "Endpoint: $endpoint_name\n" . $json_data;
}
/**
* Example Usage
*/
// Sample data (replace with your actual data)
$migration_data = [
"id" => 123,
"name" => "Example Record",
"created_at" => "2023-10-27 10:00:00",
"status" => "active",
"details" => [
"field1" => "value1",
"field2" => "value2"
]
];
// Format the output for a specific endpoint
$endpoint = "migrate_records";
$formatted_output = formatApiOutput($migration_data, $endpoint);
//Output the formatted output
echo $formatted_output . "\n";
?>
Add your comment