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,4 +1,5 @@ 1 -{{box cssClass="floatinginfobox"}} 1 +(% class="box floatinginfobox" %) 2 +((( 2 2 == (% style="display:block; margin-top:-30px; text-align:center" %)Summary(%%) == 3 3 4 4 |**Classname**|(% colspan="2" rowspan="1" %)((( ... ... @@ -20,13 +20,13 @@ 20 20 |(% colspan="1" rowspan="3" %)$collection|**type**|bool 21 21 |**optional**|true 22 22 |**default**|false 23 - {{/box}}24 +))) 24 24 25 25 = Enhanced Response Format = 26 26 27 -By default, the documentation for the response of each API service is based on the {{codelanguage="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.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. 28 28 29 -To provide clients with more information about the format of more complex responses, the attribute {{codelanguage="none"}}#[RPC\Response]{{/code}}was created.30 +To provide clients with more information about the format of more complex responses, the attribute (% class="box code" %)#[RPC\Response](%%) was created. 30 30 31 31 Let's consider an example that demonstrates the problem and its solution. You have a class containing API methods that return information about users: 32 32 ... ... @@ -34,96 +34,199 @@ 34 34 * getUserInfoAsArray(int $id) - returns information about a user in the form of an array 35 35 * getUsersList() - returns a collection of user objects 36 36 37 -{{code language="php" layout="LINENUMBERS"}} 38 -<?php 39 -namespace App\Api\Procedures; 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 +))) 40 40 41 -use App\Services\UserService; 42 -use Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService; 43 - 44 -class ExampleApi implements IRpcService 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 45 45 { 46 - public function __construct( 47 - protected UserService $userService 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(%%) 48 48 ) {} 49 - 50 - public function getUserInfo(int $id): User 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 51 51 { 52 - return this->userService->getById($id); 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(%%)); 53 53 } 54 - 55 - public function getUserInfoAsArray(int $id): array 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(%%) 56 56 { 57 - $user = this->getUserInfo($id); 58 - return [ 59 - 'id' => $user->getId(), 60 - 'email' => $user->getEmail(), 61 - 'name' => $user->getName(), 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(%%)(), 62 62 ]; 63 63 } 64 - 65 - public function getUsersList(): array 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(%%) 66 66 { 67 - return this->userService->getAll(); 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(%%)(); 68 68 } 69 -} 108 +}\\ 109 +))) 110 +))) 111 +))) 112 +))) 70 70 71 -{{/code}} 72 - 73 73 Let's look at the documentation generated by this instruction 74 74 75 -{{code language="json" layout="LINENUMBERS"}} 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 +((( 76 76 { 77 - "envelope": "JSON-RPC-2.0/UFO-RPC-6", 78 - "contentType": "application/json", 79 - "description": "", 80 - "transport": { 81 - "sync": { 82 - "scheme": "https", 83 - "host": "example.com", 84 - "path": "/api", 85 - "method": "POST" 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"(%%) 86 86 } 87 87 }, 88 - "methods": { 89 - "ExampleApi.getUserInfo": { 90 - "name": "ExampleApi.getUserInfo", 91 - "description": "", 92 - "parameters": { 93 - "id": { 94 - "type": "integer", 95 - "name": "id", 96 - "description": "", 97 - "optional": false 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(%%) 98 98 } 99 99 }, 100 - "returns": "object", 101 - "responseFormat": "object" 201 + (% style="font-weight: bold; color: #008000;" %)"returns"(%%): (% style="color: #BA2121;" %)"object"(%%), 202 + (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%): (% style="color: #BA2121;" %)"object"(%%) 102 102 }, 103 - "ExampleApi.getUserInfoAsArray": { 104 - "name": "ExampleApi.getUserInfoAsArray", 105 - "description": "", 106 - "parameters": { 107 - "id": { 108 - "type": "integer", 109 - "name": "id", 110 - "description": "", 111 - "optional": false 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(%%) 112 112 } 113 113 }, 114 - "returns": "array", 115 - "responseFormat": "array" 215 + (% style="font-weight: bold; color: #008000;" %)"returns"(%%): (% style="color: #BA2121;" %)"array"(%%), 216 + (% style="font-weight: bold; color: #008000;" %)"responseFormat"(%%): (% style="color: #BA2121;" %)"array"(%%) 116 116 }, 117 - "ExampleApi.getUsersList": { 118 - "name": "ExampleApi.getUsersList", 119 - "description": "", 120 - "parameters": [], 121 - "returns": "array", 122 - "responseFormat": "array" 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"(%%) 123 123 } 124 124 } 125 125 } 126 -{{/code}} 227 +))) 228 +))) 229 +))) 230 +))) 127 127 128 128 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. 129 129 ... ... @@ -139,98 +139,193 @@ 139 139 ((( 140 140 (% class="col-xs-12 col-sm-6" %) 141 141 ((( 142 -{{code language="php" layout="LINENUMBERS"}} 143 -<?php 144 -namespace App\Api\Procedures; 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 +))) 145 145 146 -use App\Services\UserService; 147 -use Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService; 148 -use Ufo\RpcObject\RPC; 149 - 150 -class ExampleApi implements IRpcService 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 151 151 { 152 - public function __construct( 153 - protected UserService $userService 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(%%) 154 154 ) {} 155 - 156 - #[RPC\Response(['id' => 'int', 'email' => 'string', 'name' => 'string'])] 157 - public function getUserInfo(int $id): User 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 158 158 { 159 - return this->userService->getById($id); 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 160 } 161 - 162 - #[RPC\Response(['id' => 'int', 'email' => 'string', 'name' => 'string'])] 163 - public function getUserInfoAsArray(int $id): array 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(%%) 164 164 { 165 - $user = this->getUserInfo($id); 166 - return [ 167 - 'id' => $user->getId(), 168 - 'email' => $user->getEmail(), 169 - 'name' => $user's->getName(), 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(), 170 170 ]; 171 171 } 172 - 173 - #[RPC\Response([['id' => 'int', 'email' => 'string', 'name' => 'string'])]] 174 - public function getUsersList(): array 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(%%) 175 175 { 176 - return this->userService->getAll(); 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 177 } 178 -} 324 +}\\ 325 +))) 326 +))) 327 +))) 328 +))) 179 179 180 -{{/code}} 181 - 182 182 Note the changed documentation format on the right, responseFormat now has detail and provides a clear idea of the response structure. 183 183 ))) 184 184 185 185 (% class="col-xs-12 col-sm-6" %) 186 186 ((( 187 - {{code language="json" layout="LINENUMBERS" title="(% class=~"boxwarningmessage~" %)335 +(% class="box" %) 188 188 ((( 337 +(% class="box warningmessage" %) 338 +((( 189 189 To simplify the documentation, in the response example I will remove other elements except those intended to demonstrate 190 -)))"}} 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 191 { 192 - "methods": { 193 - "ExampleApi.getUserInfo": { 194 - "name": "ExampleApi.getUserInfo", 195 - ..., 196 - "returns": "object", 197 - "responseFormat": { 198 - "id": "int", 199 - "email": "string", 200 - "name": "string" 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"(%%) 201 201 } 202 202 }, 203 - "ExampleApi.getUserInfoAsArray": { 204 - "name": "ExampleApi.getUserInfoAsArray", 205 - ..., 206 - "returns": "array", 207 - "responseFormat": { 208 - "id": "int", 209 - "email": "string", 210 - "name": "string" 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"(%%) 211 211 } 212 212 }, 213 - "ExampleApi.getUsersList": { 214 - "name": "ExampleApi.getUsersList", 215 - ..., 216 - "returns": "array", 217 - "responseFormat": [ 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"(%%): [ 218 218 { 219 - "id": "int", 220 - "email": "string", 221 - "name": "string" 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"(%%) 222 222 } 223 223 ] 224 224 } 225 225 } 226 226 } 227 -{{/code}} 423 +))) 424 +))) 425 +))) 426 +))) 228 228 229 229 230 230 ))) 231 231 ))) 232 232 233 -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. 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 234 235 235 == **Option 2 - (recommended)** == 236 236 ... ... @@ -241,103 +241,220 @@ 241 241 ((( 242 242 (% class="col-xs-12 col-sm-6" %) 243 243 ((( 244 -{{code language="php" layout="LINENUMBERS"}} 245 -<?php 246 -namespace App\Api\DTO; 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 +))) 247 247 248 -readonly class UserDTO 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 249 { 250 - public int $id; 251 - public string $email; 252 - public string $name; 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(%%); 253 253 } 254 -{{/code}} 471 +))) 472 +))) 473 +))) 474 +))) 255 255 256 -{{code language="php" layout="LINENUMBERS"}} 257 -<?php 258 -namespace App\Api\Procedures; 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 +))) 259 259 260 -use App\Services\UserService; 261 -use Ufo\JsonRpcBundle.ApiMethod\Interfaces\IRpcService; 262 -use Ufo\RpcObject\RPC; 263 -use App\Api\DTO\UserDTO; 264 - 265 -class ExampleApi implements IRpcService 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 266 266 { 267 - public function __construct( 268 - protected UserService $userService 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(%%) 269 269 ) {} 270 - 271 - #[RPC\Response(dto: UserDTO::class)] 272 - public function getUserInfo(int $id): User 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 273 273 { 274 - return this->userService->getById($id); 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 275 } 276 - 277 - #[RPC\Response(dto: UserDTO::class)] 278 - public function getUserInfoAsArray(int $id): array 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(%%) 279 279 { 280 - $user = this->getUserInfo($id); 281 - return [ 282 - 'id' => $user->getId(), 283 - 'email' => $user->getEmail(), 284 - 'name' => $user's->getName(), 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(%%)(), 285 285 ]; 286 286 } 287 - 288 - #[RPC\Response(dto: UserDTO::class, collection: true)] 289 - public function getUsersList(): array 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(%%) 290 290 { 291 - return this->userService->getAll(); 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 292 } 293 -} 294 - 295 -{{/code}} 556 +}\\ 296 296 ))) 558 +))) 559 +))) 560 +))) 561 +))) 297 297 298 298 (% class="col-xs-12 col-sm-6" %) 299 299 ((( 300 - {{code language="json" layout="LINENUMBERS" title="(% class=~"boxwarningmessage~" %)565 +(% class="box" %) 301 301 ((( 567 +(% class="box warningmessage" %) 568 +((( 302 302 To simplify the documentation, in the response example I will remove other elements except those intended to demonstrate 303 -)))"}} 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 304 { 305 - "methods": { 306 - "ExampleApi.getUserInfo": { 307 - "name": "ExampleApi.getUserInfo", 308 - ..., 309 - "returns": "object", 310 - "responseFormat": { 311 - "id": "int", 312 - "email": "string", 313 - "name": "string" 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"(%%) 314 314 } 315 315 }, 316 - "ExampleApi.getUserInfoAsArray": { 317 - "name": "ExampleApi.getUserInfoAsArray", 318 - ..., 319 - "returns": "array", 320 - "responseFormat": { 321 - "id": "int", 322 - "email": "string", 323 - "name": "string" 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"(%%) 324 324 } 325 325 }, 326 - "ExampleApi.getUsersList": { 327 - "name": "ExampleApi.getUsersList", 328 - ..., 329 - "returns": "array", 330 - "responseFormat": [ 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"(%%): [ 331 331 { 332 - "id": "int", 333 - "email": "string", 334 - "name": "string" 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"(%%) 335 335 } 336 336 ] 337 337 } 338 338 } 339 339 } 340 -{{/code}} 653 +))) 654 +))) 655 +))) 656 +))) 341 341 342 342 (% class="box successmessage" %) 343 343 (((