/** * * @param Request $request User Request * @param $source_address User Request source address * @param $destination_address User Request destination address * @param array $source_spatial_data Estimates returned by MySQL spatial query for source matches * @param array $destination_spatial_data Estimates returned by MySQL spatial query for destination matches * @param $gdm_request_potential_source_matches array Estimates returned by Google Distance Matrix API Request for source matches * @param $gdm_request_potential_destination_matches array Estimates returned by Google Distance Matrix API Request for destination matches * @return array */ public static function formatAPIResponse($request, $source_address, $destination_address, array $source_spatial_data, array $destination_spatial_data, $gdm_request_potential_source_matches, $gdm_request_potential_destination_matches) { $api_response = array(); $source_address_matched_request_ids_map = array_flip(array_keys($source_spatial_data)); $destination_address_matched_request_ids_map = array_flip(array_keys($destination_spatial_data)); $ideal_matches = array(); $api_response['request_id'] = $request->id; $api_response['pickup_time'] = RequestPickupTimes::formatPickUpTimesForAPI($request->requestPickupTimes->toArray()); $api_response['request_source_address'] = $source_address->full_address_text; $api_response['request_destination_address'] = $destination_address->full_address_text; $source_matches = self::formatAddressForAPIResponse($source_spatial_data, $gdm_request_potential_source_matches, $ideal_matches); $destination_matches = self::formatAddressForAPIResponse($destination_spatial_data, $gdm_request_potential_destination_matches, $ideal_matches, $source_address_matched_request_ids_map); foreach ($ideal_matches as $index => $match_request_id) { $api_response['best_matches'][$index]['request_id'] = $match_request_id; $api_response['best_matches'][$index]['pickup_times'] = $source_spatial_data[$match_request_id]['request_pickup_times']; //This is really bad, need to change this $api_response['best_matches'][$index]['matched_source'] = $source_matches[$source_address_matched_request_ids_map[$match_request_id]]; $api_response['best_matches'][$index]['matched_destination'] = $destination_matches[$destination_address_matched_request_ids_map[$match_request_id]]; } return $api_response; }