http
1<?php
2
3declare(strict_types=1);
4
5namespace Upside\Http;
6
7class Response
8{
9private string $content;
10private Status $status;
11
12/**
13* @var array<string, string>
14*/
15private array $headers;
16private string $version = '1.0';
17
18/**
19* @param array<string, string> $headers
20*/
21public function __construct(string $content = '', Status $status = Status::OK, array $headers = [])
22{
23$this->content = $content;
24$this->status = $status;
25$this->headers = $headers;
26}
27
28public function send(): static
29{
30$this
31->send_headers()
32->send_content();
33
34return $this;
35}
36
37private function send_headers(): static
38{
39if (\headers_sent()) {
40return $this;
41}
42
43foreach ($this->headers as $name => $value) {
44\header($name . ': ' . $value, true, $this->status->code());
45}
46
47\header(\sprintf('HTTP/%s %s %s', $this->version, $this->status->code(), $this->status->text()), true, $this->status->code());
48
49return $this;
50}
51
52private function send_content(): static
53{
54echo $this->content;
55
56return $this;
57}
58
59public function set_status(Status $status): static
60{
61$this->status = $status;
62
63return $this;
64}
65
66public function set_version(string $version): static
67{
68$this->version = $version;
69
70return $this;
71}
72}
73