diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-24 14:16:07 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-24 14:16:07 +0300 |
| commit | 1bdf80397c19362c019cdd231e8813e251764370 (patch) | |
| tree | 126c1e0ffcc5664ac2b5883bec64007070411a9b /classes/Response.php | |
| parent | 1917ab77550613654620b6f5d4ad8570704246de (diff) | |
| download | the-epube-1bdf80397c19362c019cdd231e8813e251764370.tar.gz the-epube-1bdf80397c19362c019cdd231e8813e251764370.tar.xz | |
switch to PSR-4 router with namespaced handlers
Diffstat (limited to 'classes/Response.php')
| -rw-r--r-- | classes/Response.php | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/classes/Response.php b/classes/Response.php new file mode 100644 index 0000000..aa3302a --- /dev/null +++ b/classes/Response.php @@ -0,0 +1,45 @@ +<?php + +class Response +{ + private int $status = 200; + private array $headers = []; + private string $body = ''; + + public function withStatus(int $code, string $reasonPhrase = ''): self + { + $clone = clone $this; + $clone->status = $code; + return $clone; + } + + public function withHeader(string $name, string $value): self + { + $clone = clone $this; + $clone->headers[$name] = $value; + return $clone; + } + + public function getBody(): string + { + return $this->body; + } + + public function withBody(string $body): self + { + $clone = clone $this; + $clone->body = $body; + return $clone; + } + + public function send(): void + { + if ($this->headers) { + foreach ($this->headers as $name => $value) { + header("$name: $value"); + } + } + http_response_code($this->status); + echo $this->body; + } +} |
