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