|
3 | 3 | namespace Reactmore\SupportAdapter\Adapter\Formatter; |
4 | 4 |
|
5 | 5 | use JsonSerializable; |
| 6 | +use ArrayAccess; |
6 | 7 |
|
7 | 8 | /** |
8 | | - * API Response Wrapper Class |
| 9 | + * Class Response |
| 10 | + * |
| 11 | + * API Response Wrapper Class. |
| 12 | + * Provides a default array-like response with the ability to convert |
| 13 | + * to object or JSON format. Implements ArrayAccess for convenient |
| 14 | + * array-like access to response properties. |
| 15 | + * |
| 16 | + * @package Reactmore\SupportAdapter\Adapter\Formatter |
9 | 17 | */ |
10 | | -class Response implements JsonSerializable |
| 18 | +class Response implements JsonSerializable, ArrayAccess |
11 | 19 | { |
| 20 | + /** |
| 21 | + * @var array The underlying response data. |
| 22 | + */ |
12 | 23 | private array $response; |
13 | 24 |
|
14 | 25 | /** |
15 | | - * Constructor. |
| 26 | + * Response constructor. |
16 | 27 | * |
17 | | - * @param bool $success Status keberhasilan request. |
18 | | - * @param int $statusCode HTTP Status Code. |
19 | | - * @param string $message Pesan response. |
20 | | - * @param mixed $data Data response yang bisa dikustom. |
| 28 | + * @param bool $success Indicates whether the request was successful. |
| 29 | + * @param int $statusCode HTTP status code of the response. |
| 30 | + * @param string $message Human-readable message or description. |
| 31 | + * @param mixed $data The response payload/data. |
21 | 32 | */ |
22 | | - public function __construct( |
23 | | - bool $success, |
24 | | - int $statusCode, |
25 | | - string $message, |
26 | | - mixed $data |
27 | | - ) { |
| 33 | + public function __construct(bool $success, int $statusCode, string $message, mixed $data) |
| 34 | + { |
28 | 35 | $this->response = [ |
29 | | - 'success' => $success, |
| 36 | + 'success' => $success, |
30 | 37 | 'status_code' => $statusCode, |
31 | | - 'message' => $message, |
32 | | - 'data' => $data |
| 38 | + 'message' => $message, |
| 39 | + 'data' => $data |
33 | 40 | ]; |
34 | 41 | } |
35 | 42 |
|
| 43 | + // ---------------- Default array access ---------------- |
| 44 | + |
| 45 | + /** |
| 46 | + * Check if a response key exists (ArrayAccess implementation). |
| 47 | + * |
| 48 | + * @param mixed $offset |
| 49 | + * @return bool |
| 50 | + */ |
| 51 | + public function offsetExists($offset): bool |
| 52 | + { |
| 53 | + return isset($this->response[$offset]); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get a response value by key (ArrayAccess implementation). |
| 58 | + * |
| 59 | + * @param mixed $offset |
| 60 | + * @return mixed |
| 61 | + */ |
| 62 | + public function offsetGet($offset): mixed |
| 63 | + { |
| 64 | + return $this->response[$offset] ?? null; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Set a response value by key (ArrayAccess implementation). |
| 69 | + * |
| 70 | + * @param mixed $offset |
| 71 | + * @param mixed $value |
| 72 | + * @return void |
| 73 | + */ |
| 74 | + public function offsetSet($offset, $value): void |
| 75 | + { |
| 76 | + $this->response[$offset] = $value; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Unset a response value by key (ArrayAccess implementation). |
| 81 | + * |
| 82 | + * @param mixed $offset |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function offsetUnset($offset): void |
| 86 | + { |
| 87 | + unset($this->response[$offset]); |
| 88 | + } |
| 89 | + |
| 90 | + // ---------------- Flexible output ---------------- |
| 91 | + |
| 92 | + /** |
| 93 | + * Get the entire response as an array (default format). |
| 94 | + * |
| 95 | + * @return array |
| 96 | + */ |
36 | 97 | public function toArray(): array |
37 | 98 | { |
38 | 99 | return $this->response; |
39 | 100 | } |
40 | 101 |
|
41 | | - public function jsonSerialize(): mixed |
| 102 | + /** |
| 103 | + * Get the entire response as an object. |
| 104 | + * |
| 105 | + * @return object |
| 106 | + */ |
| 107 | + public function toObject(): object |
42 | 108 | { |
43 | | - return $this->response; |
| 109 | + return (object) $this->response; |
44 | 110 | } |
45 | 111 |
|
| 112 | + /** |
| 113 | + * Get the entire response as a JSON string. |
| 114 | + * |
| 115 | + * @return string |
| 116 | + */ |
46 | 117 | public function toJson(): string |
47 | 118 | { |
48 | 119 | return json_encode($this->response, JSON_PRETTY_PRINT); |
49 | 120 | } |
50 | 121 |
|
| 122 | + /** |
| 123 | + * Specify data for JSON serialization (JsonSerializable interface). |
| 124 | + * |
| 125 | + * @return mixed |
| 126 | + */ |
| 127 | + public function jsonSerialize(): mixed |
| 128 | + { |
| 129 | + return $this->response; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Magic getter for accessing response keys as properties. |
| 134 | + * |
| 135 | + * @param string $key |
| 136 | + * @return mixed|null |
| 137 | + */ |
51 | 138 | public function __get(string $key): mixed |
52 | 139 | { |
53 | 140 | return $this->response[$key] ?? null; |
54 | 141 | } |
55 | 142 |
|
| 143 | + /** |
| 144 | + * Cast the response to a string (returns JSON string). |
| 145 | + * |
| 146 | + * @return string |
| 147 | + */ |
56 | 148 | public function __toString(): string |
57 | 149 | { |
58 | 150 | return $this->toJson(); |
59 | 151 | } |
| 152 | + |
| 153 | + /** |
| 154 | + * Get only the 'data' portion of the response. |
| 155 | + * |
| 156 | + * @return mixed |
| 157 | + */ |
| 158 | + public function getData(): mixed |
| 159 | + { |
| 160 | + return $this->response['data']; |
| 161 | + } |
60 | 162 | } |
0 commit comments