1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tequilarapido\ApiResponse; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response as HttpResponse; |
6
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Inspired from dingo/api. |
10
|
|
|
* |
11
|
|
|
* Class ResponseBuilder |
12
|
|
|
*/ |
13
|
|
|
class ResponseBuilder |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Response content. |
17
|
|
|
* |
18
|
|
|
* @var mixed |
19
|
|
|
*/ |
20
|
|
|
protected $response; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The HTTP response headers. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $headers = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The HTTP response cookies. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $cookies = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The HTTP response status code. |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $statusCode = 200; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a new response builder instance. |
45
|
|
|
* |
46
|
|
|
* @param mixed $response |
47
|
|
|
*/ |
48
|
|
|
public function __construct($response) |
49
|
|
|
{ |
50
|
|
|
$this->response = $response; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add a cookie to the response. |
55
|
|
|
* |
56
|
|
|
* @param \Symfony\Component\HttpFoundation\Cookie $cookie |
57
|
|
|
* |
58
|
|
|
* @return ResponseBuilder |
59
|
|
|
*/ |
60
|
|
|
public function withCookie(Cookie $cookie) |
61
|
|
|
{ |
62
|
|
|
$this->cookies[] = $cookie; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Add a cookie to the response. |
69
|
|
|
* |
70
|
|
|
* @param \Symfony\Component\HttpFoundation\Cookie $cookie |
71
|
|
|
* |
72
|
|
|
* @return ResponseBuilder |
73
|
|
|
*/ |
74
|
|
|
public function cookie(Cookie $cookie) |
75
|
|
|
{ |
76
|
|
|
return $this->withCookie($cookie); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Add a header to the response. |
81
|
|
|
* |
82
|
|
|
* @param string $name |
83
|
|
|
* @param string $value |
84
|
|
|
* |
85
|
|
|
* @return ResponseBuilder |
86
|
|
|
*/ |
87
|
|
|
public function withHeader($name, $value) |
88
|
|
|
{ |
89
|
|
|
$this->headers[$name] = $value; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add a header to the response. |
96
|
|
|
* |
97
|
|
|
* @param string $name |
98
|
|
|
* @param string $value |
99
|
|
|
* |
100
|
|
|
* @return ResponseBuilder |
101
|
|
|
*/ |
102
|
|
|
public function header($name, $value) |
103
|
|
|
{ |
104
|
|
|
return $this->withHeader($name, $value); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the responses status code. |
109
|
|
|
* |
110
|
|
|
* @param int $statusCode |
111
|
|
|
* |
112
|
|
|
* @return ResponseBuilder |
113
|
|
|
*/ |
114
|
|
|
public function setStatusCode($statusCode) |
115
|
|
|
{ |
116
|
|
|
$this->statusCode = $statusCode; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set the response status code. |
123
|
|
|
* |
124
|
|
|
* @param int $statusCode |
125
|
|
|
* |
126
|
|
|
* @return ResponseBuilder |
127
|
|
|
*/ |
128
|
|
|
public function statusCode($statusCode) |
129
|
|
|
{ |
130
|
|
|
return $this->setStatusCode($statusCode); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Build the response. |
135
|
|
|
* |
136
|
|
|
* @return \Illuminate\Http\Response |
137
|
|
|
*/ |
138
|
|
|
public function build() |
139
|
|
|
{ |
140
|
|
|
$response = new HttpResponse($this->response, $this->statusCode, $this->headers); |
141
|
|
|
|
142
|
|
|
foreach ($this->cookies as $cookie) { |
143
|
|
|
if ($cookie instanceof Cookie) { |
144
|
|
|
$response->withCookie($cookie); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $response; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns cookies. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function getCookies() |
157
|
|
|
{ |
158
|
|
|
return $this->cookies; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Return headers. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function getHeaders() |
167
|
|
|
{ |
168
|
|
|
return $this->headers; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return status code. |
173
|
|
|
* |
174
|
|
|
* @return int |
175
|
|
|
*/ |
176
|
|
|
public function getStatusCode() |
177
|
|
|
{ |
178
|
|
|
return $this->statusCode; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|