/
response.php
121 lines (105 loc) · 3.16 KB
/
response.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
<?php
namespace phpListRestapi;
defined('PHPLISTINIT') || die;
/**
* Common response as success and error
* Andreas Ek, 2012-12-26.
*/
class Response
{
private $result;
public function __construct()
{
$this->result = array();
}
public function setError($code, $message)
{
$this->result['status'] = 'error';
$this->result['type'] = 'Error';
$this->result['data'] = array(
'code' => $code,
'message' => $message,
);
}
public function setData($type, $data)
{
$this->result['status'] = 'success';
$this->result['type'] = $type;
$this->result['data'] = $data;
}
public function output()
{
header('Content-Type: application/json; charset=utf-8;');
echo $this->json_encode2($this->result);
die(0);
}
/**
* Convert an object into an associative array.
*
* This function converts an object into an associative array by iterating
* over its public properties. Because this function uses the foreach
* construct, Iterators are respected. It also works on arrays of objects.
*
* @return array
*/
public function object_to_array($var)
{
$result = array();
$references = array();
// loop over elements/properties
foreach ($var as $key => $value) {
// recursively convert objects
if (is_object($value) || is_array($value)) {
// but prevent cycles
if (!in_array($value, $references)) {
$result[$key] = $this->object_to_array($value);
$references[] = $value;
}
} else {
// simple values are untouched
$result[$key] = $value;
}
}
return $result;
}
/**
* Convert a value to JSON.
*
* This function returns a JSON representation of $param. It uses json_encode
* to accomplish this, but converts objects and arrays containing objects to
* associative arrays first. This way, objects that do not expose (all) their
* properties directly but only through an Iterator interface are also encoded
* correctly.
*/
public function json_encode2($param)
{
if (is_object($param) || is_array($param)) {
$param = $this->object_to_array($param);
}
return json_encode($param);
}
public static function outputError($e)
{
$response = new self();
$response->setError($e->getCode(), $e->getMessage());
$response->output();
}
public static function outputErrorMessage($message)
{
$response = new self();
$response->setError(0, $message);
$response->output();
}
public static function outputDeleted($type, $id)
{
$response = new self();
$response->setData($type, 'Item with '.$id.' is successfully deleted!');
$response->output();
}
public static function outputMessage($message)
{
$response = new self();
$response->setData('SystemMessage', $message);
$response->output();
}
}