Total Complexity | 125 |
Total Lines | 799 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like Restful often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Restful, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Restful extends Controller |
||
31 | { |
||
32 | /** |
||
33 | * Push headers flag |
||
34 | * |
||
35 | * Used for push default headers by controller. |
||
36 | * Set to FALSE if you want to set default headers on the web-server configuration. |
||
37 | * |
||
38 | * APACHE via .htaccess |
||
39 | * IIS via .webconfig |
||
40 | * Nginx via config |
||
41 | * |
||
42 | * @type string |
||
43 | */ |
||
44 | protected $pushDefaultHeaders = false; |
||
45 | |||
46 | /** |
||
47 | * Access-Control-Allow-Origin |
||
48 | * |
||
49 | * Used for indicates whether a resource can be shared based by |
||
50 | * returning the value of the Origin request header, "*", or "null" in the response. |
||
51 | * |
||
52 | * @type string |
||
53 | */ |
||
54 | protected $accessControlAllowOrigin = '*'; |
||
55 | |||
56 | /** |
||
57 | * Access-Control-Allow-Credentials |
||
58 | * |
||
59 | * Used for indicates whether the response to request can be exposed when the omit credentials flag is unset. |
||
60 | * When part of the response to a preflight request it indicates that the actual request can include user |
||
61 | * credentials. |
||
62 | * |
||
63 | * @type bool |
||
64 | */ |
||
65 | protected $accessControlAllowCredentials = true; |
||
66 | |||
67 | /** |
||
68 | * Access-Control-Method |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $accessControlMethod = 'GET'; |
||
73 | |||
74 | /** |
||
75 | * Access-Control-Params |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $accessControlParams = []; |
||
80 | |||
81 | /** |
||
82 | * Access-Control-Allow-Methods |
||
83 | * |
||
84 | * Used for indicates, as part of the response to a preflight request, |
||
85 | * which methods can be used during the actual request. |
||
86 | * |
||
87 | * @type array |
||
88 | */ |
||
89 | protected $accessControlAllowMethods = [ |
||
90 | 'GET', // common request |
||
91 | 'POST', // used for create, update request |
||
92 | 'PUT', // used for upload files request |
||
93 | 'DELETE', // used for delete request |
||
94 | 'OPTIONS', // used for preflight request |
||
95 | ]; |
||
96 | |||
97 | /** |
||
98 | * Access-Control-Allow-Headers |
||
99 | * |
||
100 | * Used for indicates, as part of the response to a preflight request, |
||
101 | * which header field names can be used during the actual request. |
||
102 | * |
||
103 | * @type array |
||
104 | */ |
||
105 | protected $accessControlAllowHeaders = [ |
||
106 | 'Origin', |
||
107 | 'Access-Control-Request-Method', |
||
108 | 'Access-Control-Request-Headers', |
||
109 | 'X-Api-Authenticate', // API-Authenticate: api_key="xxx", api_secret="xxx", api_signature="xxx" |
||
110 | 'X-Api-Token', |
||
111 | 'X-Web-Token', // X-Web-Token: xxx (json-web-token) |
||
112 | 'X-Csrf-Token', |
||
113 | 'X-Xss-Token', |
||
114 | 'X-Request-ID', |
||
115 | 'X-Requested-With', |
||
116 | 'X-Requested-Result', |
||
117 | ]; |
||
118 | |||
119 | /** |
||
120 | * Access-Control-Allow-Headers |
||
121 | * |
||
122 | * Used for indicates, as part of the response to a preflight request, |
||
123 | * which header field names can be used during the actual request. |
||
124 | * |
||
125 | * @type array |
||
126 | */ |
||
127 | protected $accessControlAllowContentTypes = [ |
||
128 | 'text/html', |
||
129 | 'application/json', |
||
130 | 'application/xml', |
||
131 | ]; |
||
132 | |||
133 | /** |
||
134 | * Access-Control-Max-Age |
||
135 | * |
||
136 | * Used for indicates how long the results of a preflight request can be cached in a preflight result cache |
||
137 | * |
||
138 | * @type int |
||
139 | */ |
||
140 | protected $accessControlMaxAge = 86400; |
||
141 | |||
142 | /** |
||
143 | * Restful::$ajaxOnly |
||
144 | * |
||
145 | * @var bool |
||
146 | */ |
||
147 | protected $ajaxOnly = false; |
||
148 | |||
149 | /** |
||
150 | * Restful::$model |
||
151 | * |
||
152 | * @var \O2System\Framework\Models\Sql\Model|\O2System\Framework\Models\NoSql\Model|\O2System\Framework\Models\Files\Model |
||
153 | */ |
||
154 | public $model; |
||
155 | |||
156 | /** |
||
157 | * Restful::$params |
||
158 | * |
||
159 | * @var array |
||
160 | */ |
||
161 | public $params = []; |
||
162 | |||
163 | /** |
||
164 | * Restful::$paramsWithRules |
||
165 | * |
||
166 | * @var array |
||
167 | */ |
||
168 | public $paramsWithRules = []; |
||
169 | |||
170 | /** |
||
171 | * Restful::$fillableColumns |
||
172 | * |
||
173 | * @var array |
||
174 | */ |
||
175 | public $fillableColumns = []; |
||
176 | |||
177 | /** |
||
178 | * Restful::$fillableColumnsWithRules |
||
179 | * |
||
180 | * @var array |
||
181 | */ |
||
182 | public $fillableColumnsWithRules = []; |
||
183 | |||
184 | /** |
||
185 | * Restful::$dataTableColumns |
||
186 | * |
||
187 | * @var array |
||
188 | */ |
||
189 | public $dataTableColumns = []; |
||
190 | |||
191 | // ------------------------------------------------------------------------ |
||
192 | |||
193 | /** |
||
194 | * Restful::__construct |
||
195 | */ |
||
196 | public function __construct() |
||
197 | { |
||
198 | if (services()->has('presenter')) { |
||
199 | presenter()->setTheme(false); |
||
|
|||
200 | } |
||
201 | |||
202 | if (is_ajax()) { |
||
203 | output()->setContentType('application/json'); |
||
204 | } elseif ($this->ajaxOnly === false) { |
||
205 | output()->setContentType('application/json'); |
||
206 | } else { |
||
207 | output()->setContentType('text/html'); |
||
208 | } |
||
209 | |||
210 | if ($contentType = input()->server('HTTP_X_REQUESTED_CONTENT_TYPE')) { |
||
211 | if (in_array($contentType, $this->accessControlAllowContentTypes)) { |
||
212 | output()->setContentType($contentType); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | if ($this->pushDefaultHeaders) { |
||
217 | |||
218 | $origin = input()->server('HTTP_ORIGIN'); |
||
219 | |||
220 | /** |
||
221 | * Prepare for preflight modern browser request |
||
222 | * |
||
223 | * Since some server cannot use 'Access-Control-Allow-Origin: *' |
||
224 | * the Access-Control-Allow-Origin will be defined based on requested origin |
||
225 | */ |
||
226 | if ($this->accessControlAllowOrigin === '*') { |
||
227 | output()->addHeader(ResponseFieldInterface::RESPONSE_ACCESS_CONTROL_ALLOW_ORIGIN, $origin); |
||
228 | } |
||
229 | |||
230 | // Set response access control allowed credentials |
||
231 | if ($this->accessControlAllowCredentials === false) { |
||
232 | output()->addHeader(ResponseFieldInterface::RESPONSE_ACCESS_CONTROL_ALLOW_CREDENTIALS, 'false'); |
||
233 | } |
||
234 | |||
235 | // Set response access control allowed methods header |
||
236 | if (count($this->accessControlAllowMethods)) { |
||
237 | output()->addHeader( |
||
238 | ResponseFieldInterface::RESPONSE_ACCESS_CONTROL_ALLOW_METHODS, |
||
239 | implode(', ', $this->accessControlAllowMethods) |
||
240 | ); |
||
241 | } |
||
242 | |||
243 | // Set response access control allowed headers header |
||
244 | if (count($this->accessControlAllowHeaders)) { |
||
245 | output()->addHeader( |
||
246 | ResponseFieldInterface::RESPONSE_ACCESS_CONTROL_ALLOW_HEADERS, |
||
247 | implode(', ', $this->accessControlAllowHeaders) |
||
248 | ); |
||
249 | } |
||
250 | |||
251 | // Set response access control allowed content types header |
||
252 | if (count($this->accessControlAllowContentTypes)) { |
||
253 | output()->addHeader( |
||
254 | ResponseFieldInterface::RESPONSE_ACCESS_CONTROL_ALLOW_CONTENT_TYPES, |
||
255 | implode(', ', $this->accessControlAllowContentTypes) |
||
256 | ); |
||
257 | } |
||
258 | |||
259 | // Set response access control max age header |
||
260 | if ($this->accessControlMaxAge > 0) { |
||
261 | output()->addHeader(ResponseFieldInterface::RESPONSE_ACCESS_CONTROL_MAX_AGE, |
||
262 | $this->accessControlMaxAge); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | if (input()->server('REQUEST_METHOD') === 'OPTIONS') { |
||
267 | exit(EXIT_SUCCESS); |
||
268 | } elseif ( ! in_array(input()->server('REQUEST_METHOD'), $this->accessControlAllowMethods)) { |
||
269 | $this->sendError(405); |
||
270 | } elseif (count($this->accessControlParams)) { |
||
271 | if ($this->accessControlMethod === 'GET') { |
||
272 | if (empty($_GET)) { |
||
273 | $this->sendError(400); |
||
274 | } |
||
275 | } elseif ($this->accessControlMethod === 'POST') { |
||
276 | if (empty($_POST)) { |
||
277 | $this->sendError(400); |
||
278 | } |
||
279 | } elseif (in_array($this->accessControlMethod, ['GETPOST', 'POSTGET'])) { |
||
280 | if (empty($_REQUEST)) { |
||
281 | $this->sendError(400); |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | |||
286 | if (empty($this->model)) { |
||
287 | $controllerClassName = get_called_class(); |
||
288 | $modelClassName = str_replace('Controllers', 'Models', $controllerClassName); |
||
289 | |||
290 | if (class_exists($modelClassName)) { |
||
291 | $this->model = new $modelClassName(); |
||
292 | } |
||
293 | } elseif (class_exists($this->model)) { |
||
294 | $this->model = new $this->model(); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | // ------------------------------------------------------------------------ |
||
299 | |||
300 | /** |
||
301 | * Restful::index |
||
302 | */ |
||
303 | public function index() |
||
374 | } |
||
375 | } |
||
376 | } |
||
377 | } |
||
378 | |||
379 | // ------------------------------------------------------------------------ |
||
380 | |||
381 | /** |
||
382 | * Restful::datatable |
||
383 | */ |
||
384 | public function datatable() |
||
385 | { |
||
386 | if (empty($this->model)) { |
||
387 | output()->sendError(204); |
||
388 | } else { |
||
389 | if ( ! $this->model instanceof Model) { |
||
390 | $this->sendError(503, 'Model is not exists!'); |
||
391 | } |
||
392 | |||
393 | if ( ! $this->model instanceof Model) { |
||
394 | $this->sendError(503, 'Model is not exists!'); |
||
395 | } |
||
396 | |||
397 | $hasAction = false; |
||
398 | if($request = input()->request()) { |
||
399 | // Start as limit |
||
400 | $this->model->qb->limit($request['start']); |
||
401 | |||
402 | // Length as offset |
||
403 | $this->model->qb->offset($request['length']); |
||
404 | |||
405 | // Set ordering |
||
406 | if( ! empty($request['order'])) { |
||
407 | foreach($request['order'] as $dt => $order) { |
||
408 | $field = $request['columns'][$order['column']]['data']; |
||
409 | $this->model->qb->orderBy($field, strtoupper($order['dir'])); |
||
410 | } |
||
411 | } |
||
412 | |||
413 | $this->model->visibleColumns = []; |
||
414 | |||
415 | foreach($request['columns'] as $dt => $column) { |
||
416 | if($column['data'] === 'action') { |
||
417 | $this->model->appendColumns[] = 'action'; |
||
418 | |||
419 | continue; |
||
420 | } |
||
421 | |||
422 | if($column['searchable']) { |
||
423 | if($dt == 0) { |
||
424 | if( ! empty($column['search']['value']) ) { |
||
425 | $this->model->qb->like($column['data'], $column['search']['value']); |
||
426 | |||
427 | if( ! empty($request['search']['value'])) { |
||
428 | $this->model->qb->orLike($column['data'], $request['search']['value']); |
||
429 | } |
||
430 | } elseif( ! empty($request['search']['value'])) { |
||
431 | $this->model->qb->like($column['data'], $request['search']['value']); |
||
432 | } |
||
433 | } else { |
||
434 | if( ! empty($column['search']['value']) ) { |
||
435 | $this->model->qb->orLike($column[ 'data' ], $column[ 'search' ][ 'value' ]); |
||
436 | } |
||
437 | |||
438 | if( ! empty($request['search']['value'])) { |
||
439 | $this->model->qb->orLike($column['data'], $request['search']['value']); |
||
440 | } |
||
441 | } |
||
442 | } |
||
443 | |||
444 | $this->model->visibleColumns[] = $column['data']; |
||
445 | } |
||
446 | } |
||
447 | |||
448 | $this->model->rebuildRowCallback(function ($row) { |
||
449 | $row->DT_RowId = 'datatable-row-' . $row->id; |
||
450 | }); |
||
451 | |||
452 | if (false !== ($result = $this->model->all())) { |
||
453 | output()->sendPayload([ |
||
454 | 'draw' => input()->request('draw'), |
||
455 | 'recordsTotal' => $result->info->num_total, |
||
456 | 'recordsFiltered' => $result->info->num_founds, |
||
457 | 'data' => $result->toArray(), |
||
458 | ]); |
||
459 | } else { |
||
460 | $this->sendError(204); |
||
461 | } |
||
462 | } |
||
463 | } |
||
464 | |||
465 | // ------------------------------------------------------------------------ |
||
466 | |||
467 | /** |
||
468 | * Controller::create |
||
469 | * |
||
470 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadDependencyCallException |
||
471 | * @throws \O2System\Spl\Exceptions\Logic\OutOfRangeException |
||
472 | */ |
||
473 | public function create() |
||
474 | { |
||
475 | if ($post = input()->post()) { |
||
476 | if (count($this->fillableColumnsWithRules)) { |
||
477 | $rules = new Rules($post); |
||
478 | $rules->sets($this->fillableColumnsWithRules); |
||
479 | if ( ! $rules->validate()) { |
||
480 | $this->sendError(400, $rules->displayErrors(true)); |
||
481 | } |
||
482 | } |
||
483 | |||
484 | if ( ! $this->model instanceof Model) { |
||
485 | $this->sendError(503, 'Model is not ready'); |
||
486 | } |
||
487 | |||
488 | $data = []; |
||
489 | |||
490 | if (count($this->fillableColumnsWithRules)) { |
||
491 | foreach ($this->fillableColumnsWithRules as $column) { |
||
492 | if ($post->offsetExists($column[ 'field' ])) { |
||
493 | $data[ $column[ 'field' ] ] = $post->offsetGet($column[ 'field' ]); |
||
494 | } |
||
495 | } |
||
496 | } elseif (count($this->fillableColumns)) { |
||
497 | foreach ($this->fillableColumns as $column) { |
||
498 | if ($post->offsetExists($column[ 'field' ])) { |
||
499 | $data[ $column[ 'field' ] ] = $post->offsetGet($column[ 'field' ]); |
||
500 | } |
||
501 | } |
||
502 | } else { |
||
503 | $data = $post->getArrayCopy(); |
||
504 | } |
||
505 | |||
506 | if (count($data)) { |
||
507 | $data[ 'record_create_timestamp' ] = $data[ 'record_update_timestamp' ] = timestamp(); |
||
508 | $data[ 'record_create_user' ] = $data[ 'record_update_user' ] = globals()->account->id; |
||
509 | |||
510 | if ($this->model->insert($data)) { |
||
511 | $data[ 'id' ] = $this->model->db->getLastInsertId(); |
||
512 | $this->sendPayload([ |
||
513 | 'code' => 201, |
||
514 | 'Successful insert request', |
||
515 | 'data' => $data, |
||
516 | ]); |
||
517 | } else { |
||
518 | $this->sendError(501, 'Failed update request'); |
||
519 | } |
||
520 | } else { |
||
521 | $this->sendError(400, 'Post parameters cannot be empty!'); |
||
522 | } |
||
523 | } else { |
||
524 | $this->sendError(400); |
||
525 | } |
||
526 | } |
||
527 | |||
528 | // ------------------------------------------------------------------------ |
||
529 | |||
530 | /** |
||
531 | * Restful::update |
||
532 | * |
||
533 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadDependencyCallException |
||
534 | * @throws \O2System\Spl\Exceptions\Logic\OutOfRangeException |
||
535 | */ |
||
536 | public function update() |
||
537 | { |
||
538 | if ($post = input()->post()) { |
||
539 | $conditions = []; |
||
540 | |||
541 | if (count($this->fillableColumnsWithRules)) { |
||
542 | $rules = new Rules($post); |
||
543 | $rules->sets($this->fillableColumnsWithRules); |
||
544 | |||
545 | if (count($this->model->primaryKeys)) { |
||
546 | foreach ($this->model->primaryKeys as $primaryKey) { |
||
547 | $rules->add($primaryKey, language('LABEL_' . strtoupper($primaryKey)), 'required', |
||
548 | 'this field cannot be empty!'); |
||
549 | } |
||
550 | } else { |
||
551 | $primaryKey = empty($this->model->primaryKey) ? 'id' : $this->model->primaryKey; |
||
552 | $conditions = [$primaryKey => $post->offsetGet($primaryKey)]; |
||
553 | $rules->add($primaryKey, language('LABEL_' . strtoupper($primaryKey)), 'required', |
||
554 | 'this field cannot be empty!'); |
||
555 | } |
||
556 | |||
557 | if ( ! $rules->validate()) { |
||
558 | $this->sendError(400, implode(', ', $rules->displayErrors(true))); |
||
559 | } |
||
560 | } |
||
561 | |||
562 | if ( ! $this->model instanceof Model) { |
||
563 | $this->sendError(503, 'Model is not ready'); |
||
564 | } |
||
565 | |||
566 | $data = []; |
||
567 | |||
568 | if (count($this->fillableColumnsWithRules)) { |
||
569 | foreach ($this->fillableColumnsWithRules as $column) { |
||
570 | if ($post->offsetExists($column[ 'field' ])) { |
||
571 | $data[ $column[ 'field' ] ] = $post->offsetGet($column[ 'field' ]); |
||
572 | } |
||
573 | } |
||
574 | } elseif (count($this->fillableColumns)) { |
||
575 | foreach ($this->fillableColumns as $column) { |
||
576 | if ($post->offsetExists($column[ 'field' ])) { |
||
577 | $data[ $column[ 'field' ] ] = $post->offsetGet($column[ 'field' ]); |
||
578 | } |
||
579 | } |
||
580 | } else { |
||
581 | $data = $post->getArrayCopy(); |
||
582 | } |
||
583 | |||
584 | if (count($data)) { |
||
585 | $data[ 'record_update_timestamp' ] = timestamp(); |
||
586 | $data[ 'record_update_user' ] = globals()->account->id; |
||
587 | |||
588 | |||
589 | if ($this->model->update($data, $conditions)) { |
||
590 | $this->sendError(201, 'Successful update request'); |
||
591 | } else { |
||
592 | $this->sendError(501, 'Failed update request'); |
||
593 | } |
||
594 | } else { |
||
595 | $this->sendError(400, 'Post parameters cannot be empty!'); |
||
596 | } |
||
597 | } else { |
||
598 | $this->sendError(400); |
||
599 | } |
||
600 | } |
||
601 | |||
602 | // ------------------------------------------------------------------------ |
||
603 | |||
604 | /** |
||
605 | * Restful::delete |
||
606 | * |
||
607 | * @throws \O2System\Spl\Exceptions\Logic\OutOfRangeException |
||
608 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
609 | * @throws \Psr\Cache\InvalidArgumentException |
||
610 | */ |
||
611 | public function delete() |
||
632 | } |
||
633 | } |
||
634 | |||
635 | // ------------------------------------------------------------------------ |
||
636 | |||
637 | /** |
||
638 | * Restful::publish |
||
639 | * |
||
640 | * @throws OutOfRangeException |
||
641 | */ |
||
642 | public function publish() |
||
643 | { |
||
644 | if ($post = input()->post()) { |
||
645 | $rules = new Rules($post); |
||
646 | $rules->add('id', 'ID', 'required', 'ID field cannot be empty!'); |
||
647 | |||
648 | if ( ! $rules->validate()) { |
||
649 | $this->sendError(400, implode(', ', $rules->getErrors())); |
||
650 | } |
||
651 | |||
652 | if ( ! $this->model instanceof Model) { |
||
653 | $this->sendError(503, 'Model is not ready'); |
||
654 | } |
||
655 | |||
656 | if ($this->model->publish($post->id)) { |
||
657 | $this->sendError(201, 'Successful publish request'); |
||
658 | } else { |
||
659 | $this->sendError(501, 'Failed publish request'); |
||
660 | } |
||
661 | } else { |
||
662 | $this->sendError(400); |
||
663 | } |
||
664 | } |
||
665 | |||
666 | // ------------------------------------------------------------------------ |
||
667 | |||
668 | /** |
||
669 | * Restful::unpublish |
||
670 | * |
||
671 | * @throws OutOfRangeException |
||
672 | */ |
||
673 | public function unpublish() |
||
674 | { |
||
675 | if ($post = input()->post()) { |
||
676 | $rules = new Rules($post); |
||
677 | $rules->add('id', 'ID', 'required', 'ID field cannot be empty!'); |
||
678 | |||
679 | if ( ! $rules->validate()) { |
||
680 | $this->sendError(400, implode(', ', $rules->getErrors())); |
||
681 | } |
||
682 | |||
683 | if ( ! $this->model instanceof Model) { |
||
684 | $this->sendError(503, 'Model is not ready'); |
||
685 | } |
||
686 | |||
687 | if ($this->model->unpublish($post->id)) { |
||
688 | $this->sendError(201, 'Successful unpublish request'); |
||
689 | } else { |
||
690 | $this->sendError(501, 'Failed unpublish request'); |
||
691 | } |
||
692 | } else { |
||
693 | $this->sendError(400); |
||
694 | } |
||
695 | } |
||
696 | |||
697 | // ------------------------------------------------------------------------ |
||
698 | |||
699 | /** |
||
700 | * Restful::archive |
||
701 | * |
||
702 | * @throws OutOfRangeException |
||
703 | */ |
||
704 | public function archive() |
||
705 | { |
||
706 | if ($post = input()->post()) { |
||
707 | $rules = new Rules($post); |
||
708 | $rules->add('id', 'ID', 'required', 'ID field cannot be empty!'); |
||
709 | |||
710 | if ( ! $rules->validate()) { |
||
711 | $this->sendError(400, implode(', ', $rules->getErrors())); |
||
712 | } |
||
713 | |||
714 | if ( ! $this->model instanceof Model) { |
||
715 | $this->sendError(503, 'Model is not ready'); |
||
716 | } |
||
717 | |||
718 | if ($this->model->unpublish($post->id)) { |
||
719 | $this->sendError(201, 'Successful archived request'); |
||
720 | } else { |
||
721 | $this->sendError(501, 'Failed archived request'); |
||
722 | } |
||
723 | } else { |
||
724 | $this->sendError(400); |
||
725 | } |
||
726 | } |
||
727 | |||
728 | // ------------------------------------------------------------------------ |
||
729 | |||
730 | /** |
||
731 | * Restful::sendError |
||
732 | * |
||
733 | * @param int $code |
||
734 | * @param string|null $message |
||
735 | */ |
||
736 | public function sendError($code, $message = null) |
||
737 | { |
||
738 | if ($this->ajaxOnly === false) { |
||
739 | output()->setContentType('application/json'); |
||
740 | } |
||
741 | |||
742 | if (is_array($code)) { |
||
743 | if (is_numeric(key($code))) { |
||
744 | $message = reset($code); |
||
745 | $code = key($code); |
||
746 | } elseif (isset($code[ 'code' ])) { |
||
747 | $code = $code[ 'code' ]; |
||
748 | $message = $code[ 'message' ]; |
||
749 | } |
||
750 | } |
||
751 | |||
752 | output()->sendError($code, $message); |
||
753 | } |
||
754 | |||
755 | // ------------------------------------------------------------------------ |
||
756 | |||
757 | /** |
||
758 | * Restful::sendPayload |
||
759 | * |
||
760 | * @param mixed $data The payload data to-be send. |
||
761 | * @param bool $longPooling Long pooling flag mode. |
||
762 | * |
||
763 | * @throws \Exception |
||
764 | */ |
||
765 | public function sendPayload($data, $longPooling = false) |
||
829 | } |
||
830 | } |
||
831 | } |