Wiki source code of #[RPC\Response]
Last modified by Ashterix on 2024/05/19 21:13
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | (% class="box floatinginfobox" %) | ||
2 | ((( | ||
3 | == (% style="display:block; margin-top:-30px; text-align:center" %)Summary(%%) == | ||
4 | |||
5 | |**Classname**|(% colspan="2" rowspan="1" %)((( | ||
6 | Response | ||
7 | ))) | ||
8 | |**Namespace**|(% colspan="2" %)((( | ||
9 | Ufo\RpcObject\RPC | ||
10 | ))) | ||
11 | |**Target**|(% colspan="2" rowspan="1" %)method | ||
12 | |(% colspan="3" %)**Arguments:** | ||
13 | |(% colspan="1" rowspan="3" %)((( | ||
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 | ))) | ||
25 | |||
26 | = Enhanced Response Format = | ||
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. | ||
29 | |||
30 | To provide clients with more information about the format of more complex responses, the attribute (% class="box code" %)#[RPC\Response](%%) was created. | ||
31 | |||
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 | |||
34 | * getUserInfo(int $id) - returns a user object containing keys id, email, name | ||
35 | * getUserInfoAsArray(int $id) - returns information about a user in the form of an array | ||
36 | * getUsersList() - returns a collection of user objects | ||
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 | ))) | ||
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 | ||
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(%%) | ||
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 | ||
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(%%)); | ||
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(%%) | ||
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(%%)(), | ||
102 | ]; | ||
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(%%) | ||
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(%%)(); | ||
107 | } | ||
108 | }\\ | ||
109 | ))) | ||
110 | ))) | ||
111 | ))) | ||
112 | ))) | ||
113 | |||
114 | Let's look at the documentation generated by this instruction | ||
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 | ((( | ||
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(%%) | ||
199 | } | ||
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"(%%) | ||
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(%%) | ||
213 | } | ||
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"(%%) | ||
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"(%%) | ||
224 | } | ||
225 | } | ||
226 | } | ||
227 | ))) | ||
228 | ))) | ||
229 | ))) | ||
230 | ))) | ||
231 | |||
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 | |||
234 | It's time to add the Response attribute. There are variations in its use. | ||
235 | |||
236 | == **Option 1 - (one-time) suitable for methods that return a unique set of parameters.** == | ||
237 | |||
238 | Passing the $responseFormat parameter, in which as a key=>value array you need to enumerate all the parameters that the response object will have. | ||
239 | |||
240 | The key is the name of the parameter, and the value is the type of data. | ||
241 | |||
242 | (% class="row" %) | ||
243 | ((( | ||
244 | (% class="col-xs-12 col-sm-6" %) | ||
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 | ))) | ||
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 | ||
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(%%) | ||
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 | ||
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(%%)); | ||
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(%%) | ||
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(), | ||
317 | ]; | ||
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(%%) | ||
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(%%)(); | ||
323 | } | ||
324 | }\\ | ||
325 | ))) | ||
326 | ))) | ||
327 | ))) | ||
328 | ))) | ||
329 | |||
330 | Note the changed documentation format on the right, responseFormat now has detail and provides a clear idea of the response structure. | ||
331 | ))) | ||
332 | |||
333 | (% class="col-xs-12 col-sm-6" %) | ||
334 | ((( | ||
335 | (% class="box" %) | ||
336 | ((( | ||
337 | (% class="box warningmessage" %) | ||
338 | ((( | ||
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 | ((( | ||
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"(%%) | ||
397 | } | ||
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"(%%) | ||
407 | } | ||
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"(%%): [ | ||
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"(%%) | ||
418 | } | ||
419 | ] | ||
420 | } | ||
421 | } | ||
422 | } | ||
423 | ))) | ||
424 | ))) | ||
425 | ))) | ||
426 | ))) | ||
427 | |||
428 | |||
429 | ))) | ||
430 | ))) | ||
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. | ||
433 | |||
434 | == **Option 2 - (recommended)** == | ||
435 | |||
436 | This option assumes that you need to create classes that will act as DTOs (Data Transfer Object), it should be a class that has the same properties as the object returned by your API method. | ||
437 | |||
438 | |||
439 | (% class="row" %) | ||
440 | ((( | ||
441 | (% class="col-xs-12 col-sm-6" %) | ||
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 | ))) | ||
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(%%) | ||
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(%%); | ||
470 | } | ||
471 | ))) | ||
472 | ))) | ||
473 | ))) | ||
474 | ))) | ||
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 | ))) | ||
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 | ||
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(%%) | ||
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 | ||
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(%%)); | ||
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(%%) | ||
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(%%)(), | ||
549 | ]; | ||
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(%%) | ||
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(%%)(); | ||
555 | } | ||
556 | }\\ | ||
557 | ))) | ||
558 | ))) | ||
559 | ))) | ||
560 | ))) | ||
561 | ))) | ||
562 | |||
563 | (% class="col-xs-12 col-sm-6" %) | ||
564 | ((( | ||
565 | (% class="box" %) | ||
566 | ((( | ||
567 | (% class="box warningmessage" %) | ||
568 | ((( | ||
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 | ((( | ||
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"(%%) | ||
627 | } | ||
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"(%%) | ||
637 | } | ||
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"(%%): [ | ||
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"(%%) | ||
648 | } | ||
649 | ] | ||
650 | } | ||
651 | } | ||
652 | } | ||
653 | ))) | ||
654 | ))) | ||
655 | ))) | ||
656 | ))) | ||
657 | |||
658 | (% class="box successmessage" %) | ||
659 | ((( | ||
660 | The response format has not changed, but maintaining and expanding your API methods has become much easier and more comfortable | ||
661 | ))) | ||
662 | ))) | ||
663 | ))) |