1 | <?php |
||
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 | public static function create($data = null, ?int $statusCode = null, array $headers = []): self |
||
35 | { |
||
36 | return new static($data, $statusCode, $headers); |
||
37 | } |
||
38 | |||
39 | public static function createRedirect(string $url, int $statusCode = Response::HTTP_FOUND, array $headers = []): self |
||
40 | { |
||
41 | $view = static::create(null, $statusCode, $headers); |
||
42 | $view->setLocation($url); |
||
43 | |||
44 | return $view; |
||
45 | } |
||
46 | |||
47 | public static function createRouteRedirect( |
||
48 | string $route, |
||
49 | array $parameters = [], |
||
50 | int $statusCode = Response::HTTP_FOUND, |
||
51 | array $headers = [] |
||
52 | ): self { |
||
53 | $view = static::create(null, $statusCode, $headers); |
||
54 | $view->setRoute($route); |
||
55 | $view->setRouteParameters($parameters); |
||
56 | |||
57 | return $view; |
||
58 | } |
||
59 | |||
60 | public function __construct($data = null, ?int $statusCode = null, array $headers = []) |
||
61 | { |
||
62 | $this->setData($data); |
||
63 | $this->setStatusCode($statusCode); |
||
64 | |||
65 | if (!empty($headers)) { |
||
66 | $this->getResponse()->headers->replace($headers); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | public function setData($data): self |
||
71 | { |
||
72 | $this->data = $data; |
||
73 | |||
74 | 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 | public function setHeaders(array $headers): self |
||
85 | { |
||
86 | $this->getResponse()->headers->replace($headers); |
||
87 | |||
88 | return $this; |
||
89 | } |
||
90 | |||
91 | public function setStatusCode(?int $code): self |
||
92 | { |
||
93 | 13 | if (null !== $code) { |
|
94 | $this->statusCode = $code; |
||
95 | 13 | } |
|
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | public function setContext(Context $context): self |
||
101 | { |
||
102 | $this->context = $context; |
||
103 | |||
104 | 1 | return $this; |
|
105 | } |
||
106 | 1 | ||
107 | 1 | public function setFormat(string $format): self |
|
108 | { |
||
109 | 1 | $this->format = $format; |
|
110 | |||
111 | return $this; |
||
112 | } |
||
113 | |||
114 | public function setLocation(string $location): self |
||
115 | { |
||
116 | $this->location = $location; |
||
117 | $this->route = null; |
||
118 | 3 | ||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Sets the route (implicitly removes the location). |
||
124 | 3 | */ |
|
125 | 3 | public function setRoute(string $route): self |
|
126 | 3 | { |
|
127 | $this->route = $route; |
||
128 | 3 | $this->location = null; |
|
129 | |||
130 | return $this; |
||
131 | } |
||
132 | |||
133 | public function setRouteParameters(array $parameters): self |
||
134 | 95 | { |
|
135 | $this->routeParameters = $parameters; |
||
136 | 95 | ||
137 | 95 | return $this; |
|
138 | 95 | } |
|
139 | |||
140 | 95 | public function setResponse(Response $response): self |
|
141 | { |
||
142 | $this->response = $response; |
||
143 | 95 | ||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | public function getData() |
||
151 | |||
152 | 95 | public function getStatusCode(): ?int |
|
153 | { |
||
154 | return $this->statusCode; |
||
155 | } |
||
156 | |||
157 | public function getHeaders(): array |
||
158 | { |
||
159 | return $this->getResponse()->headers->all(); |
||
160 | } |
||
161 | |||
162 | 15 | public function getFormat(): ?string |
|
163 | { |
||
164 | 15 | return $this->format; |
|
165 | 6 | } |
|
166 | |||
167 | public function getLocation(): ?string |
||
171 | |||
172 | public function getRoute(): ?string |
||
173 | { |
||
174 | return $this->route; |
||
175 | } |
||
176 | |||
177 | public function getRouteParameters(): ?array |
||
178 | { |
||
179 | return $this->routeParameters; |
||
180 | } |
||
181 | |||
182 | public function getResponse(): Response |
||
183 | { |
||
184 | if (null === $this->response) { |
||
185 | $this->response = new Response(); |
||
186 | |||
187 | if (null !== ($code = $this->getStatusCode())) { |
||
188 | $this->response->setStatusCode($code); |
||
189 | 2 | } |
|
190 | } |
||
194 | |||
195 | public function getContext(): Context |
||
203 | } |
||
204 |