. */ namespace SP\Http; use SP\Bootstrap; 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)); } } /** * Devuelve una respuesta de error en formato HTML. * * @param $data */ public static function printHtmlError($data) { $error = '
' . htmlentities($data) . '
'; exit($error); } /** * Internal URL redirection * * @param $url */ public static function redirectInternal($url) { self::redirect(Bootstrap::$WEBURI . '/' . $url); } /** * URL redirection * * @param $url */ public static function redirect($url) { header('Location: ' . $url); die(); } }