fix: Response from arguments or array

This commit is contained in:
Victor Westerlund 2024-03-18 12:05:17 +01:00
parent 11fdfe9d36
commit 4513afcea2
2 changed files with 10 additions and 12 deletions

View file

@ -106,7 +106,7 @@
$resp_code = (int) explode(" ", $http_response_header[0])[1];
// Return response as [<http_status_code>, <resp_body_assoc_array>]
return [$resp_code, $resp];
return [$resp, $resp_code];
}
// Make request and return response over socket

View file

@ -3,21 +3,19 @@
namespace Reflect;
class Response {
public int $status;
public int $code;
public bool $ok = false;
private mixed $body;
public function __construct(array $response) {
// Pass response array into properties
[$this->status, $this->body] = $response;
public function __construct(string|array $resp, int $code = 200) {
// Get response body from array or directly from string
$this->body = is_array($resp) ? $resp[0] : $resp;
// Set response code from array or with method argument
$this->code = is_array($resp) ? (int) $resp[1] : $code;
$this->ok = $this->is_ok();
}
// A boolean indicating whether the response was successful (status in the range 200 299) or not
public function is_ok(): bool {
return $this->status >= 200 && $this->status < 300;
// Response code is within the Success range
$this->ok = $this->code >= 200 && $this->code < 300;
}
// Parse JSON from response body and return as PHP array
@ -26,7 +24,7 @@
}
// Return response body as-is
public function text() {
public function output() {
return $this->body;
}
}