. * */ namespace SP; defined('APP_ROOT') || die(_('No es posible acceder directamente a este archivo')); /** * 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 = array("&", "<", ">", "\"", "\'"); $arrStrTo = array("&", "<", ">", """, "'"); $cleanDescription = str_replace($arrStrFrom, $arrStrTo, $description); $xml = "\n"; $xml .= "\n" . $status . "\n " . $cleanDescription . "\n"; header("Content-Type: application/xml"); exit($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_string($data) && !is_array($data)) { return false; } $arrStrFrom = array("\\", '"', "'"); $arrStrTo = array("\\", '\"', "\'"); if (!is_array($data)) { $json = array( 'status' => $status, 'description' => str_replace($arrStrFrom, $arrStrTo, $data), 'action' => $action ); } else { array_walk($data, function (&$value, &$key) use ($arrStrFrom, $arrStrTo) { return str_replace($arrStrFrom, $arrStrTo, $value); } ); $data['status'] = $status; $data['action'] = $action; $json = $data; } header('Content-type: application/json'); exit(json_encode($json)); } }