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