1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Response; |
4
|
|
|
|
5
|
|
|
use DateTimeInterface; |
6
|
|
|
use Nette\Http\IRequest; |
7
|
|
|
use Nette\Http\IResponse; |
8
|
|
|
use Nette\SmartObject; |
9
|
|
|
use Nette\Utils\Json; |
10
|
|
|
|
11
|
|
|
class JsonApiResponse implements ResponseInterface |
12
|
|
|
{ |
13
|
|
|
use SmartObject; |
14
|
|
|
|
15
|
|
|
/** @var integer */ |
16
|
|
|
private $code; |
17
|
|
|
|
18
|
|
|
/** @var mixed */ |
19
|
|
|
private $payload; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
private $contentType; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $charset; |
26
|
|
|
|
27
|
|
|
/** @var bool|DateTimeInterface|int|string */ |
28
|
|
|
private $expiration; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This class is only copy of JsonResponse from Nette with added possibility |
32
|
|
|
* to setup response code, content type, charset and expiration |
33
|
|
|
* |
34
|
|
|
* @param integer $code |
35
|
|
|
* @param mixed $data |
|
|
|
|
36
|
|
|
* @param string $contentType |
37
|
|
|
* @param string $charset |
38
|
|
|
* @param bool|DateTimeInterface|int|string $expiration |
39
|
|
|
*/ |
40
|
21 |
|
public function __construct($code, $payload, $contentType = 'application/json', $charset = 'utf-8', $expiration = false) |
41
|
|
|
{ |
42
|
21 |
|
$this->code = $code; |
43
|
21 |
|
$this->payload = $payload; |
44
|
21 |
|
$this->contentType = $contentType ?: 'application/json'; |
45
|
21 |
|
$this->charset = $charset; |
46
|
21 |
|
$this->expiration = $expiration; |
47
|
21 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
21 |
|
public function getCode(): int |
53
|
|
|
{ |
54
|
21 |
|
return $this->code; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
21 |
|
public function getPayload() |
61
|
|
|
{ |
62
|
21 |
|
return $this->payload; |
63
|
|
|
} |
64
|
|
|
|
65
|
9 |
|
public function getContentType(): string |
66
|
|
|
{ |
67
|
9 |
|
return $this->contentType; |
68
|
|
|
} |
69
|
|
|
|
70
|
9 |
|
public function getCharset(): string |
71
|
|
|
{ |
72
|
9 |
|
return $this->charset; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool|DateTimeInterface|int|string |
77
|
|
|
*/ |
78
|
3 |
|
public function getExpiration() |
79
|
|
|
{ |
80
|
3 |
|
return $this->expiration; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
3 |
|
public function send(IRequest $httpRequest, IResponse $httpResponse): void |
87
|
|
|
{ |
88
|
3 |
|
$httpResponse->setContentType($this->getContentType(), $this->getCharset()); |
89
|
3 |
|
$httpResponse->setExpiration($this->getExpiration()); |
90
|
3 |
|
$result = Json::encode($this->getPayload()); |
91
|
3 |
|
$httpResponse->setHeader('Content-Length', strlen($result)); |
92
|
3 |
|
echo $result; |
93
|
3 |
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.