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

JsonApiResponseFormatter   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 26
eloc 57
dl 0
loc 156
ccs 55
cts 70
cp 0.7856
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A formatException() 0 2 1
A format() 0 2 1
A addPageLinks() 0 5 2
A __construct() 0 2 1
A getOne() 0 2 1
A get() 0 7 3
A getDatas() 0 2 1
A getLink() 0 6 2
A getModel() 0 2 1
A toJson() 0 2 1
A getJSONDatas() 0 2 1
A getFrontClassname() 0 2 1
A addRelationshipsLink() 0 2 1
B cleanRestObject() 0 36 8
A addSelfLink() 0 2 1
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
/**
10
 * JsonAPI Formatter
11
 * Ubiquity\controllers\rest\api\jsonapi$JsonApiResponseFormatter
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.0
16
 * @since Ubiquity 2.0.11
17
 */
18
class JsonApiResponseFormatter extends ResponseFormatter {
19
	private $selfLink = "%baseRoute%/%classname%/%id%/";
20
	private $relationLink = "%baseRoute%/%classname%/%id%/%member%/";
21
	private $pageLink = "%baseRoute%/%classname%/?page[number]=%pageNumber%&page[size]=%pageSize%";
22
	private $baseRoute;
23
24 3
	public function __construct($baseRoute = "") {
25 3
		$this->baseRoute = $baseRoute;
26 3
	}
27
28
	/**
29
	 *
30
	 * {@inheritdoc}
31
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::cleanRestObject()
32
	 */
33 1
	public function cleanRestObject($o, &$classname = null) {
34 1
		$pk = OrmUtils::getFirstKeyValue ( $o );
35 1
		$classname = get_class ( $o );
36 1
		$frontClassname = $this->getFrontClassname ( $classname );
37 1
		$r = [ 'id' => $pk,'type' => $frontClassname ];
38 1
		$r ['attributes'] = $o->_rest;
39 1
		$fieldsInRelations = OrmUtils::getRelationInfos ( $classname );
40
41 1
		$this->addSelfLink ( $r, $pk, $frontClassname );
42 1
		$o = $o->_rest;
43 1
		foreach ( $o as $k => $v ) {
44 1
			if (isset ( $fieldsInRelations [$k] )) {
45 1
				$member = $fieldsInRelations [$k] ['member'] ?? $k;
46 1
				if (isset ( $v->_rest )) {
47 1
					$r ['included'] [$k] = $this->cleanRestObject ( $v );
48 1
				} elseif (\is_array ( $v )) {
49 1
					foreach ( $v as $index => $value ) {
50 1
						if (isset ( $value->_rest ))
51 1
							$v [$index] = $this->cleanRestObject ( $value );
52
					}
53 1
					$r ['included'] [$k] = $v;
54
				} else {
55 1
					if (isset ( $v )) {
56 1
						$rFrontClassname = $this->getFrontClassname ( $fieldsInRelations [$k] ['className'] );
57 1
						$r ['relationships'] [$member] ['data'] = [ 'id' => $v,'type' => $rFrontClassname ];
58 1
						$this->addRelationshipsLink ( $r, $pk, $v, $frontClassname, $rFrontClassname, $member );
59
					}
60
				}
61
62 1
				unset ( $r ['attributes'] [$member] );
63 1
				unset ( $r ['attributes'] [$k] );
64
			}
65
		}
66 1
		unset ( $r ['attributes'] ['id'] );
67 1
		unset ( $r ['attributes'] ['_rest'] );
68 1
		return $r;
69
	}
70
71 1
	protected function addSelfLink(&$r, $pk, $frontClassname) {
72 1
		$r ['links'] ['self'] = $this->getLink ( $this->selfLink, [ "baseRoute" => $this->baseRoute,'id' => $pk,'classname' => $frontClassname ] );
73 1
	}
74
75
	protected function addPageLinks(&$r, $classname, $pages) {
76
		$pageSize = $pages ['pageSize'];
77
		unset ( $pages ['pageSize'] );
78
		foreach ( $pages as $page => $number ) {
79
			$r ['links'] [$page] = $this->getLink ( $this->pageLink, [ "baseRoute" => $this->baseRoute,'classname' => $classname,'pageNumber' => $number,'pageSize' => $pageSize ] );
80
		}
81
	}
82
83 1
	protected function addRelationshipsLink(&$r, $pk, $pkMember, $frontClassname, $rFrontClassname, $member) {
84 1
		$r ['relationships'] [$member] ['links'] = [ $this->getLink ( $this->relationLink, [ "baseRoute" => $this->baseRoute,'id' => $pk,'member' => $member,'classname' => $frontClassname ] ),$this->getLink ( $this->selfLink, [ "baseRoute" => $this->baseRoute,'id' => $pkMember,'classname' => $rFrontClassname ] ) ];
85 1
	}
86
87 1
	private function getLink($pattern, $params = []) {
88 1
		$r = $pattern;
89 1
		foreach ( $params as $k => $v ) {
90 1
			$r = str_replace ( '%' . $k . '%', $v, $r );
91
		}
92 1
		return $r;
93
	}
94
95 1
	private function getFrontClassname($classname) {
96 1
		return lcfirst ( ClassUtils::getClassSimpleName ( $classname ) );
97
	}
98
99
	/**
100
	 *
101
	 * {@inheritdoc}
102
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::format()
103
	 */
104 1
	public function format($arrayResponse) {
105 1
		return parent::format ( $arrayResponse );
106
	}
107
108
	/**
109
	 *
110
	 * {@inheritdoc}
111
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::formatException()
112
	 */
113
	public function formatException($e) {
114
		return parent::formatException ( $e );
115
	}
116
117
	/**
118
	 *
119
	 * {@inheritdoc}
120
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::get()
121
	 */
122 1
	public function get($datas, $pages = null) {
123 1
		$datas = $this->getDatas ( $datas, $classname );
124 1
		$r = [ 'data' => $datas ];
125 1
		if (isset ( $pages ) && sizeof ( $datas ) > 0) {
126
			$this->addPageLinks ( $r, $this->getFrontClassname ( $classname ), $pages );
127
		}
128 1
		return $this->format ( $r );
129
	}
130
131
	/**
132
	 *
133
	 * {@inheritdoc}
134
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::getDatas()
135
	 */
136 1
	public function getDatas($datas, &$classname) {
137 1
		return parent::getDatas ( $datas, $classname );
138
	}
139
140
	/**
141
	 *
142
	 * {@inheritdoc}
143
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::getJSONDatas()
144
	 */
145
	public function getJSONDatas($datas) {
146
		return parent::getJSONDatas ( $datas );
147
	}
148
149
	/**
150
	 *
151
	 * {@inheritdoc}
152
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::getModel()
153
	 */
154
	public function getModel($controllerName) {
155
		return parent::getModel ( $controllerName );
156
	}
157
158
	/**
159
	 *
160
	 * {@inheritdoc}
161
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::getOne()
162
	 */
163 1
	public function getOne($datas) {
164 1
		return parent::getOne ( $datas );
165
	}
166
167
	/**
168
	 *
169
	 * {@inheritdoc}
170
	 * @see \Ubiquity\controllers\rest\ResponseFormatter::toJson()
171
	 */
172
	public function toJson($data) {
173
		return parent::toJson ( $data );
174
	}
175
}
176
177