1 | <?php |
||
8 | class JsonApiResponse extends JsonResponse |
||
9 | { |
||
10 | /** |
||
11 | * @var integer |
||
12 | */ |
||
13 | private $code; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $charset; |
||
19 | |||
20 | /** |
||
21 | * Create JsonApiResponse |
||
22 | * This class only wrap JsonResponse from Nette and add possibility |
||
23 | * to setup response code and automaticaly set content type |
||
24 | * |
||
25 | * @param integer $code |
||
26 | * @param mixed $data |
||
27 | * @param string $contentType |
||
28 | * @param string $charset |
||
29 | */ |
||
30 | public function __construct($code, $data, $contentType = 'application/json', $charset = 'utf-8') |
||
31 | { |
||
32 | parent::__construct($data, $contentType); |
||
33 | $this->charset = $charset; |
||
34 | $this->code = $code; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Return api response http code |
||
39 | * |
||
40 | * @return integer |
||
41 | */ |
||
42 | public function getCode() |
||
43 | { |
||
44 | return $this->code; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Return encoding charset for http response |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function getCharset() |
||
53 | { |
||
54 | return $this->charset; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Sends response to output. |
||
59 | * @return void |
||
60 | */ |
||
61 | public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) |
||
62 | { |
||
63 | $httpResponse->setContentType($this->getContentType(), $this->charset); |
||
64 | $httpResponse->setExpiration(false); |
||
65 | $result = Nette\Utils\Json::encode($this->getPayload()) |
||
66 | $httpResponse->setHeader('Content-Length', strlen($result)); |
||
|
|||
67 | echo $result; |
||
68 | } |
||
70 |