Passed
Push — master ( 7da913...a3998d )
by Jean-Christophe
05:48
created

JsonApiResponseFormatter::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Ubiquity\controllers\rest\api\jsonapi;
4
5
use Ubiquity\controllers\rest\ResponseFormatter;
6
use Ubiquity\orm\OrmUtils;
7
use Ubiquity\cache\ClassUtils;
8
9
class JsonApiResponseFormatter extends ResponseFormatter {
10
	private $selfLink = "%baseRoute%/%classname%/%id%/";
11
	private $relationLink="%baseRoute%/%classname%/%id%/%member%/";
12
	private $baseRoute;
13
	
14
	public function __construct($baseRoute=""){
15
		$this->baseRoute=$baseRoute;
16
	}
17
18
	/**
19
	 *
20
	 * {@inheritdoc}
21
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::cleanRestObject()
22
	 */
23
	public function cleanRestObject($o) {
24
		$pk=OrmUtils::getFirstKeyValue ( $o );
25
		$classname=get_class ( $o );
26
		$frontClassname=$this->getFrontClassname($classname);
27
		$r = [ 'id' => $pk,'type' => $frontClassname ];
28
		$r ['attributes'] = $o->_rest;
29
		$fieldsInRelations=OrmUtils::getRelationInfos($classname);
30
		$this->addSelfLink($r, $pk, $frontClassname);
31
		$o=$o->_rest;
32
		foreach ( $o as $k => $v ) {
33
			if(isset($fieldsInRelations[$k])){
34
				if (isset ( $v->_rest )) {
35
					$r ['included'] [$k] = $v->_rest;
36
				}elseif (\is_array ( $v )) {
37
					foreach ( $v as $index => $value ) {
38
						if (isset ( $value->_rest ))
39
							$v [$index] = $this->cleanRestObject ( $value );
40
					}
41
					$r ['included'] [$k] = $v;
42
				}else{
43
					$member=$fieldsInRelations[$k]['member']??$k;
44
					$rFrontClassname=$this->getFrontClassname($fieldsInRelations[$k]['className']);
45
					$r['relationships'][$member]['data']=['id'=>$v,'type'=>$rFrontClassname];
46
					$this->addRelationshipsLink($r, $pk, $v, $frontClassname, $rFrontClassname, $member);
47
				}
48
				
49
				unset($r['attributes'][$k]);
50
			}
51
		}
52
		unset($r['attributes']['id']);
53
		unset($r['attributes']['_rest']);
54
		return $r;
55
	}
56
	
57
	protected function addSelfLink(&$r,$pk,$frontClassname){
58
		$r['links']['self']=$this->getLink($this->selfLink,["baseRoute"=>$this->baseRoute,'id'=>$pk,'classname'=>$frontClassname]);
59
	}
60
	
61
	protected function addRelationshipsLink(&$r,$pk,$pkMember,$frontClassname,$rFrontClassname,$member){
62
		$r['relationships'][$member]['links']=[
63
				$this->getLink($this->relationLink,["baseRoute"=>$this->baseRoute,'id'=>$pk,'member'=>$member,'classname'=>$frontClassname]),
64
				$this->getLink($this->selfLink,["baseRoute"=>$this->baseRoute,'id'=>$pkMember,'classname'=>$rFrontClassname])];
65
	}
66
	
67
	private function getLink($pattern,$params=[]){
68
		$r=$pattern;
69
		foreach ($params as $k=>$v){
70
			$r=str_replace('%'.$k.'%', $v, $r);
71
		}
72
		return $r;
73
	}
74
	
75
	private function getFrontClassname($classname){
76
		return lcfirst(ClassUtils::getClassSimpleName ( $classname ));
77
	}
78
79
	/**
80
	 *
81
	 * {@inheritdoc}
82
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::format()
83
	 */
84
	public function format($arrayResponse) {
85
		return parent::format ( $arrayResponse );
86
	}
87
88
	/**
89
	 *
90
	 * {@inheritdoc}
91
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::formatException()
92
	 */
93
	public function formatException($e) {
94
		return parent::formatException ( $e );
95
	}
96
97
	/**
98
	 *
99
	 * {@inheritdoc}
100
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::get()
101
	 */
102
	public function get($datas) {
103
		$datas = $this->getDatas ( $datas );
104
		return $this->format ( [ "data" => $datas] );
105
	}
106
	
107
108
	/**
109
	 *
110
	 * {@inheritdoc}
111
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::getDatas()
112
	 */
113
	public function getDatas($datas) {
114
		return parent::getDatas ( $datas );
115
	}
116
117
	/**
118
	 *
119
	 * {@inheritdoc}
120
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::getJSONDatas()
121
	 */
122
	public function getJSONDatas($datas) {
123
		return parent::getJSONDatas ( $datas );
124
	}
125
126
	/**
127
	 *
128
	 * {@inheritdoc}
129
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::getModel()
130
	 */
131
	public function getModel($controllerName) {
132
		return parent::getModel ( $controllerName );
133
	}
134
135
	/**
136
	 *
137
	 * {@inheritdoc}
138
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::getOne()
139
	 */
140
	public function getOne($datas) {
141
		return parent::getOne ( $datas );
142
	}
143
144
	/**
145
	 *
146
	 * {@inheritdoc}
147
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::toJson()
148
	 */
149
	public function toJson($data) {
150
		return parent::toJson ( $data );
151
	}
152
}
153
154