From 95c3250ea157379d98b8e21ef241a13e6c86d5eb Mon Sep 17 00:00:00 2001 From: Juan Cristobal <65052633+juancristobalgd1@users.noreply.github.com> Date: Sun, 9 Jun 2024 12:46:05 +0200 Subject: [PATCH] update file --- src/functions.php | 2 +- src/libraries/Http/Response.php | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/functions.php b/src/functions.php index 2f7c4b3..8626402 100644 --- a/src/functions.php +++ b/src/functions.php @@ -639,7 +639,7 @@ function esc(string $text): string */ function csrf(): string { - return ''; + return ''; } } \ No newline at end of file diff --git a/src/libraries/Http/Response.php b/src/libraries/Http/Response.php index 645a995..7068192 100644 --- a/src/libraries/Http/Response.php +++ b/src/libraries/Http/Response.php @@ -122,7 +122,6 @@ public function make(?string $content = '', array $headers = [], int $status = 2 */ public function send(): void { - if (func_num_args() > 0) { $this->make(...func_get_args()); } @@ -277,6 +276,30 @@ public function download(string $filePath, string $fileName = null, array $addit return $this; } + /** + * Output content encoded as a Html string. + */ + public function toHtml(?string $content, int $statusCode = 200, string $charset = 'utf-8'): void + { + if (!is_string($content)) { + throw new RuntimeException('Content must be a string to be converted to HTML.'); + } + + $this->send($content, [], $statusCode, 'text/html; charset=' . $charset); + } + + /** + * Output content encoded as a Text string. + */ + public function toText($content, int $statusCode = 200, string $charset = 'utf-8'): void + { + if (!is_string($content)) { + throw new RuntimeException('Content must be a string to be converted to Text.'); + } + + $this->send($content, [], $statusCode, 'application/text', $charset); + } + /** * Output content encoded as a JSON string. */ @@ -300,7 +323,7 @@ public function toArray(string $content): array /** * Returns response in XML format **/ - public function toXml(mixed $data, int $statusCode = 200, string $charset = 'utf-8'): void + public function toXml(array|object $data, int $statusCode = 200, string $charset = 'utf-8'): void { $xmlContent = $this->convertToXml($data); if ($xmlContent !== null) { @@ -313,7 +336,7 @@ public function toXml(mixed $data, int $statusCode = 200, string $charset = 'utf /** * Converts data to XML format **/ - private function convertToXml($data): ?string + private function convertToXml(array|object $data): ?string { $xml = new \SimpleXMLElement(''); array_walk_recursive($data, [$xml, 'addChild']);