1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Api Response |
4
|
|
|
* |
5
|
|
|
* @package erdiko/core |
6
|
|
|
* @copyright 2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com |
7
|
|
|
* @author John Arroyo <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
namespace erdiko\core; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class ApiResponse extends Response |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* _statusCode |
17
|
|
|
* |
18
|
|
|
* Unless explicitly set, default to a 200 status |
19
|
|
|
* assuming everything went ok. |
20
|
|
|
*/ |
21
|
|
|
protected $_statusCode = 200; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* _errors |
25
|
|
|
* |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
protected $_errors = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Theme |
32
|
|
|
*/ |
33
|
|
|
protected $_theme; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Content |
37
|
|
|
*/ |
38
|
|
|
protected $_content = null; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* setStatusCode |
44
|
|
|
* |
45
|
|
|
* Set, and send, the HTTP status code. |
46
|
|
|
*/ |
47
|
|
|
public function setStatusCode($code = null) |
48
|
|
|
{ |
49
|
|
|
if (!empty($code)) { |
50
|
|
|
$this->_statusCode = $code; |
51
|
|
|
http_response_code($this->_statusCode); // this sends the http status code |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setErrors($errors = null) |
56
|
|
|
{ |
57
|
|
|
if (!empty($errors)) { |
58
|
|
|
if (!is_array($errors)) { |
59
|
|
|
$errors = array($errors); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->_errors = $errors; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Ajax render function |
68
|
|
|
* @todo add support for xml |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function render() |
73
|
|
|
{ |
74
|
|
|
$responseData = array( |
75
|
|
|
"status" => $this->_statusCode, |
76
|
|
|
"body" => $this->_content, |
77
|
|
|
"errors" => $this->_errors |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
return json_encode($responseData); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Render and send data to browser then end request |
85
|
|
|
* |
86
|
|
|
* @todo allow switching between json and xml |
87
|
|
|
* @note This should only be called at the very end of processing the response |
88
|
|
|
*/ |
89
|
|
|
public function send() |
90
|
|
|
{ |
91
|
|
|
// set the mime type to JSON |
92
|
|
|
header('Content-Type: application/json'); |
93
|
|
|
|
94
|
|
|
echo $this->render(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..