1 | <?php |
||
2 | /* |
||
3 | * This file is part of the Scrawler package. |
||
4 | * |
||
5 | * (c) Pranjal Pandey <[email protected]> |
||
6 | * |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | */ |
||
10 | |||
11 | declare(strict_types=1); |
||
12 | |||
13 | namespace Scrawler\Http; |
||
14 | |||
15 | /** |
||
16 | * Response class adds magic to the Symfony response. |
||
17 | */ |
||
18 | class Response extends \Symfony\Component\HttpFoundation\Response |
||
19 | { |
||
20 | /** |
||
21 | * Redirect to url. |
||
22 | */ |
||
23 | public function redirect(string $url): RedirectResponse |
||
24 | { |
||
25 | return new RedirectResponse($url); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Return json response. |
||
30 | * |
||
31 | * @param string|array<mixed> $data |
||
32 | * @param array<mixed> $headers |
||
33 | */ |
||
34 | public function json(string|array $data, array $headers = []): Response |
||
35 | { |
||
36 | if (is_array($data)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
37 | $data = \Safe\json_encode($data); |
||
38 | } |
||
39 | $this->setContent($data); |
||
40 | $this->headers->set('Content-Type', 'application/json'); |
||
41 | foreach ($headers as $key => $value) { |
||
42 | $this->headers->set($key, $value); |
||
43 | } |
||
44 | |||
45 | return $this; |
||
46 | } |
||
47 | } |
||
48 |