. */ namespace SP\Http; use SP\Util\Json; defined('APP_ROOT') || die(); /** * Esta clase es encargada de ejecutar acciones comunes para las funciones */ 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 * @return bool */ 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'); exit(Json::getJson($json)); } /** * Devuelve una respuesta de error en formato HTML. * * @param $data */ public static function printHtmlError($data) { $error = '
' . htmlentities($data) . '
'; exit($error); } }