Changes for page #[RPC\Response]
Last modified by Ashterix on 2024/05/19 21:13
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,33 +1,39 @@ 1 -(% class="box floatinginfobox" %) 2 -((( 1 +{{box cssClass="floatinginfobox" width="400px"}} 3 3 == (% style="display:block; margin-top:-30px; text-align:center" %)Summary(%%) == 4 4 5 -|**Classname**|(% colspan="2" rowspan="1" %)((( 4 +(% style="margin-right:auto; width:353px" %) 5 +|(% style="width:141px" %)**Classname**|(% colspan="2" rowspan="1" style="width:79px" %)((( 6 6 Response 7 7 ))) 8 -|**Namespace**|(% colspan="2" %)((( 8 +|(% style="width:141px" %)**Namespace**|(% colspan="2" style="width:79px" %)((( 9 9 Ufo\RpcObject\RPC 10 10 ))) 11 -|**Target**|(% colspan="2" rowspan="1" %)method 12 -|(% colspan="3" %)**Arguments:** 13 -|(% colspan="1" rowspan="3" %)((( 11 +|(% style="width:141px" %)((( 12 +**Target** 13 +)))|(% colspan="2" rowspan="1" style="width:79px" %)method 14 +|(% colspan="3" style="width:141px" %)**Arguments:** 15 +|(% colspan="1" rowspan="3" style="width:141px" %)((( 14 14 $responseFormat 15 -)))|**type**|array 16 -|**optional**|true 17 -|**default**|[] 18 -|(% colspan="1" rowspan="3" %)$dto|**type**|string 19 -|**optional**|true 20 -|**default**|'' 21 -|(% colspan="1" rowspan="3" %)$collection|**type**|bool 22 -|**optional**|true 23 -|**default**|false 24 -))) 17 +)))|(% style="width:79px" %)**type**|(% style="width:153px" %)array 18 +|(% style="width:79px" %)**optional**|(% style="width:153px" %)true 19 +|(% style="width:79px" %)**default**|(% style="width:153px" %)[] 20 +|(% colspan="1" rowspan="3" style="width:141px" %)((( 21 +$dto 22 +)))|(% style="width:79px" %)**type**|(% style="width:153px" %)string 23 +|(% style="width:79px" %)**optional**|(% style="width:153px" %)true 24 +|(% style="width:79px" %)**default**|(% style="width:153px" %)'' 25 +|(% colspan="1" rowspan="3" style="width:141px" %)((( 26 +$collection 27 +)))|(% style="width:79px" %)**type**|(% style="width:153px" %)bool 28 +|(% style="width:79px" %)**optional**|(% style="width:153px" %)true 29 +|(% style="width:79px" %)**default**|(% style="width:153px" %)false 30 +{{/box}} 25 25 26 26 = Enhanced Response Format = 27 27 28 -By default, the documentation for the response of each API service is based on the (%class="box code"%)return type(%%), which works well with primitive types. However, if your method returns an associative array, an object, or a collection of objects, the information about the data type of the response becomes insufficient.34 +By default, the documentation for the response of each API service is based on the {{code language="none"}}return type{{/code}}, which works well with primitive types. However, if your method returns an associative array, an object, or a collection of objects, the information about the data type of the response becomes insufficient. 29 29 30 -To provide clients with more information about the format of more complex responses, the attribute (%class="box code"%)#[RPC\Response](%%)was created.36 +To provide clients with more information about the format of more complex responses, the attribute {{code language="none"}}#[RPC\Response]{{/code}} was created. 31 31 32 32 Let's consider an example that demonstrates the problem and its solution. You have a class containing API methods that return information about users: 33 33 ... ... @@ -35,199 +35,90 @@ 35 35 * getUserInfoAsArray(int $id) - returns information about a user in the form of an array 36 36 * getUsersList() - returns a collection of user objects 37 37 38 -(% class="box" %) 39 -((( 40 -(% class="code" %) 41 -((( 42 -(% class="linenoswrapper" %) 43 -((( 44 -(% class="linenos" %) 45 -((( 46 -1 47 -2 48 -3 49 -4 50 -5 51 -6 52 -7 53 -8 54 -9 55 -10 56 -11 57 -12 58 -13 59 -14 60 -15 61 -16 62 -17 63 -18 64 -19 65 -20 66 -21 67 -22 68 -23 69 -24 70 -25 71 -26 72 -27 73 -28 74 -29 75 -30 76 -31 77 -32 78 -33\\ 79 -))) 44 +{{code language="php" layout="LINENUMBERS"}} 45 +<?php 46 +namespace App\Api\Procedures; 80 80 81 -((( 82 -(% style="color: #BC7A00;" %)<?php(%%) 83 -(% style="font-weight: bold; color: #008000;" %)namespace(%%) App\Api\Procedures; 84 -\\(% style="font-weight: bold; color: #008000;" %)use(%%) App\Services\UserService; 85 -(% style="font-weight: bold; color: #008000;" %)use(%%) Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService; 86 -\\(% style="font-weight: bold; color: #008000;" %)class(%%) (% style="font-weight: bold; color: #0000FF;" %)ExampleApi(%%) (% style="font-weight: bold; color: #008000;" %)implements(%%) IRpcService 48 +use App\Services\UserService; 49 +use Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService; 50 + 51 +class ExampleApi implements IRpcService 87 87 { 88 - (% style="font-weight: bold; color: #008000;" %)public(%%)(% style="font-weight: bold; color: #008000;" %)function(%%)(% style="color: #0000FF;" %)~_~_construct(%%)(89 - (% style="font-weight: bold; color: #008000;" %)protected(%%)UserService(% style="color: #19177C;" %)$userService(%%)53 + public function __construct( 54 + protected UserService $userService 90 90 ) {} 91 -\\ (% style="font-weight: bold; color: #008000;" %)public(%%) (% style="font-weight: bold; color: #008000;" %)function(%%) (% style="color: #0000FF;" %)getUserInfo(%%)(int (% style="color: #19177C;" %)$id(%%))(% style="color: #666666;" %):(%%) User 56 + 57 + public function getUserInfo(int $id): User 92 92 { 93 - (% style="font-weight: bold; color: #008000;" %)return(%%)(% style="font-weight: bold; color: #008000;" %)this(% style="color: #666666;" %)->(% style="color: #7D9029;" %)userService(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getById(%%)((% style="color: #19177C;" %)$id(%%));59 + return this->userService->getById($id); 94 94 } 95 -\\ (% style="font-weight: bold; color: #008000;" %)public(%%) (% style="font-weight: bold; color: #008000;" %)function(%%) (% style="color: #0000FF;" %)getUserInfoAsArray(%%)(int (% style="color: #19177C;" %)$id(%%))(% style="color: #666666;" %):(%%) (% style="font-weight: bold; color: #008000;" %)array(%%) 61 + 62 + public function getUserInfoAsArray(int $id): array 96 96 { 97 - (% style="color: #19177C;" %)$user(%%)(% style="color:#666666;" %)=(%%) (% style="font-weight: bold; color: #008000;" %)this(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getUserInfo(%%)((% style="color: #19177C;" %)$id(%%));98 - (% style="font-weight: bold; color: #008000;" %)return(%%)[99 - (% style="color: #BA2121;" %)'id'(%%)(% style="color: #666666;" %)=>(%%)(% style="color: #19177C;" %)$user(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getId(%%)(),100 - (% style="color: #BA2121;" %)'email'(%%)(% style="color: #666666;" %)=>(%%)(% style="color: #19177C;" %)$user(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getEmail(%%)(),101 - (% style="color: #BA2121;" %)'name'(%%)(% style="color: #666666;" %)=>(%%)(% style="color: #19177C;" %)$user(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getName(%%)(),64 + $user = this->getUserInfo($id); 65 + return [ 66 + 'id' => $user->getId(), 67 + 'email' => $user->getEmail(), 68 + 'name' => $user->getName(), 102 102 ]; 103 103 } 104 -\\ (% style="font-weight: bold; color: #008000;" %)public(%%) (% style="font-weight: bold; color: #008000;" %)function(%%) (% style="color: #0000FF;" %)getUsersList(%%)()(% style="color: #666666;" %):(%%) (% style="font-weight: bold; color: #008000;" %)array(%%) 71 + 72 + public function getUsersList(): array 105 105 { 106 - (% style="font-weight: bold; color: #008000;" %)return(%%)(% style="font-weight: bold; color: #008000;" %)this(% style="color: #666666;" %)->(% style="color: #7D9029;" %)userService(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getAll(%%)();74 + return this->userService->getAll(); 107 107 } 108 -}\\ 109 -))) 110 -))) 111 -))) 112 -))) 76 +} 113 113 78 +{{/code}} 79 + 114 114 Let's look at the documentation generated by this instruction 115 115 116 -(% class="box" %) 117 -((( 118 -(% class="code" %) 119 -((( 120 -(% class="linenoswrapper" %) 121 -((( 122 -(% class="linenos" %) 123 -((( 124 -1 125 -2 126 -3 127 -4 128 -5 129 -6 130 -7 131 -8 132 -9 133 -10 134 -11 135 -12 136 -13 137 -14 138 -15 139 -16 140 -17 141 -18 142 -19 143 -20 144 -21 145 -22 146 -23 147 -24 148 -25 149 -26 150 -27 151 -28 152 -29 153 -30 154 -31 155 -32 156 -33 157 -34 158 -35 159 -36 160 -37 161 -38 162 -39 163 -40 164 -41 165 -42 166 -43 167 -44 168 -45 169 -46 170 -47 171 -48 172 -49 173 -50\\ 174 -))) 175 - 176 -((( 82 +{{code language="json" layout="LINENUMBERS"}} 177 177 { 178 - (% style="font-weight: bold; color: #008000;" %)"envelope"(%%): (% style="color: #BA2121;" %)"JSON-RPC-2.0/UFO-RPC-6"(%%), 179 - (% style="font-weight: bold; color: #008000;" %)"contentType"(%%): (% style="color: #BA2121;" %)"application/json"(%%), 180 - (% style="font-weight: bold; color: #008000;" %)"description"(%%): (% style="color: #BA2121;" %)""(%%), 181 - (% style="font-weight: bold; color: #008000;" %)"transport"(%%): { 182 - (% style="font-weight: bold; color: #008000;" %)"sync"(%%): { 183 - (% style="font-weight: bold; color: #008000;" %)"scheme"(%%): (% style="color: #BA2121;" %)"https"(%%), 184 - (% style="font-weight: bold; color: #008000;" %)"host"(%%): (% style="color: #BA2121;" %)"example.com"(%%), 185 - (% style="font-weight: bold; color: #008000;" %)"path"(%%): (% style="color: #BA2121;" %)"/api"(%%), 186 - (% style="font-weight: bold; color: #008000;" %)"method"(%%): (% style="color: #BA2121;" %)"POST"(%%) 187 - } 188 - }, 189 - (% style="font-weight: bold; color: #008000;" %)"methods"(%%): { 190 - (% style="font-weight: bold; color: #008000;" %)"ExampleApi.getUserInfo"(%%): { 191 - (% style="font-weight: bold; color: #008000;" %)"name"(%%): (% style="color: #BA2121;" %)"ExampleApi.getUserInfo"(%%), 192 - (% style="font-weight: bold; color: #008000;" %)"description"(%%): (% style="color: #BA2121;" %)""(%%), 193 - (% style="font-weight: bold; color: #008000;" %)"parameters"(%%): { 194 - (% style="font-weight: bold; color: #008000;" %)"id"(%%): { 195 - (% style="font-weight: bold; color: #008000;" %)"type"(%%): (% style="color: #BA2121;" %)"integer"(%%), 196 - (% style="font-weight: bold; color: #008000;" %)"name"(%%): (% style="color: #BA2121;" %)"id"(%%), 197 - (% style="font-weight: bold; color: #008000;" %)"description"(%%): (% style="color: #BA2121;" %)""(%%), 198 - (% style="font-weight: bold; color: #008000;" %)"optional"(%%): (% style="font-weight: bold; color: #008000;" %)false(%%) 84 + "envelope": "JSON-RPC-2.0/UFO-RPC-6", 85 + "transport": "POST", 86 + "target": "/api", 87 + "contentType": "application/json", 88 + "description": "", 89 + "methods": { 90 + "ExampleApi.getUserInfo": { 91 + "name": "ExampleApi.getUserInfo", 92 + "description": "", 93 + "parameters": { 94 + "id": { 95 + "type": "integer", 96 + "name": "id", 97 + "description": "", 98 + "optional": false 199 199 } 200 200 }, 201 - (% style="font-weight: bold; color: #008000;" %)"returns"(%%):(% style="color: #BA2121;" %)"object"(%%),202 - (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%):(% style="color: #BA2121;" %)"object"(%%)101 + "returns": "object", 102 + "responseFormat": "object" 203 203 }, 204 - (% style="font-weight: bold; color: #008000;" %)"ExampleApi.getUserInfoAsArray"(%%): {205 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"ExampleApi.getUserInfoAsArray"(%%),206 - (% style="font-weight: bold; color: #008000;" %)"description"(%%):(% style="color: #BA2121;"%)""(%%),207 - (% style="font-weight: bold; color: #008000;" %)"parameters"(%%): {208 - (% style="font-weight: bold; color: #008000;"%)"id"(%%): {209 - (% style="font-weight: bold; color: #008000;" %)"type"(%%):(% style="color: #BA2121;" %)"integer"(%%),210 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"id"(%%),211 - (% style="font-weight: bold; color: #008000;" %)"description"(%%):(% style="color: #BA2121;"%)""(%%),212 - (% style="font-weight: bold; color: #008000;" %)"optional"(%%):(% style="font-weight: bold; color: #008000;" %)false(%%)104 + "ExampleApi.getUserInfoAsArray": { 105 + "name": "ExampleApi.getUserInfoAsArray", 106 + "description": "", 107 + "parameters": { 108 + "id": { 109 + "type": "integer", 110 + "name": "id", 111 + "description": "", 112 + "optional": false 213 213 } 214 214 }, 215 - (% style="font-weight: bold; color: #008000;" %)"returns"(%%):(% style="color: #BA2121;" %)"array"(%%),216 - (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%):(% style="color: #BA2121;" %)"array"(%%)115 + "returns": "array", 116 + "responseFormat": "array" 217 217 }, 218 - (% style="font-weight: bold; color: #008000;" %)"ExampleApi.getUsersList"(%%): {219 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"ExampleApi.getUsersList"(%%),220 - (% style="font-weight: bold; color: #008000;" %)"description"(%%):(% style="color: #BA2121;"%)""(%%),221 - (% style="font-weight: bold; color: #008000;" %)"parameters"(%%): [],222 - (% style="font-weight: bold; color: #008000;" %)"returns"(%%):(% style="color: #BA2121;" %)"array"(%%),223 - (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%):(% style="color: #BA2121;" %)"array"(%%)118 + "ExampleApi.getUsersList": { 119 + "name": "ExampleApi.getUsersList", 120 + "description": "", 121 + "parameters": [], 122 + "returns": "array", 123 + "responseFormat": "array" 224 224 } 225 225 } 226 226 } 227 -))) 228 -))) 229 -))) 230 -))) 127 +{{/code}} 231 231 232 232 We are interested in returns and responseFormat (lines 19-20, 33-34, and 40-41), we see that the RPC Server interpreted User as an object and this information does not tell the client how to work with the response object, what properties it should have, nor is there information about the responses of the other two methods, in both cases an array should be returned and we know nothing about its contents. 233 233 ... ... @@ -243,193 +243,98 @@ 243 243 ((( 244 244 (% class="col-xs-12 col-sm-6" %) 245 245 ((( 246 -(% class="box" %) 247 -((( 248 -(% class="code" %) 249 -((( 250 -(% class="linenoswrapper" %) 251 -((( 252 -(% class="linenos" %) 253 -((( 254 -1 255 -2 256 -3 257 -4 258 -5 259 -6 260 -7 261 -8 262 -9 263 -10 264 -11 265 -12 266 -13 267 -14 268 -15 269 -16 270 -17 271 -18 272 -19 273 -20 274 -21 275 -22 276 -23 277 -24 278 -25 279 -26 280 -27 281 -28 282 -29 283 -30 284 -31 285 -32 286 -33 287 -34 288 -35 289 -36 290 -37\\ 291 -))) 143 +{{code language="php" layout="LINENUMBERS"}} 144 +<?php 145 +namespace App\Api\Procedures; 292 292 293 -((( 294 -(% style="color: #BC7A00;" %)<?php(%%) 295 -(% style="font-weight: bold; color: #008000;" %)namespace(%%) App\Api\Procedures; 296 -\\(% style="font-weight: bold; color: #008000;" %)use(%%) App\Services\UserService; 297 -(% style="font-weight: bold; color: #008000;" %)use(%%) Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService; 298 -(% style="font-weight: bold; color: #008000;" %)use(%%) Ufo\RpcObject\RPC; 299 -\\(% style="font-weight: bold; color: #008000;" %)class(%%) (% style="font-weight: bold; color: #0000FF;" %)ExampleApi(%%) (% style="font-weight: bold; color: #008000;" %)implements(%%) IRpcService 147 +use App\Services\UserService; 148 +use Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService; 149 +use Ufo\RpcObject\RPC; 150 + 151 +class ExampleApi implements IRpcService 300 300 { 301 - (% style="font-weight: bold; color: #008000;" %)public(%%)(% style="font-weight: bold; color: #008000;" %)function(%%)(% style="color: #0000FF;" %)~_~_construct(%%)(302 - (% style="font-weight: bold; color: #008000;" %)protected(%%)UserService(% style="color: #19177C;" %)$userService(%%)153 + public function __construct( 154 + protected UserService $userService 303 303 ) {} 304 -\\ (% style="font-style: italic; color: #408080;" %)#[RPC\Response(['id' => 'int', 'email' => 'string', 'name' => 'string'])] 305 -(%%) (% style="font-weight: bold; color: #008000;" %)public(%%) (% style="font-weight: bold; color: #008000;" %)function(%%) (% style="color: #0000FF;" %)getUserInfo(%%)(int (% style="color: #19177C;" %)$id(%%))(% style="color: #666666;" %):(%%) User 156 + 157 + #[RPC\Response(['id' => 'int', 'email' => 'string', 'name' => 'string'])] 158 + public function getUserInfo(int $id): User 306 306 { 307 - (% style="font-weight: bold; color: #008000;" %)return(%%)(% style="font-weight: bold; color: #008000;" %)this(% style="color: #666666;" %)->(% style="color: #7D9029;" %)userService(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getById(%%)((% style="color: #19177C;" %)$id(%%));160 + return this->userService->getById($id); 308 308 } 309 -\\ (% style="font-style: italic; color: #408080;" %)#[RPC\Response(['id' => 'int', 'email' => 'string', 'name' => 'string'])] 310 -(%%) (% style="font-weight: bold; color: #008000;" %)public(%%) (% style="font-weight: bold; color: #008000;" %)function(%%) (% style="color: #0000FF;" %)getUserInfoAsArray(%%)(int (% style="color: #19177C;" %)$id(%%))(% style="color: #666666;" %):(%%) (% style="font-weight: bold; color: #008000;" %)array(%%) 162 + 163 + #[RPC\Response(['id' => 'int', 'email' => 'string', 'name' => 'string'])] 164 + public function getUserInfoAsArray(int $id): array 311 311 { 312 - (% style="color: #19177C;" %)$user(%%)(% style="color:#666666;" %)=(%%) (% style="font-weight: bold; color: #008000;" %)this(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getUserInfo(%%)((% style="color: #19177C;" %)$id(%%));313 - (% style="font-weight: bold; color: #008000;" %)return(%%)[314 - (% style="color: #BA2121;" %)'id'(%%)(% style="color: #666666;" %)=>(%%)(% style="color: #19177C;" %)$user(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getId(%%)(),315 - (% style="color: #BA2121;" %)'email'(%%)(% style="color: #666666;" %)=>(%%)(% style="color: #19177C;" %)$user(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getEmail(%%)(),316 - (% style="color: #BA2121;" %)'name'(%%)(% style="color: #666666;" %)=>(%%)(% style="color: #19177C;" %)$user(% style="color: #BA2121;" %)'s->getName(),166 + $user = this->getUserInfo($id); 167 + return [ 168 + 'id' => $user->getId(), 169 + 'email' => $user->getEmail(), 170 + 'name' => $user's->getName(), 317 317 ]; 318 318 } 319 -\\ #[RPC\Response(~[~['(%%)id(% style="color: #BA2121;" %)' => '(%%)int(% style="color: #BA2121;" %)', '(%%)email(% style="color: #BA2121;" %)' => '(%%)string(% style="color: #BA2121;" %)', '(%%)name(% style="color: #BA2121;" %)' => '(%%)string(% style="border: 1px solid #FF0000;" %)'(%%)])]] 320 - (% style="font-weight: bold; color: #008000;" %)public(%%) (% style="font-weight: bold; color: #008000;" %)function(%%) (% style="color: #0000FF;" %)getUsersList(%%)()(% style="color: #666666;" %):(%%) (% style="font-weight: bold; color: #008000;" %)array(%%) 173 + 174 + #[RPC\Response([['id' => 'int', 'email' => 'string', 'name' => 'string'])]] 175 + public function getUsersList(): array 321 321 { 322 - (% style="font-weight: bold; color: #008000;" %)return(%%)(% style="font-weight: bold; color: #008000;" %)this(% style="color: #666666;" %)->(% style="color: #7D9029;" %)userService(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getAll(%%)();177 + return this->userService->getAll(); 323 323 } 324 -}\\ 325 -))) 326 -))) 327 -))) 328 -))) 179 +} 329 329 181 +{{/code}} 182 + 330 330 Note the changed documentation format on the right, responseFormat now has detail and provides a clear idea of the response structure. 331 331 ))) 332 332 333 333 (% class="col-xs-12 col-sm-6" %) 334 334 ((( 335 -(% class="box" %) 188 +{{code language="json" layout="LINENUMBERS" title="(% class=~"box warningmessage~" %) 336 336 ((( 337 -(% class="box warningmessage" %) 338 -((( 339 339 To simplify the documentation, in the response example I will remove other elements except those intended to demonstrate 340 -))) 341 - 342 -(% class="code" %) 343 -((( 344 -(% class="linenoswrapper" %) 345 -((( 346 -(% class="linenos" %) 347 -((( 348 -1 349 -2 350 -3 351 -4 352 -5 353 -6 354 -7 355 -8 356 -9 357 -10 358 -11 359 -12 360 -13 361 -14 362 -15 363 -16 364 -17 365 -18 366 -19 367 -20 368 -21 369 -22 370 -23 371 -24 372 -25 373 -26 374 -27 375 -28 376 -29 377 -30 378 -31 379 -32 380 -33 381 -34 382 -35 383 -36\\ 384 -))) 385 - 386 -((( 191 +)))"}} 387 387 { 388 - (% style="font-weight: bold; color: #008000;" %)"methods"(%%): {389 - (% style="font-weight: bold; color: #008000;" %)"ExampleApi.getUserInfo"(%%): {390 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"ExampleApi.getUserInfo"(%%),391 - (% style="border: 1px solid #FF0000;" %)...,(%%)392 - (% style="font-weight: bold; color: #008000;" %)"returns"(%%):(% style="color: #BA2121;" %)"object"(%%),393 - (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%): {394 - (% style="font-weight: bold; color: #008000;"%)"id"(%%):(% style="color: #BA2121;" %)"int"(%%),395 - (% style="font-weight: bold; color: #008000;" %)"email"(%%):(% style="color: #BA2121;" %)"string"(%%),396 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"string"(%%)193 + "methods": { 194 + "ExampleApi.getUserInfo": { 195 + "name": "ExampleApi.getUserInfo", 196 + ... 197 + "returns": "object", 198 + "responseFormat": { 199 + "id": "int", 200 + "email": "string", 201 + "name": "string" 397 397 } 398 398 }, 399 - (% style="font-weight: bold; color: #008000;" %)"ExampleApi.getUserInfoAsArray"(%%): {400 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"ExampleApi.getUserInfoAsArray"(%%),401 - (% style="border: 1px solid #FF0000;" %)...,(%%)402 - (% style="font-weight: bold; color: #008000;" %)"returns"(%%):(% style="color: #BA2121;" %)"array"(%%),403 - (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%): {404 - (% style="font-weight: bold; color: #008000;"%)"id"(%%):(% style="color: #BA2121;" %)"int"(%%),405 - (% style="font-weight: bold; color: #008000;" %)"email"(%%):(% style="color: #BA2121;" %)"string"(%%),406 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"string"(%%)204 + "ExampleApi.getUserInfoAsArray": { 205 + "name": "ExampleApi.getUserInfoAsArray", 206 + ... 207 + "returns": "array", 208 + "responseFormat": { 209 + "id": "int", 210 + "email": "string", 211 + "name": "string" 407 407 } 408 408 }, 409 - (% style="font-weight: bold; color: #008000;" %)"ExampleApi.getUsersList"(%%): {410 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"ExampleApi.getUsersList"(%%),411 - (% style="border: 1px solid #FF0000;" %)...,(%%)412 - (% style="font-weight: bold; color: #008000;" %)"returns"(%%):(% style="color: #BA2121;" %)"array"(%%),413 - (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%): [214 + "ExampleApi.getUsersList": { 215 + "name": "ExampleApi.getUsersList", 216 + ... 217 + "returns": "array", 218 + "responseFormat": [ 414 414 { 415 - (% style="font-weight: bold; color: #008000;"%)"id"(%%):(% style="color: #BA2121;" %)"int"(%%),416 - (% style="font-weight: bold; color: #008000;" %)"email"(%%):(% style="color: #BA2121;" %)"string"(%%),417 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"string"(%%)220 + "id": "int", 221 + "email": "string", 222 + "name": "string" 418 418 } 419 419 ] 420 420 } 421 421 } 422 422 } 423 -))) 424 -))) 425 -))) 426 -))) 228 +{{/code}} 427 427 428 428 429 429 ))) 430 430 ))) 431 431 432 -You might notice that using an array in such an example seems inconvenient because even within just this one class we used it three times and if you need to change the structure of the user properties this array will have to be changed in many places. In other words, this violates [[DRY>> url:https://ru.wikipedia.org/wiki/Don%E2%80%99t_repeat_yourself]]. That's why I mentioned that this option is only suitable as a one-time. In other cases, you need to use a different approach.234 +You might notice that using an array in such an example seems inconvenient because even within just this one class we used it three times and if you need to change the structure of the user properties this array will have to be changed in many places. In other words, this violates [[DRY>>https://ru.wikipedia.org/wiki/Don%E2%80%99t_repeat_yourself]]. That's why I mentioned that this option is only suitable as a one-time. In other cases, you need to use a different approach. 433 433 434 434 == **Option 2 - (recommended)** == 435 435 ... ... @@ -440,220 +440,103 @@ 440 440 ((( 441 441 (% class="col-xs-12 col-sm-6" %) 442 442 ((( 443 -(% class="box" %) 444 -((( 445 -(% class="code" %) 446 -((( 447 -(% class="linenoswrapper" %) 448 -((( 449 -(% class="linenos" %) 450 -((( 451 -1 452 -2 453 -3 454 -4 455 -5 456 -6 457 -7 458 -8 459 -9\\ 460 -))) 245 +{{code language="php" layout="LINENUMBERS"}} 246 +<?php 247 +namespace App\Api\DTO; 461 461 462 -((( 463 -(% style="color: #BC7A00;" %)<?php(%%) 464 -(% style="font-weight: bold; color: #008000;" %)namespace(%%) App\Api\DTO; 465 -\\readonly (% style="font-weight: bold; color: #008000;" %)class(%%) (% style="font-weight: bold; color: #0000FF;" %)UserDTO(%%) 249 +readonly class UserDTO 466 466 { 467 - (% style="font-weight: bold; color: #008000;" %)public(%%)int(% style="color: #19177C;" %)$id(%%);468 - (% style="font-weight: bold; color: #008000;" %)public(%%)string(% style="color: #19177C;" %)$email(%%);469 - (% style="font-weight: bold; color: #008000;" %)public(%%)string(% style="color: #19177C;" %)$name(%%);251 + public int $id; 252 + public string $email; 253 + public string $name; 470 470 } 471 -))) 472 -))) 473 -))) 474 -))) 255 +{{/code}} 475 475 476 -(% class="box" %) 477 -((( 478 -(% class="code" %) 479 -((( 480 -(% class="linenoswrapper" %) 481 -((( 482 -(% class="linenos" %) 483 -((( 484 -1 485 -2 486 -3 487 -4 488 -5 489 -6 490 -7 491 -8 492 -9 493 -10 494 -11 495 -12 496 -13 497 -14 498 -15 499 -16 500 -17 501 -18 502 -19 503 -20 504 -21 505 -22 506 -23 507 -24 508 -25 509 -26 510 -27 511 -28 512 -29 513 -30 514 -31 515 -32 516 -33 517 -34 518 -35 519 -36 520 -37 521 -38\\ 522 -))) 257 +{{code language="php" layout="LINENUMBERS"}} 258 +<?php 259 +namespace App\Api\Procedures; 523 523 524 -((( 525 -(% style="color: #BC7A00;" %)<?php(%%) 526 -(% style="font-weight: bold; color: #008000;" %)namespace(%%) App\Api\Procedures; 527 -\\(% style="font-weight: bold; color: #008000;" %)use(%%) App\Services\UserService; 528 -(% style="font-weight: bold; color: #008000;" %)use(%%) Ufo\JsonRpcBundle(% style="color: #666666;" %).(%%)ApiMethod\Interfaces\IRpcService; 529 -(% style="font-weight: bold; color: #008000;" %)use(%%) Ufo\RpcObject\RPC; 530 -(% style="font-weight: bold; color: #008000;" %)use(%%) App\Api\DTO\UserDTO; 531 -\\(% style="font-weight: bold; color: #008000;" %)class(%%) (% style="font-weight: bold; color: #0000FF;" %)ExampleApi(%%) (% style="font-weight: bold; color: #008000;" %)implements(%%) IRpcService 261 +use App\Services\UserService; 262 +use Ufo\JsonRpcBundle.ApiMethod\Interfaces\IRpcService; 263 +use Ufo\RpcObject\RPC; 264 +use App\Api\DTO\UserDTO; 265 + 266 +class ExampleApi implements IRpcService 532 532 { 533 - (% style="font-weight: bold; color: #008000;" %)public(%%)(% style="font-weight: bold; color: #008000;" %)function(%%)(% style="color: #0000FF;" %)~_~_construct(%%)(534 - (% style="font-weight: bold; color: #008000;" %)protected(%%)UserService(% style="color: #19177C;" %)$userService(%%)268 + public function __construct( 269 + protected UserService $userService 535 535 ) {} 536 -\\ (% style="font-style: italic; color: #408080;" %)#[RPC\Response(dto: UserDTO::class)] 537 -(%%) (% style="font-weight: bold; color: #008000;" %)public(%%) (% style="font-weight: bold; color: #008000;" %)function(%%) (% style="color: #0000FF;" %)getUserInfo(%%)(int (% style="color: #19177C;" %)$id(%%))(% style="color: #666666;" %):(%%) User 271 + 272 + #[RPC\Response(dto: UserDTO::class)] 273 + public function getUserInfo(int $id): User 538 538 { 539 - (% style="font-weight: bold; color: #008000;" %)return(%%)(% style="font-weight: bold; color: #008000;" %)this(% style="color: #666666;" %)->(% style="color: #7D9029;" %)userService(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getById(%%)((% style="color: #19177C;" %)$id(%%));275 + return this->userService->getById($id); 540 540 } 541 -\\ (% style="font-style: italic; color: #408080;" %)#[RPC\Response(dto: UserDTO::class)] 542 -(%%) (% style="font-weight: bold; color: #008000;" %)public(%%) (% style="font-weight: bold; color: #008000;" %)function(%%) (% style="color: #0000FF;" %)getUserInfoAsArray(%%)(int (% style="color: #19177C;" %)$id(%%))(% style="color: #666666;" %):(%%) (% style="font-weight: bold; color: #008000;" %)array(%%) 277 + 278 + #[RPC\Response(dto: UserDTO::class)] 279 + public function getUserInfoAsArray(int $id): array 543 543 { 544 - (% style="color: #19177C;" %)$user(%%)(% style="color:#666666;" %)=(%%) (% style="font-weight: bold; color: #008000;" %)this(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getUserInfo(%%)((% style="color: #19177C;" %)$id(%%));545 - (% style="font-weight: bold; color: #008000;" %)return(%%)[546 - (% style="color: #BA2121;" %)'id'(%%)(% style="color: #666666;" %)=>(%%)(% style="color: #19177C;" %)$user(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getId(%%)(),547 - (% style="color: #BA2121;" %)'email'(%%)(% style="color: #666666;" %)=>(%%)(% style="color: #19177C;" %)$user(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getEmail(%%)(),548 - (% style="color: #BA2121;" %)'name'(%%)(% style="color: #666666;" %)=>(%%)(% style="color: #19177C;" %)$user(% style="border: 1px solid #FF0000;" %)'(%%)s(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getName(%%)(),281 + $user = this->getUserInfo($id); 282 + return [ 283 + 'id' => $user->getId(), 284 + 'email' => $user->getEmail(), 285 + 'name' => $user's->getName(), 549 549 ]; 550 550 } 551 -\\ (% style="font-style: italic; color: #408080;" %)#[RPC\Response(dto: UserDTO::class, collection: true)] 552 -(%%) (% style="font-weight: bold; color: #008000;" %)public(%%) (% style="font-weight: bold; color: #008000;" %)function(%%) (% style="color: #0000FF;" %)getUsersList(%%)()(% style="color: #666666;" %):(%%) (% style="font-weight: bold; color: #008000;" %)array(%%) 288 + 289 + #[RPC\Response(dto: UserDTO::class, collection: true)] 290 + public function getUsersList(): array 553 553 { 554 - (% style="font-weight: bold; color: #008000;" %)return(%%)(% style="font-weight: bold; color: #008000;" %)this(% style="color: #666666;" %)->(% style="color: #7D9029;" %)userService(% style="color: #666666;" %)->(% style="color: #7D9029;" %)getAll(%%)();292 + return this->userService->getAll(); 555 555 } 556 -}\\ 294 +} 295 + 296 +{{/code}} 557 557 ))) 558 -))) 559 -))) 560 -))) 561 -))) 562 562 563 563 (% class="col-xs-12 col-sm-6" %) 564 564 ((( 565 -(% class="box" %) 301 +{{code language="json" layout="LINENUMBERS" title="(% class=~"box warningmessage~" %) 566 566 ((( 567 -(% class="box warningmessage" %) 568 -((( 569 569 To simplify the documentation, in the response example I will remove other elements except those intended to demonstrate 570 -))) 571 - 572 -(% class="code" %) 573 -((( 574 -(% class="linenoswrapper" %) 575 -((( 576 -(% class="linenos" %) 577 -((( 578 -1 579 -2 580 -3 581 -4 582 -5 583 -6 584 -7 585 -8 586 -9 587 -10 588 -11 589 -12 590 -13 591 -14 592 -15 593 -16 594 -17 595 -18 596 -19 597 -20 598 -21 599 -22 600 -23 601 -24 602 -25 603 -26 604 -27 605 -28 606 -29 607 -30 608 -31 609 -32 610 -33 611 -34 612 -35 613 -36\\ 614 -))) 615 - 616 -((( 304 +)))"}} 617 617 { 618 - (% style="font-weight: bold; color: #008000;" %)"methods"(%%): {619 - (% style="font-weight: bold; color: #008000;" %)"ExampleApi.getUserInfo"(%%): {620 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"ExampleApi.getUserInfo"(%%),621 - (% style="border: 1px solid #FF0000;" %)...,(%%)622 - (% style="font-weight: bold; color: #008000;" %)"returns"(%%):(% style="color: #BA2121;" %)"object"(%%),623 - (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%): {624 - (% style="font-weight: bold; color: #008000;"%)"id"(%%):(% style="color: #BA2121;" %)"int"(%%),625 - (% style="font-weight: bold; color: #008000;" %)"email"(%%):(% style="color: #BA2121;" %)"string"(%%),626 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"string"(%%)306 + "methods": { 307 + "ExampleApi.getUserInfo": { 308 + "name": "ExampleApi.getUserInfo", 309 + ... 310 + "returns": "object", 311 + "responseFormat": { 312 + "id": "int", 313 + "email": "string", 314 + "name": "string" 627 627 } 628 628 }, 629 - (% style="font-weight: bold; color: #008000;" %)"ExampleApi.getUserInfoAsArray"(%%): {630 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"ExampleApi.getUserInfoAsArray"(%%),631 - (% style="border: 1px solid #FF0000;" %)...,(%%)632 - (% style="font-weight: bold; color: #008000;" %)"returns"(%%):(% style="color: #BA2121;" %)"array"(%%),633 - (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%): {634 - (% style="font-weight: bold; color: #008000;"%)"id"(%%):(% style="color: #BA2121;" %)"int"(%%),635 - (% style="font-weight: bold; color: #008000;" %)"email"(%%):(% style="color: #BA2121;" %)"string"(%%),636 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"string"(%%)317 + "ExampleApi.getUserInfoAsArray": { 318 + "name": "ExampleApi.getUserInfoAsArray", 319 + 320 + "returns": "array", 321 + "responseFormat": { 322 + "id": "int", 323 + "email": "string", 324 + "name": "string" 637 637 } 638 638 }, 639 - (% style="font-weight: bold; color: #008000;" %)"ExampleApi.getUsersList"(%%): {640 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"ExampleApi.getUsersList"(%%),641 - (% style="border: 1px solid #FF0000;" %)...,(%%)642 - (% style="font-weight: bold; color: #008000;" %)"returns"(%%):(% style="color: #BA2121;" %)"array"(%%),643 - (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%): [327 + "ExampleApi.getUsersList": { 328 + "name": "ExampleApi.getUsersList", 329 + 330 + "returns": "array", 331 + "responseFormat": [ 644 644 { 645 - (% style="font-weight: bold; color: #008000;"%)"id"(%%):(% style="color: #BA2121;" %)"int"(%%),646 - (% style="font-weight: bold; color: #008000;" %)"email"(%%):(% style="color: #BA2121;" %)"string"(%%),647 - (% style="font-weight: bold; color: #008000;" %)"name"(%%):(% style="color: #BA2121;" %)"string"(%%)333 + "id": "int", 334 + "email": "string", 335 + "name": "string" 648 648 } 649 649 ] 650 650 } 651 651 } 652 652 } 653 -))) 654 -))) 655 -))) 656 -))) 341 +{{/code}} 657 657 658 658 (% class="box successmessage" %) 659 659 ((( ... ... @@ -661,3 +661,4 @@ 661 661 ))) 662 662 ))) 663 663 ))) 349 +