Json.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Response; |
| 6 | |
| 7 | use DomainException; |
| 8 | |
| 9 | class Json |
| 10 | { |
| 11 | public const MESSAGE = 'message'; |
| 12 | |
| 13 | protected array $parameters = []; |
| 14 | |
| 15 | protected array $headers = []; |
| 16 | |
| 17 | protected int $status_code = 200; |
| 18 | |
| 19 | public function __construct(array $parameters = []) |
| 20 | { |
| 21 | $this->set_header('Content-Type', 'application/json'); |
| 22 | |
| 23 | $this->parameters = $parameters; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @return never |
| 28 | */ |
| 29 | public function send() |
| 30 | { |
| 31 | if (empty($this->parameters)) { |
| 32 | throw new DomainException('Missing response body.'); |
| 33 | } |
| 34 | |
| 35 | $this->send_response($this->parameters); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return never |
| 40 | */ |
| 41 | private function send_response($data) |
| 42 | { |
| 43 | status_header($this->status_code); |
| 44 | |
| 45 | foreach ($this->headers as $header) { |
| 46 | header($header); |
| 47 | } |
| 48 | |
| 49 | echo json_encode($data); |
| 50 | exit; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return never |
| 55 | */ |
| 56 | public function error() |
| 57 | { |
| 58 | $this->send_response([ |
| 59 | 'success' => false, |
| 60 | 'data' => $this->parameters, |
| 61 | ]); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @return never |
| 66 | */ |
| 67 | public function success() |
| 68 | { |
| 69 | $this->send_response([ |
| 70 | 'success' => true, |
| 71 | 'data' => $this->parameters, |
| 72 | ]); |
| 73 | } |
| 74 | |
| 75 | public function set_parameter($key, $value): self |
| 76 | { |
| 77 | $this->parameters[$key] = $value; |
| 78 | |
| 79 | return $this; |
| 80 | } |
| 81 | |
| 82 | public function with_parameter($key, $value): self |
| 83 | { |
| 84 | $clone = clone $this; |
| 85 | $clone->parameters[$key] = $value; |
| 86 | |
| 87 | return $clone; |
| 88 | } |
| 89 | |
| 90 | public function set_parameters(array $values): self |
| 91 | { |
| 92 | foreach ($values as $key => $value) { |
| 93 | $this->set_parameter($key, $value); |
| 94 | } |
| 95 | |
| 96 | return $this; |
| 97 | } |
| 98 | |
| 99 | public function with_parameters(array $values): self |
| 100 | { |
| 101 | $clone = $this; |
| 102 | |
| 103 | foreach ($values as $key => $value) { |
| 104 | $clone = $clone->with_parameter($key, $value); |
| 105 | } |
| 106 | |
| 107 | return $clone; |
| 108 | } |
| 109 | |
| 110 | public function set_header(string $name, string $value): self |
| 111 | { |
| 112 | $this->headers[] = sprintf('%s: %s', $name, $value); |
| 113 | |
| 114 | return $this; |
| 115 | } |
| 116 | |
| 117 | public function with_header(string $name, string $value): self |
| 118 | { |
| 119 | $clone = clone $this; |
| 120 | $clone->headers[] = sprintf('%s: %s', $name, $value); |
| 121 | |
| 122 | return $clone; |
| 123 | } |
| 124 | |
| 125 | public function set_message(string $message): self |
| 126 | { |
| 127 | $this->set_parameter(self::MESSAGE, $message); |
| 128 | |
| 129 | return $this; |
| 130 | } |
| 131 | |
| 132 | public function with_message(string $message): self |
| 133 | { |
| 134 | return $this->with_parameter(self::MESSAGE, $message); |
| 135 | } |
| 136 | |
| 137 | public function set_status_code(int $code): self |
| 138 | { |
| 139 | $this->status_code = $code; |
| 140 | |
| 141 | return $this; |
| 142 | } |
| 143 | |
| 144 | public function with_status_code(int $code): self |
| 145 | { |
| 146 | $clone = clone $this; |
| 147 | $clone->status_code = $code; |
| 148 | |
| 149 | return $clone; |
| 150 | } |
| 151 | |
| 152 | } |
| 153 |