|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSRestBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\View; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\RestBundle\Context\Context; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Default View implementation. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
|
21
|
|
|
* @author Lukas K. Smith <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
final class View |
|
24
|
|
|
{ |
|
25
|
|
|
private $data; |
|
26
|
|
|
private $statusCode; |
|
27
|
|
|
private $format; |
|
28
|
|
|
private $location; |
|
29
|
|
|
private $route; |
|
30
|
|
|
private $routeParameters; |
|
31
|
|
|
private $context; |
|
32
|
|
|
private $response; |
|
33
|
|
|
|
|
34
|
9 |
|
public static function create($data = null, ?int $statusCode = null, array $headers = []): self |
|
35
|
|
|
{ |
|
36
|
9 |
|
return new static($data, $statusCode, $headers); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
public static function createRedirect(string $url, int $statusCode = Response::HTTP_FOUND, array $headers = []): self |
|
40
|
|
|
{ |
|
41
|
1 |
|
$view = static::create(null, $statusCode, $headers); |
|
42
|
1 |
|
$view->setLocation($url); |
|
43
|
|
|
|
|
44
|
1 |
|
return $view; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public static function createRouteRedirect( |
|
48
|
|
|
string $route, |
|
49
|
|
|
array $parameters = [], |
|
50
|
|
|
int $statusCode = Response::HTTP_FOUND, |
|
51
|
|
|
array $headers = [] |
|
52
|
|
|
): self { |
|
53
|
2 |
|
$view = static::create(null, $statusCode, $headers); |
|
54
|
2 |
|
$view->setRoute($route); |
|
55
|
2 |
|
$view->setRouteParameters($parameters); |
|
56
|
|
|
|
|
57
|
2 |
|
return $view; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
57 |
|
public function __construct($data = null, ?int $statusCode = null, array $headers = []) |
|
61
|
|
|
{ |
|
62
|
57 |
|
$this->setData($data); |
|
63
|
57 |
|
$this->setStatusCode($statusCode); |
|
64
|
|
|
|
|
65
|
57 |
|
if (!empty($headers)) { |
|
66
|
|
|
$this->getResponse()->headers->replace($headers); |
|
67
|
|
|
} |
|
68
|
57 |
|
} |
|
69
|
|
|
|
|
70
|
57 |
|
public function setData($data): self |
|
71
|
|
|
{ |
|
72
|
57 |
|
$this->data = $data; |
|
73
|
|
|
|
|
74
|
57 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function setHeader(string $name, string $value): self |
|
78
|
|
|
{ |
|
79
|
|
|
$this->getResponse()->headers->set($name, $value); |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
public function setHeaders(array $headers): self |
|
85
|
|
|
{ |
|
86
|
2 |
|
$this->getResponse()->headers->replace($headers); |
|
87
|
|
|
|
|
88
|
2 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
57 |
|
public function setStatusCode(?int $code): self |
|
92
|
|
|
{ |
|
93
|
57 |
|
if (null !== $code) { |
|
94
|
13 |
|
$this->statusCode = $code; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
57 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function setContext(Context $context): self |
|
101
|
|
|
{ |
|
102
|
|
|
$this->context = $context; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
24 |
|
public function setFormat(string $format): self |
|
108
|
|
|
{ |
|
109
|
24 |
|
$this->format = $format; |
|
110
|
|
|
|
|
111
|
24 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
4 |
|
public function setLocation(string $location): self |
|
115
|
|
|
{ |
|
116
|
4 |
|
$this->location = $location; |
|
117
|
4 |
|
$this->route = null; |
|
118
|
|
|
|
|
119
|
4 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Sets the route (implicitly removes the location). |
|
124
|
|
|
*/ |
|
125
|
3 |
|
public function setRoute(string $route): self |
|
126
|
|
|
{ |
|
127
|
3 |
|
$this->route = $route; |
|
128
|
3 |
|
$this->location = null; |
|
129
|
|
|
|
|
130
|
3 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
3 |
|
public function setRouteParameters(array $parameters): self |
|
134
|
|
|
{ |
|
135
|
3 |
|
$this->routeParameters = $parameters; |
|
136
|
|
|
|
|
137
|
3 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function setResponse(Response $response): self |
|
141
|
|
|
{ |
|
142
|
|
|
$this->response = $response; |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
42 |
|
public function getData() |
|
148
|
|
|
{ |
|
149
|
42 |
|
return $this->data; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
51 |
|
public function getStatusCode(): ?int |
|
153
|
|
|
{ |
|
154
|
51 |
|
return $this->statusCode; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function getHeaders(): array |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->getResponse()->headers->all(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
29 |
|
public function getFormat(): ?string |
|
163
|
|
|
{ |
|
164
|
29 |
|
return $this->format; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
32 |
|
public function getLocation(): ?string |
|
168
|
|
|
{ |
|
169
|
32 |
|
return $this->location; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
34 |
|
public function getRoute(): ?string |
|
173
|
|
|
{ |
|
174
|
34 |
|
return $this->route; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
2 |
|
public function getRouteParameters(): ?array |
|
178
|
|
|
{ |
|
179
|
2 |
|
return $this->routeParameters; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
39 |
|
public function getResponse(): Response |
|
183
|
|
|
{ |
|
184
|
39 |
|
if (null === $this->response) { |
|
185
|
39 |
|
$this->response = new Response(); |
|
186
|
|
|
|
|
187
|
39 |
|
if (null !== ($code = $this->getStatusCode())) { |
|
188
|
9 |
|
$this->response->setStatusCode($code); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
39 |
|
return $this->response; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
33 |
|
public function getContext(): Context |
|
196
|
|
|
{ |
|
197
|
33 |
|
if (null === $this->context) { |
|
198
|
33 |
|
$this->context = new Context(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
33 |
|
return $this->context; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|