-
Notifications
You must be signed in to change notification settings - Fork 453
/
EditResponse.php
307 lines (276 loc) · 6.89 KB
/
EditResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
namespace Concrete\Core\Application;
use Concrete\Core\Error\ErrorList\ErrorList;
use Concrete\Core\Support\Facade\Application as ApplicationFacade;
use Core;
use Exception;
use JsonSerializable;
use stdClass;
/**
* The result of an edit operation.
*/
class EditResponse implements JsonSerializable
{
/**
* The error(s) of the response.
*
* @var \Concrete\Core\Error\ErrorList\ErrorList|\Exception|string|null
*
* @deprecated since concrete5 8.5.0a3 (what's deprecated is the "public part") - use setError/getError/hasError
*/
public $error;
/**
* The date/time of the response in ISO-9075 format (YYYY-MM-DD hh:mm:ss).
*
* @var string
*
* @deprecated since concrete5 8.5.0a3 (what's deprecated is the "public part") - use getTime
*/
public $time;
/**
* The title of the response.
*
* @var string|null
*
* @deprecated since concrete5 8.5.0a3 (what's deprecated is the "public part") - use setTitle/getTitle
*/
public $title;
/**
* The message of the response.
*
* @var string|null
*
* @deprecated since concrete5 8.5.0a3 (what's deprecated is the "public part") - use setMessage/getMessage
*/
public $message;
/**
* The redirect URL of the response.
*
* @var string|\League\URL\URLInterface|null
*
* @deprecated since concrete5 8.5.0a3 (what's deprecated is the "public part") - use setMessage/getMessage
*/
public $redirectURL;
/**
* Additional response data.
*
* @var array
*/
protected $additionalData = [];
/**
* @param \Concrete\Core\Error\ErrorList\ErrorList|\Exception|string|false $e
*/
public function __construct($e = false)
{
$app = ApplicationFacade::getFacadeApplication();
if ($e instanceof ErrorList) {
$this->error = $e;
} else {
$this->error = $app->make('error');
if ($e instanceof Exception) {
$this->error->add($e);
} else {
$e = (string) $e;
if ($e !== '') {
$this->error->add($e);
}
}
}
$this->time = $app->make('date')->toDB();
}
/**
* Get the error(s) of the response.
*
* @return \Concrete\Core\Error\ErrorList\ErrorList|\Exception|string|null
*
* @since concrete5 8.5.0a3
*/
public function getError()
{
return $this->error;
}
/**
* Set the error(s) of the response.
*
* @param \Concrete\Core\Error\ErrorList\ErrorList|\Exception|string|null $error
*
* @return $this
*/
public function setError($error)
{
$this->error = $error;
return $this;
}
/**
* Does this response contain an error?
*
* @return bool
*/
public function hasError()
{
$error = $this->getError();
if ($error instanceof ErrorList) {
return $error->has();
}
if ($error instanceof Exception) {
return true;
}
return (string) $error !== '';
}
/**
* Get the date/time of the response in ISO-9075 format (YYYY-MM-DD hh:mm:ss).
*
* @return string
*
* @since concrete5 8.5.0a3
*/
public function getTime()
{
return $this->time;
}
/**
* Set the title of the response.
*
* @param string|null $title
*
* @return $this
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get the title of the response.
*
* @return string|null
*/
public function getTitle()
{
return $this->title;
}
/**
* Set the message of the response.
*
* @param string|null $message
*
* @return $this
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}
/**
* Get the message of the response.
*
* @return string|null
*/
public function getMessage()
{
return $this->message;
}
/**
* Set the redirect URL of the response.
*
* @param string|\League\URL\URLInterface|null $url
*
* @return $this
*/
public function setRedirectURL($url)
{
$this->redirectURL = $url;
return $this;
}
/**
* Get the redirect URL of the response.
*
* @return string|\League\URL\URLInterface|null
*/
public function getRedirectURL()
{
return $this->redirectURL;
}
/**
* Set additional response data.
*
* @param string $key
* @param mixed $value
*/
public function setAdditionalDataAttribute($key, $value)
{
$this->additionalData[$key] = $value;
}
/**
* Get the JSON representation of the data of this instance.
*
* @return string
*/
public function getJSON()
{
return json_encode($this->getJSONObject());
}
/**
* Get an stdClass instance containing the serialized data of this instance.
*
* @return \stdClass
*/
public function getJSONObject()
{
return $this->getBaseJSONObject();
}
/**
* Get an stdClass instance containing the serialized data of this instance.
*
* @return \stdClass
*/
public function getBaseJSONObject()
{
$o = new stdClass();
$error = $this->getError();
if ($error instanceof Exception) {
$o->error = true;
$o->errors = [$error->getMessage()];
} elseif ($error instanceof ErrorList && $error->has()) {
$o->error = true;
$o->errors = [];
foreach ($error->getList() as $e) {
$o->errors[] = (string) $e;
}
} else {
$error = (string) $error;
if ($error !== '') {
$o->error = true;
$o->errors = [$error];
}
}
$o->time = $this->getTime();
$o->message = $this->getMessage();
$o->title = $this->getTitle();
$o->redirectURL = (string) $this->getRedirectURL();
foreach ($this->additionalData as $key => $value) {
$o->{$key} = $value;
}
return $o;
}
/**
* {@inheritdoc}
*
* @see \JsonSerializable::jsonSerialize()
*/
public function jsonSerialize()
{
return $this->getJSONObject();
}
/**
* @deprecated since concrete5 8.5.0a3 This method sends the response directly to the cliend and ends the execution: you should build a Response instance.
*/
public function outputJSON()
{
if ($this->hasError()) {
Core::make('helper/ajax')->sendError($this->error);
} else {
Core::make('helper/ajax')->sendResult($this->getJSONObject());
}
}
}