Passed
Push — master ( 9c1274...95a447 )
by Jean-Christophe
06:00
created

ResponseFormatter::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Ubiquity\controllers\rest;
4
5
use Ubiquity\utils\base\UString;
6
7
class ResponseFormatter {
8
9
	/**
10
	 *
11
	 * @param array $datas
12
	 * @param array|null $pages
13
	 * @return string
14
	 */
15 1
	public function get($datas,$pages=null) {
16 1
		$datas = $this->getDatas ( $datas,$pages);
17 1
		return $this->format ( [ "datas" => $datas,"count" => \sizeof ( $datas ) ] );
18
	}
19
20 2
	public function getDatas($datas,&$classname) {
21
		$datas = \array_map ( function ($o) use(&$classname){
22 2
			return $this->cleanRestObject ( $o ,$classname);
23 2
		}, $datas );
24 2
		return \array_values ( $datas );
25
	}
26
27
	public function getJSONDatas($datas) {
28
		return $this->toJson ( $this->getDatas ( $datas ) );
0 ignored issues
show
Bug introduced by
The call to Ubiquity\controllers\res...seFormatter::getDatas() has too few arguments starting with classname. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
		return $this->toJson ( $this->/** @scrutinizer ignore-call */ getDatas ( $datas ) );

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
29
	}
30
31 2
	public function cleanRestObject($o,&$classname=null) {
32 2
		$o = $o->_rest;
33 2
		foreach ( $o as $k => $v ) {
34 2
			if (isset ( $v->_rest )) {
35 1
				$o [$k] = $v->_rest;
36
			}
37 2
			if (\is_array ( $v )) {
38 2
				foreach ( $v as $index => $values ) {
39 2
					if (isset ( $values->_rest ))
40 2
						$v [$index] = $this->cleanRestObject ( $values);
41
				}
42 2
				$o [$k] = $v;
43
			}
44
		}
45 2
		return $o;
46
	}
47
48 2
	public function getOne($datas) {
49 2
		return $this->format ( [ "data" => $this->cleanRestObject ( $datas ) ] );
50
	}
51
52
	/**
53
	 * Formats a response array
54
	 *
55
	 * @param array $arrayResponse
56
	 * @return string
57
	 */
58 3
	public function format($arrayResponse) {
59 3
		return \json_encode ( $arrayResponse );
60
	}
61
62
	public function getModel($controllerName) {
63
		$array = \explode ( "\\", $controllerName );
64
		$result = \ucfirst ( end ( $array ) );
65
		if (UString::endswith ( $result, "s" )) {
66
			$result = \substr ( $result, 0, - 1 );
67
		}
68
		return $result;
69
	}
70
71 2
	public function toJson($data) {
72 2
		return \json_encode ( $data );
73
	}
74
75
	public function formatException($e) {
76
		$error=new RestError(@$e->getCode (), \utf8_encode ( $e->getMessage ()),@$e->getTraceAsString(),@$e->getFile(),500);
77
		return $this->format ($error->asArray() );
78
	}
79
80
	public static function toXML($data, &$xml_data) {
81
		foreach ( $data as $key => $value ) {
82
			if (is_numeric ( $key )) {
83
				$key = 'item' . $key; // dealing with <0/>..<n/> issues
84
			}
85
			if (is_array ( $value )) {
86
				$subnode = $xml_data->addChild ( $key );
87
				self::toXML ( $value, $subnode );
88
			} else {
89
				$xml_data->addChild ( "$key", htmlspecialchars ( "$value" ) );
90
			}
91
		}
92
	}
93
}
94