|
1 | 1 | <?php
|
| 2 | + |
2 | 3 | namespace LangleyFoxall\Helpers;
|
3 | 4 |
|
4 | 5 | class ApiResponse implements \ArrayAccess
|
5 | 6 | {
|
6 |
| - /** @var int $status */ |
7 |
| - protected $status; |
8 |
| - |
9 |
| - /** @var bool $success */ |
10 |
| - protected $success; |
11 |
| - |
12 |
| - /** @var array|null|string $error */ |
13 |
| - protected $error; |
14 |
| - |
15 |
| - /** @var array|null $data */ |
16 |
| - protected $data; |
17 |
| - |
18 |
| - /** @var array|null $meta */ |
19 |
| - protected $meta; |
20 |
| - |
21 |
| - /** |
22 |
| - * ApiResponse constructor. |
23 |
| - * |
24 |
| - * @param bool $success |
25 |
| - * @param array|null|string $error |
26 |
| - * @param array|null $data |
27 |
| - * @param array|null $meta |
28 |
| - * @param int $status |
29 |
| - */ |
30 |
| - public function __construct(bool $success = true, $error = null, $data = null, $meta = null, int $status = 200) |
31 |
| - { |
32 |
| - $this->status = $status; |
33 |
| - $this->success = $success; |
34 |
| - $this->error = $error; |
35 |
| - $this->data = $data; |
36 |
| - $this->meta = $meta; |
37 |
| - } |
38 |
| - |
39 |
| - /** |
40 |
| - * @param array|null $data |
41 |
| - * @param array|null $meta |
42 |
| - * @param int $status |
43 |
| - * @return ApiResponse |
44 |
| - */ |
45 |
| - public static function success($data = null, $meta = null, int $status = 200) |
46 |
| - { |
47 |
| - $response = new self; |
48 |
| - |
49 |
| - $response->status = $status; |
50 |
| - $response->data = $data; |
51 |
| - $response->meta = $meta; |
52 |
| - |
53 |
| - return $response; |
54 |
| - } |
55 |
| - |
56 |
| - /** |
57 |
| - * @param mixed $error |
58 |
| - * @param int $status |
59 |
| - * @return ApiResponse |
60 |
| - */ |
61 |
| - public static function error($error = null, int $status = 400) |
62 |
| - { |
63 |
| - $response = new self; |
64 |
| - |
65 |
| - $response->status = $status; |
66 |
| - $response->success = false; |
67 |
| - $response->error = $error; |
68 |
| - |
69 |
| - return $response; |
70 |
| - } |
71 |
| - |
72 |
| - /** |
73 |
| - * @param array $data |
74 |
| - * @return $this |
75 |
| - */ |
76 |
| - public function data(array $data = null) |
77 |
| - { |
78 |
| - $this->data = $data; |
79 |
| - |
80 |
| - return $this; |
81 |
| - } |
82 |
| - |
83 |
| - /** |
84 |
| - * @param array $meta |
85 |
| - * @return $this |
86 |
| - */ |
87 |
| - public function meta(array $meta = null) |
88 |
| - { |
89 |
| - $this->meta = $meta; |
90 |
| - |
91 |
| - return $this; |
92 |
| - } |
93 |
| - |
94 |
| - /** |
95 |
| - * @param int $status |
96 |
| - * @return $this |
97 |
| - */ |
98 |
| - public function status(int $status) |
99 |
| - { |
100 |
| - $this->status = $status; |
101 |
| - |
102 |
| - return $this; |
103 |
| - } |
104 |
| - |
105 |
| - /** |
106 |
| - * @return \Illuminate\Http\JsonResponse |
107 |
| - */ |
108 |
| - public function json() |
109 |
| - { |
110 |
| - return response()->json(get_object_vars($this)) |
111 |
| - ->setStatusCode($this->status); |
112 |
| - } |
113 |
| - |
114 |
| - /** |
115 |
| - * @param string $offset |
116 |
| - * @return bool |
117 |
| - */ |
118 |
| - public function offsetExists($offset) |
119 |
| - { |
120 |
| - return isset($this->data[ $offset ]); |
121 |
| - } |
122 |
| - |
123 |
| - /** |
124 |
| - * @param string $offset |
125 |
| - * @return mixed|null |
126 |
| - */ |
127 |
| - public function offsetGet($offset) |
128 |
| - { |
129 |
| - return isset($this->data[ $offset ]) |
130 |
| - ? $this->data[ $offset ] : null; |
131 |
| - } |
132 |
| - |
133 |
| - /** |
134 |
| - * @param string $offset |
135 |
| - * @param mixed $value |
136 |
| - */ |
137 |
| - public function offsetSet($offset, $value) |
138 |
| - { |
139 |
| - $this->data[ $offset ] = $value; |
140 |
| - } |
141 |
| - |
142 |
| - /** |
143 |
| - * @param string $offset |
144 |
| - */ |
145 |
| - public function offsetUnset($offset) |
146 |
| - { |
147 |
| - unset($this->data[ $offset ]); |
148 |
| - } |
149 |
| - |
| 7 | + /** @var int */ |
| 8 | + protected $status; |
| 9 | + |
| 10 | + /** @var bool */ |
| 11 | + protected $success; |
| 12 | + |
| 13 | + /** @var array|null|string */ |
| 14 | + protected $error; |
| 15 | + |
| 16 | + /** @var array|null */ |
| 17 | + protected $data; |
| 18 | + |
| 19 | + /** @var array|null */ |
| 20 | + protected $meta; |
| 21 | + |
| 22 | + /** |
| 23 | + * ApiResponse constructor. |
| 24 | + * |
| 25 | + * @param bool $success |
| 26 | + * @param array|null|string $error |
| 27 | + * @param array|null $data |
| 28 | + * @param array|null $meta |
| 29 | + * @param int $status |
| 30 | + */ |
| 31 | + public function __construct(bool $success = true, $error = null, $data = null, $meta = null, int $status = 200) |
| 32 | + { |
| 33 | + $this->status = $status; |
| 34 | + $this->success = $success; |
| 35 | + $this->error = $error; |
| 36 | + $this->data = $data; |
| 37 | + $this->meta = $meta; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param array|null $data |
| 42 | + * @param array|null $meta |
| 43 | + * @param int $status |
| 44 | + * |
| 45 | + * @return ApiResponse |
| 46 | + */ |
| 47 | + public static function success($data = null, $meta = null, int $status = 200) |
| 48 | + { |
| 49 | + $response = new self(); |
| 50 | + |
| 51 | + $response->status = $status; |
| 52 | + $response->data = $data; |
| 53 | + $response->meta = $meta; |
| 54 | + |
| 55 | + return $response; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param mixed $error |
| 60 | + * @param int $status |
| 61 | + * |
| 62 | + * @return ApiResponse |
| 63 | + */ |
| 64 | + public static function error($error = null, int $status = 400) |
| 65 | + { |
| 66 | + $response = new self(); |
| 67 | + |
| 68 | + $response->status = $status; |
| 69 | + $response->success = false; |
| 70 | + $response->error = $error; |
| 71 | + |
| 72 | + return $response; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param array $data |
| 77 | + * |
| 78 | + * @return $this |
| 79 | + */ |
| 80 | + public function data(array $data = null) |
| 81 | + { |
| 82 | + $this->data = $data; |
| 83 | + |
| 84 | + return $this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param array $meta |
| 89 | + * |
| 90 | + * @return $this |
| 91 | + */ |
| 92 | + public function meta(array $meta = null) |
| 93 | + { |
| 94 | + $this->meta = $meta; |
| 95 | + |
| 96 | + return $this; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param int $status |
| 101 | + * |
| 102 | + * @return $this |
| 103 | + */ |
| 104 | + public function status(int $status) |
| 105 | + { |
| 106 | + $this->status = $status; |
| 107 | + |
| 108 | + return $this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @return \Illuminate\Http\JsonResponse |
| 113 | + */ |
| 114 | + public function json() |
| 115 | + { |
| 116 | + return response()->json(get_object_vars($this)) |
| 117 | + ->setStatusCode($this->status); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @param string $offset |
| 122 | + * |
| 123 | + * @return bool |
| 124 | + */ |
| 125 | + public function offsetExists($offset) |
| 126 | + { |
| 127 | + return isset($this->data[$offset]); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param string $offset |
| 132 | + * |
| 133 | + * @return mixed|null |
| 134 | + */ |
| 135 | + public function offsetGet($offset) |
| 136 | + { |
| 137 | + return isset($this->data[$offset]) |
| 138 | + ? $this->data[$offset] : null; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @param string $offset |
| 143 | + * @param mixed $value |
| 144 | + */ |
| 145 | + public function offsetSet($offset, $value) |
| 146 | + { |
| 147 | + $this->data[$offset] = $value; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @param string $offset |
| 152 | + */ |
| 153 | + public function offsetUnset($offset) |
| 154 | + { |
| 155 | + unset($this->data[$offset]); |
| 156 | + } |
| 157 | + |
150 | 158 | /**
|
151 | 159 | * @param $lifespan
|
152 | 160 | * @param ResponseCache $cache
|
| 161 | + * |
153 | 162 | * @return $this
|
154 | 163 | */
|
155 |
| - public function cache($lifespan, ResponseCache $cache, $forceOverwrite = false){ |
156 |
| - if(!$cache->hasData() || $forceOverwrite){ |
| 164 | + public function cache($lifespan, ResponseCache $cache, $forceOverwrite = false) |
| 165 | + { |
| 166 | + if (!$cache->hasData() || $forceOverwrite) { |
157 | 167 | $cache->cacheData($this->data, $lifespan);
|
158 | 168 | }
|
| 169 | + |
159 | 170 | return $this;
|
160 | 171 | }
|
161 | 172 | }
|
0 commit comments