1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace voku\slim; |
6
|
|
|
|
7
|
|
|
use Slim\Slim; |
8
|
|
|
use Slim\View; |
9
|
|
|
use voku\helper\UTF8; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* JsonApiView - view wrapper for json responses (with error code). |
13
|
|
|
*/ |
14
|
|
|
class JsonApiView extends View |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
private $encodingOptions = 0; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $contentType = 'application/json'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $dataWraper; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $metaWrapper; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $dataOnly = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Construct JsonApiView instance |
46
|
|
|
* |
47
|
|
|
* @param string $dataWrapper (optional) Wrapper for data in response |
48
|
|
|
* @param string $metaWrapper (optional) Wrapper for metadata in response |
49
|
|
|
*/ |
50
|
1 |
|
public function __construct($dataWrapper = null, $metaWrapper = null) |
51
|
|
|
{ |
52
|
1 |
|
parent::__construct(); |
53
|
|
|
|
54
|
1 |
|
$this->dataWraper = $dataWrapper; |
55
|
1 |
|
$this->metaWrapper = $metaWrapper; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set whether to return only the data. |
60
|
|
|
* |
61
|
|
|
* @param bool $dataOnly |
62
|
|
|
*/ |
63
|
|
|
public function setDataOnly($dataOnly = true) |
64
|
|
|
{ |
65
|
|
|
$this->dataOnly = (bool)$dataOnly; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
|
|
public function getEncodingOptions(): int |
72
|
|
|
{ |
73
|
|
|
return $this->encodingOptions; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Bitmask consisting of<br /><br /> |
78
|
|
|
* <b>JSON_HEX_QUOT</b>,<br /> |
79
|
|
|
* <b>JSON_HEX_TAG</b>,<br /> |
80
|
|
|
* <b>JSON_HEX_AMP</b>,<br /> |
81
|
|
|
* <b>JSON_HEX_APOS</b>,<br /> |
82
|
|
|
* <b>JSON_NUMERIC_CHECK</b>,<br /> |
83
|
|
|
* <b>JSON_PRETTY_PRINT</b>,<br /> |
84
|
|
|
* <b>JSON_UNESCAPED_SLASHES</b>,<br /> |
85
|
|
|
* <b>JSON_FORCE_OBJECT</b>,<br /> |
86
|
|
|
* <b>JSON_UNESCAPED_UNICODE</b>.<br /> |
87
|
|
|
* |
88
|
|
|
* <p>The behaviour of these constants is described on the JSON constants page.</p> |
89
|
|
|
* |
90
|
|
|
* @param int $encodingOptions |
91
|
|
|
*/ |
92
|
|
|
public function setEncodingOptions($encodingOptions) |
93
|
|
|
{ |
94
|
|
|
$this->encodingOptions = $encodingOptions; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getContentType(): string |
101
|
|
|
{ |
102
|
|
|
return $this->contentType; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Content-Type sent through the HTTP header. |
107
|
|
|
* |
108
|
|
|
* <p>Default is set to "application/json", append ";charset=UTF-8" to force the charset</p> |
109
|
|
|
* |
110
|
|
|
* @param string $contentType |
111
|
|
|
*/ |
112
|
|
|
public function setContentType($contentType) |
113
|
|
|
{ |
114
|
|
|
$this->contentType = $contentType; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param int $status |
119
|
|
|
* @param mixed $data |
120
|
|
|
* @param string $slimNameInstance |
121
|
|
|
*/ |
122
|
|
|
public function render($status = 200, $data = null, $slimNameInstance = 'default') |
123
|
|
|
{ |
124
|
|
|
$app = Slim::getInstance($slimNameInstance); |
125
|
|
|
|
126
|
|
|
if ($this->dataWraper) { |
127
|
|
|
$response[$this->dataWraper] = $this->all(); |
|
|
|
|
128
|
|
|
} else { |
129
|
|
|
$response = $this->all(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (!$this->dataOnly) { |
133
|
|
|
//append error bool |
134
|
|
|
if ($status < 400) { |
135
|
|
View Code Duplication |
if ($this->metaWrapper) { |
|
|
|
|
136
|
|
|
$response[$this->metaWrapper]['error'] = false; |
137
|
|
|
} else { |
138
|
|
|
$response['error'] = false; |
139
|
|
|
} |
140
|
|
View Code Duplication |
} else { |
|
|
|
|
141
|
|
|
if ($this->metaWrapper) { |
142
|
|
|
$response[$this->metaWrapper]['error'] = true; |
143
|
|
|
} else { |
144
|
|
|
$response['error'] = true; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// append status code |
149
|
|
|
if ($this->metaWrapper) { |
150
|
|
|
$response[$this->metaWrapper]['status'] = $status; |
151
|
|
|
} else { |
152
|
|
|
$response['status'] = $status; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// add flash messages |
156
|
|
|
if (isset($this->data->flash) && \is_object($this->data->flash)) { |
157
|
|
|
$flash = $this->data->flash->getMessages(); |
158
|
|
|
|
159
|
|
|
if ($this->dataWraper) { |
160
|
|
|
unset($response[$this->dataWraper]['flash']); |
161
|
|
|
} else { |
162
|
|
|
unset($response['flash']); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (\count($flash)) { |
166
|
|
|
if ($this->metaWrapper) { |
167
|
|
|
$response[$this->metaWrapper]['flash'] = $flash; |
168
|
|
|
} else { |
169
|
|
|
$response['flash'] = $flash; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} else { |
174
|
|
|
unset($response['flash'], $response['status'], $response['error']); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$app->response()->status($status); |
178
|
|
|
$app->response()->header('Content-Type', $this->contentType); |
179
|
|
|
|
180
|
|
|
$jsonp_callback = $app->request->get('callback', null); |
181
|
|
|
|
182
|
|
|
if ($jsonp_callback !== null) { |
183
|
|
|
$app->response()->body($jsonp_callback . '(' . UTF8::json_encode($response, $this->encodingOptions) . ')'); |
184
|
|
|
} else { |
185
|
|
|
$app->response()->body(UTF8::json_encode($response, $this->encodingOptions)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$app->stop(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.