wip: 2023-10-02T11:22:18+0200 (1696238538)

This commit is contained in:
Victor Westerlund 2023-10-02 11:54:06 +02:00
parent e67a59fa53
commit 11fdfe9d36
2 changed files with 45 additions and 0 deletions

13
src/Reflect/Method.php Normal file
View file

@ -0,0 +1,13 @@
<?php
namespace Reflect;
// Allowed HTTP verbs
enum Method: string {
case GET = "GET";
case POST = "POST";
case PUT = "PUT";
case DELETE = "DELETE";
case PATCH = "PATCH";
case OPTIONS = "OPTIONS";
}

32
src/Reflect/Response.php Normal file
View file

@ -0,0 +1,32 @@
<?php
namespace Reflect;
class Response {
public int $status;
public bool $ok = false;
private mixed $body;
public function __construct(array $response) {
// Pass response array into properties
[$this->status, $this->body] = $response;
$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;
}
// Parse JSON from response body and return as PHP array
public function json(bool $assoc = true): array {
return json_decode($this->body, $assoc);
}
// Return response body as-is
public function text() {
return $this->body;
}
}