. */ namespace SP\Http; use SP\Core\Exceptions\SPException; use SP\Util\Json; defined('APP_ROOT') || die(); /** * Esta clase es encargada de ejecutar acciones comunes para las funciones */ final class Response { /** * Devuelve una respuesta en formato XML con el estado y el mensaje. * * @param string $description mensaje a devolver * @param int $status devuelve el estado * * @return bool */ public static function printXml($description, $status = 1) { if (!is_string($description)) { return false; } $arrStrFrom = ['&', '<', '>', '"', "\'"]; $arrStrTo = ['&', '<', '>', '"', ''']; $cleanDescription = str_replace($arrStrFrom, $arrStrTo, $description); $xml[] = ''; $xml[] = ''; $xml[] = '' . $status . ''; $xml[] = '' . $cleanDescription . ''; $xml[] = ''; header('Content-Type: application/xml'); exit(implode(PHP_EOL, $xml)); } /** * Devuelve una respuesta en formato JSON con el estado y el mensaje. * * @param string|array $data mensaje a devolver * @param int $status devuelve el estado * @param string $action con la accion a realizar */ public static function printJson($data, $status = 1, $action = '') { if (!is_array($data)) { $json = [ 'status' => $status, 'description' => $data, 'action' => $action ]; } else { $data['status'] = $status; $data['action'] = $action; $json = $data; } header('Content-type: application/json; charset=utf-8'); try { exit(Json::getJson($json)); } catch (SPException $e) { $data['status'] = 1; $data['description'] = __($e->getMessage()); if (isset($data['html'])) { $data['html'] = __($e->getMessage()); } exit(json_encode($data)); } } }