Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ResponseFormatter.php
Go to the documentation of this file.
1<?php
2
4
7
18
26 public function get($objects, $pages = null) {
27 $objects = $this->getDatas($objects);
28 return $this->format($objects);
29 }
30
38 public function getDatas($objects, &$classname = null) {
39 $objects = \array_map(function ($o) use (&$classname) {
40 return $this->cleanRestObject($o, $classname);
41 }, $objects);
42 return \array_values($objects);
43 }
44
45 public function getJSONDatas($datas) {
46 return $this->toJson($this->getDatas($datas));
47 }
48
56 public function cleanRestObject($o, &$classname = null) {
57 $o = $o->_rest;
58 foreach ($o as $k => $v) {
59 if (isset ($v->_rest)) {
60 $o [$k] = $this->cleanRestObject($v);
61 }
62 if (\is_array($v)) {
63 foreach ($v as $index => $values) {
64 if (isset ($values->_rest))
65 $v [$index] = $this->cleanRestObject($values);
66 }
67 $o [$k] = $v;
68 }
69 }
70 return $o;
71 }
72
79 public function getOne($object) {
80 return $this->format($this->cleanRestObject($object));
81 }
82
89 public function format($arrayResponse) {
90 /*if(isset($arrayResponse['data'])){//To check
91 return \json_encode ( $arrayResponse['data'] );
92 }*/
93 return \json_encode($arrayResponse);
94 }
95
102 public function getModel($controllerName) {
103 $array = \explode("\\", $controllerName);
104 $result = \ucfirst(end($array));
105 if (UString::endswith($result, "s")) {
106 $result = \substr($result, 0, -1);
107 }
108 return $result;
109 }
110
117 public function toJson($data) {
118 return \json_encode($data);
119 }
120
127 public function formatException($e) {
128 $error = new RestError (@$e->getCode(), @$e->getMessage(), @$e->getTraceAsString(), @$e->getFile(), 500);
129 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}
getModel($controllerName)
Returns the model name corresponding to $controlleName.
getOne($object)
Returns a formated JSON response for an object $object.
toJson($data)
Formats an array of datas in JSON.
getDatas($objects, &$classname=null)
Returns an array of datas from an array of objects.
formatException($e)
Returns a JSON representation of the exception $e.
format($arrayResponse)
Formats a response array.
cleanRestObject($o, &$classname=null)
Returns the array of attributes corresponding to an object $o.
String utilities.
Definition UString.php:15