1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\rest; |
4
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UString; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Base class for Formating Rest responses. |
9
|
|
|
* Ubiquity\controllers\rest$ResponseFormatter |
10
|
|
|
* This class is part of Ubiquity |
11
|
|
|
* |
12
|
|
|
* @author jcheron <[email protected]> |
13
|
|
|
* @version 1.0.0 |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class ResponseFormatter { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Returns a formated JSON response for an array of objects $objects |
20
|
|
|
* |
21
|
|
|
* @param array $objects |
22
|
|
|
* @param array|null $pages |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
5 |
|
public function get($objects, $pages = null) { |
26
|
5 |
|
$objects = $this->getDatas ( $objects ); |
27
|
5 |
|
return $this->format ( [ "datas" => $objects,"count" => \sizeof ( $objects ) ] ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns an array of datas from an array of objects |
32
|
|
|
* |
33
|
|
|
* @param array $objects |
34
|
|
|
* @param string $classname |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
8 |
|
public function getDatas($objects, &$classname = null) { |
38
|
|
|
$objects = \array_map ( function ($o) use (&$classname) { |
39
|
8 |
|
return $this->cleanRestObject ( $o, $classname ); |
40
|
8 |
|
}, $objects ); |
41
|
8 |
|
return \array_values ( $objects ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getJSONDatas($datas) { |
45
|
|
|
return $this->toJson ( $this->getDatas ( $datas ) ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the array of attributes corresponding to an object $o |
50
|
|
|
* |
51
|
|
|
* @param object $o |
52
|
|
|
* @param string $classname |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
8 |
|
public function cleanRestObject($o, &$classname = null) { |
56
|
8 |
|
$o = $o->_rest; |
57
|
8 |
|
foreach ( $o as $k => $v ) { |
58
|
8 |
|
if (isset ( $v->_rest )) { |
59
|
1 |
|
$o [$k] = $v->_rest; |
60
|
|
|
} |
61
|
8 |
|
if (\is_array ( $v )) { |
62
|
8 |
|
foreach ( $v as $index => $values ) { |
63
|
8 |
|
if (isset ( $values->_rest )) |
64
|
8 |
|
$v [$index] = $this->cleanRestObject ( $values ); |
65
|
|
|
} |
66
|
8 |
|
$o [$k] = $v; |
67
|
|
|
} |
68
|
|
|
} |
69
|
8 |
|
return $o; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns a formated JSON response for an object $object |
74
|
|
|
* |
75
|
|
|
* @param object $object |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
4 |
|
public function getOne($object) { |
79
|
4 |
|
return $this->format ( [ "data" => $this->cleanRestObject ( $object ) ] ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Formats a response array |
84
|
|
|
* |
85
|
|
|
* @param array $arrayResponse |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
12 |
|
public function format($arrayResponse) { |
89
|
12 |
|
return \json_encode ( $arrayResponse ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the model name corresponding to $controlleName |
94
|
|
|
* |
95
|
|
|
* @param string $controllerName |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
5 |
|
public function getModel($controllerName) { |
99
|
5 |
|
$array = \explode ( "\\", $controllerName ); |
100
|
5 |
|
$result = \ucfirst ( end ( $array ) ); |
101
|
5 |
|
if (UString::endswith ( $result, "s" )) { |
102
|
|
|
$result = \substr ( $result, 0, - 1 ); |
103
|
|
|
} |
104
|
5 |
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Formats an array of datas in JSON |
109
|
|
|
* |
110
|
|
|
* @param array $data |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
2 |
|
public function toJson($data) { |
114
|
2 |
|
return \json_encode ( $data ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns a JSON representation of the exception $e |
119
|
|
|
* |
120
|
|
|
* @param \Exception $e |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function formatException($e) { |
124
|
|
|
$error = new RestError ( @$e->getCode (), \utf8_encode ( $e->getMessage () ), @$e->getTraceAsString (), @$e->getFile (), 500 ); |
125
|
|
|
return $this->format ( $error->asArray () ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public static function toXML($data, &$xml_data) { |
129
|
|
|
foreach ( $data as $key => $value ) { |
130
|
|
|
if (is_numeric ( $key )) { |
131
|
|
|
$key = 'item' . $key; // dealing with <0/>..<n/> issues |
132
|
|
|
} |
133
|
|
|
if (is_array ( $value )) { |
134
|
|
|
$subnode = $xml_data->addChild ( $key ); |
135
|
|
|
self::toXML ( $value, $subnode ); |
136
|
|
|
} else { |
137
|
|
|
$xml_data->addChild ( "$key", htmlspecialchars ( "$value" ) ); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|