Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
JsonApiResponseFormatter.php
Go to the documentation of this file.
1<?php
2
4
8
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 private $_included;
24
25 public function __construct($baseRoute = "") {
26 $this->baseRoute = $baseRoute;
27 }
28
34 public function cleanRestObject($o, &$classname = null) {
35 $pk = OrmUtils::getFirstKeyValue ( $o );
36 $classname = get_class ( $o );
37 $frontClassname = $this->getFrontClassname ( $classname );
38 $r = [ 'id' => $pk,'type' => $frontClassname ];
39 $r ['attributes'] = $o->_rest;
40 $fieldsInRelations = OrmUtils::getRelationInfos ( $classname );
41
42 $this->addSelfLink ( $r, $pk, $frontClassname );
43 $o = $o->_rest;
44 foreach ( $o as $k => $v ) {
45 if (isset ( $fieldsInRelations [$k] )) {
46 $rClassname = $fieldsInRelations [$k] ['className'] ?? $fieldsInRelations [$k] ['targetEntity'];
47 $rFrontClassname = $this->getFrontClassname ( $rClassname );
48 $member = $fieldsInRelations [$k] ['member'] ?? $k;
49 if (isset ( $v->_rest )) {
50 $pkf = OrmUtils::getFirstKey ( $rClassname );
51 $r ['relationships'] [$member] ['data'] = [ 'id' => $v->_rest [$pkf],'type' => $rFrontClassname ];
52 $this->_included [] = $this->cleanRestObject ( $v );
53 } elseif (\is_array ( $v )) {
54 foreach ( $v as $index => $value ) {
55 if (isset ( $value->_rest )) {
56 $this->_included [] = $this->cleanRestObject ( $value );
57 }
58 $pkf = OrmUtils::getFirstKey ( $rClassname );
59 $r ['relationships'] [$member] ['data'] [] = [ 'id' => $value->_rest [$pkf],'type' => $rFrontClassname ];
60 }
61 } else {
62 if (isset ( $v )) {
63 $r ['relationships'] [$member] ['data'] = [ 'id' => $v,'type' => $rFrontClassname ];
64 $this->addRelationshipsLink ( $r, $pk, $v, $frontClassname, $rFrontClassname, $member );
65 }
66 }
67
68 unset ( $r ['attributes'] [$member] );
69 unset ( $r ['attributes'] [$k] );
70 }
71 }
72 unset ( $r ['attributes'] ['id'] );
73 unset ( $r ['attributes'] ['_rest'] );
74 return $r;
75 }
76
77 protected function addSelfLink(&$r, $pk, $frontClassname) {
78 $r ['links'] ['self'] = $this->getLink ( $this->selfLink, [ "baseRoute" => $this->baseRoute,'id' => $pk,'classname' => $frontClassname ] );
79 }
80
88 protected function addPageLinks(&$r, $classname, $pages) {
89 $pageSize = $pages ['pageSize'];
90 unset ( $pages ['pageSize'] );
91 foreach ( $pages as $page => $number ) {
92 $r ['links'] [$page] = $this->getLink ( $this->pageLink, [ "baseRoute" => $this->baseRoute,'classname' => $classname,'pageNumber' => $number,'pageSize' => $pageSize ] );
93 }
94 }
95
96 protected function addRelationshipsLink(&$r, $pk, $pkMember, $frontClassname, $rFrontClassname, $member) {
97 $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 ] ) ];
98 }
99
100 private function getLink($pattern, $params = [ ]) {
101 $r = $pattern;
102 foreach ( $params as $k => $v ) {
103 $r = \str_replace ( '%' . $k . '%', $v, $r );
104 }
105 return $r;
106 }
107
108 private function getFrontClassname($classname) {
109 return \lcfirst ( ClassUtils::getClassSimpleName ( $classname ) );
110 }
111
117 public function get($objects, $pages = null) {
118 $objects = $this->getDatas ( $objects, $classname );
119 $r = [ 'data' => $objects ];
120 if ($this->_included) {
121 $r ['included'] = $this->_included;
122 }
123 if (isset ( $pages ) && \count ( $objects ) > 0) {
124 $this->addPageLinks ( $r, $this->getFrontClassname ( $classname ), $pages );
125 }
126 return $this->format ( $r );
127 }
128
134 public function getOne($object) {
135 $r = [ 'data' => $this->cleanRestObject ( $object ) ];
136 if ($this->_included) {
137 $r['included'] = $this->_included;
138 }
139 return $this->format ( $r );
140 }
141}
142
Manipulates class and namespace names Ubiquity\cache$ClassUtils This class is part of Ubiquity.
addRelationshipsLink(&$r, $pk, $pkMember, $frontClassname, $rFrontClassname, $member)
cleanRestObject($o, &$classname=null)
{Returns the array of attributes corresponding to an object $o.array}
getDatas($objects, &$classname=null)
Returns an array of datas from an array of objects.
format($arrayResponse)
Formats a response array.
Object/relational mapping utilities.
Definition OrmUtils.php:17