Skip to content

Commit 7648d9f

Browse files
authored
Merge pull request #24 from alexsandrov16/dev
Fixed response methods
2 parents 5f92e32 + e3898f9 commit 7648d9f

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

docs/response.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ This method returns the message body in JSON format.
9797
- `$headers` (array): The headers of the response.
9898

9999
```php
100-
$response->json('{"message":"Hello World!"}', $status, $headers);
100+
Response::json('{"message":"Hello World!"}', $status, $headers);
101101
```
102102

103103
## Method `Response::plain(string $content, Status|array $status = Status::Ok, array $headers = [])`.
@@ -109,7 +109,7 @@ This method returns the message body in plain text format.
109109
- `$headers` (array): The headers of the response
110110

111111
```php
112-
$response->plain('Hello World!', $status, $headers);
112+
Response::plain('Hello World!', $status, $headers);
113113
```
114114

115115
## Method `Response::html(string $content, Status|array $status = Status::Ok, array $headers = [])`.
@@ -121,7 +121,7 @@ This method returns the message body in HTML format.
121121
- `$headers` (array): The headers of the response
122122

123123
```php
124-
$response->html('<h1>Hello World!</h1>', $status, $headers);
124+
Response::html('<h1>Hello World!</h1>', $status, $headers);
125125
```
126126

127127
## Method `Response::xml(string $content, Status|array $status = Status::Ok, array $headers = [])`.
@@ -142,5 +142,5 @@ $xml=<<<XML
142142
<body>Don't forget me this weekend!</body>
143143
</note>
144144
XML;
145-
$response->xml($xml, $status, $headers);
145+
Response::xml($xml, $status, $headers);
146146
```

src/Response.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,13 @@ protected function send(): string
165165
/**
166166
* Devuelve cuerpo del mensaje como JSON
167167
*/
168-
public static function json(array|object $content, Status|array $status = Status::Ok, array $headers = []): Response
168+
public static function json(array|string $content, Status|array $status = Status::Ok, array $headers = []): Response
169169
{
170170
$headers['content-type'] = 'application/json';
171-
return new static(json_encode($content, JSON_PRETTY_PRINT), $status, $headers);
171+
return new static(
172+
is_string($content) ? $content : json_encode($content,JSON_PRETTY_PRINT),
173+
$status,
174+
$headers);
172175
}
173176

174177
/**

0 commit comments

Comments
 (0)