. */ namespace SP\Http; use Klein\Response; /** * Class Xml * * @package SP\Http */ final class Xml { const SAFE = [ 'from' => ['&', '<', '>', '"', "\'"], 'to' => ['&', '<', '>', '"', '''] ]; /** * @var Response */ private $response; /** * Xml constructor. * * @param Response $response */ public function __construct(Response $response) { $this->response = $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 */ public function printXml(string $description, int $status = 1) { if (!is_string($description)) { return; } $xml[] = ''; $xml[] = ''; $xml[] = '' . $status . ''; $xml[] = '' . $this->safeString($description) . ''; $xml[] = ''; $this->response ->header('Content-Type', 'application/xml') ->body(implode(PHP_EOL, $xml)) ->send(true); } /** * @param string $string * * @return mixed */ public function safeString(string $string) { return str_replace(self::SAFE['from'], self::SAFE['to'], $string); } }