Passed
Push — master ( d36a99...d31551 )
by Jean-Christophe
06:01
created

ResponseFormatter::cleanRestObject()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 9
nop 2
crap 6
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
	 *
20
	 * @param array $datas
21
	 * @param array|null $pages
22
	 * @return string
23
	 */
24 1
	public function get($datas, $pages = null) {
25 1
		$datas = $this->getDatas ( $datas );
26 1
		return $this->format ( [ "datas" => $datas,"count" => \sizeof ( $datas ) ] );
27
	}
28
29 4
	public function getDatas($datas, &$classname = null) {
30
		$datas = \array_map ( function ($o) use (&$classname) {
31 4
			return $this->cleanRestObject ( $o, $classname );
32 4
		}, $datas );
33 4
		return \array_values ( $datas );
34
	}
35
36
	public function getJSONDatas($datas) {
37
		return $this->toJson ( $this->getDatas ( $datas ) );
38
	}
39
40 2
	public function cleanRestObject($o, &$classname = null) {
41 2
		$o = $o->_rest;
42 2
		foreach ( $o as $k => $v ) {
43 2
			if (isset ( $v->_rest )) {
44 1
				$o [$k] = $v->_rest;
45
			}
46 2
			if (\is_array ( $v )) {
47 2
				foreach ( $v as $index => $values ) {
48 2
					if (isset ( $values->_rest ))
49 2
						$v [$index] = $this->cleanRestObject ( $values );
50
				}
51 2
				$o [$k] = $v;
52
			}
53
		}
54 2
		return $o;
55
	}
56
57 4
	public function getOne($datas) {
58 4
		return $this->format ( [ "data" => $this->cleanRestObject ( $datas ) ] );
59
	}
60
61
	/**
62
	 * Formats a response array
63
	 *
64
	 * @param array $arrayResponse
65
	 * @return string
66
	 */
67 8
	public function format($arrayResponse) {
68 8
		return \json_encode ( $arrayResponse );
69
	}
70
71 5
	public function getModel($controllerName) {
72 5
		$array = \explode ( "\\", $controllerName );
73 5
		$result = \ucfirst ( end ( $array ) );
74 5
		if (UString::endswith ( $result, "s" )) {
75
			$result = \substr ( $result, 0, - 1 );
76
		}
77 5
		return $result;
78
	}
79
80 2
	public function toJson($data) {
81 2
		return \json_encode ( $data );
82
	}
83
84
	public function formatException($e) {
85
		$error = new RestError ( @$e->getCode (), \utf8_encode ( $e->getMessage () ), @$e->getTraceAsString (), @$e->getFile (), 500 );
86
		return $this->format ( $error->asArray () );
87
	}
88
89
	public static function toXML($data, &$xml_data) {
90
		foreach ( $data as $key => $value ) {
91
			if (is_numeric ( $key )) {
92
				$key = 'item' . $key; // dealing with <0/>..<n/> issues
93
			}
94
			if (is_array ( $value )) {
95
				$subnode = $xml_data->addChild ( $key );
96
				self::toXML ( $value, $subnode );
97
			} else {
98
				$xml_data->addChild ( "$key", htmlspecialchars ( "$value" ) );
99
			}
100
		}
101
	}
102
}
103