1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2020-2025 |
6
|
|
|
* @package Client |
7
|
|
|
* @subpackage JsonApi |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Client\JsonApi\Customer\Review; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* JSON API customer/review client |
19
|
|
|
* |
20
|
|
|
* @package Client |
21
|
|
|
* @subpackage JsonApi |
22
|
|
|
*/ |
23
|
|
|
class Standard |
24
|
|
|
extends \Aimeos\Client\JsonApi\Base |
25
|
|
|
implements \Aimeos\Client\JsonApi\Iface |
26
|
|
|
{ |
27
|
|
|
/** client/jsonapi/customer/review/name |
28
|
|
|
* Class name of the used customer/review client implementation |
29
|
|
|
* |
30
|
|
|
* Each default JSON API client can be replace by an alternative imlementation. |
31
|
|
|
* To use this implementation, you have to set the last part of the class |
32
|
|
|
* name as configuration value so the client factory knows which class it |
33
|
|
|
* has to instantiate. |
34
|
|
|
* |
35
|
|
|
* For example, if the name of the default class is |
36
|
|
|
* |
37
|
|
|
* \Aimeos\Client\JsonApi\Customer\Review\Standard |
38
|
|
|
* |
39
|
|
|
* and you want to replace it with your own version named |
40
|
|
|
* |
41
|
|
|
* \Aimeos\Client\JsonApi\Customer\Review\Mycustomer/review |
42
|
|
|
* |
43
|
|
|
* then you have to set the this configuration option: |
44
|
|
|
* |
45
|
|
|
* client/jsonapi/customer/review/name = Mycustomer/review |
46
|
|
|
* |
47
|
|
|
* The value is the last part of your own class name and it's case sensitive, |
48
|
|
|
* so take care that the configuration value is exactly named like the last |
49
|
|
|
* part of the class name. |
50
|
|
|
* |
51
|
|
|
* The allowed characters of the class name are A-Z, a-z and 0-9. No other |
52
|
|
|
* characters are possible! You should always start the last part of the class |
53
|
|
|
* name with an upper case character and continue only with lower case characters |
54
|
|
|
* or numbers. Avoid chamel case names like "MyReview"! |
55
|
|
|
* |
56
|
|
|
* @param string Last part of the class name |
57
|
|
|
* @since 2017.03 |
58
|
|
|
* @category Developer |
59
|
|
|
*/ |
60
|
|
|
|
61
|
|
|
/** client/jsonapi/customer/review/decorators/excludes |
62
|
|
|
* Excludes decorators added by the "common" option from the JSON API clients |
63
|
|
|
* |
64
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
65
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
66
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
67
|
|
|
* modify what is returned to the caller. |
68
|
|
|
* |
69
|
|
|
* This option allows you to remove a decorator added via |
70
|
|
|
* "client/jsonapi/common/decorators/default" before they are wrapped |
71
|
|
|
* around the JsonApi client. |
72
|
|
|
* |
73
|
|
|
* client/jsonapi/decorators/excludes = array( 'decorator1' ) |
74
|
|
|
* |
75
|
|
|
* This would remove the decorator named "decorator1" from the list of |
76
|
|
|
* common decorators ("\Aimeos\Client\JsonApi\Common\Decorator\*") added via |
77
|
|
|
* "client/jsonapi/common/decorators/default" for the JSON API client. |
78
|
|
|
* |
79
|
|
|
* @param array List of decorator names |
80
|
|
|
* @since 2017.07 |
81
|
|
|
* @category Developer |
82
|
|
|
* @see client/jsonapi/common/decorators/default |
83
|
|
|
* @see client/jsonapi/customer/review/decorators/global |
84
|
|
|
* @see client/jsonapi/customer/review/decorators/local |
85
|
|
|
*/ |
86
|
|
|
|
87
|
|
|
/** client/jsonapi/customer/review/decorators/global |
88
|
|
|
* Adds a list of globally available decorators only to the JsonApi client |
89
|
|
|
* |
90
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
91
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
92
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
93
|
|
|
* modify what is returned to the caller. |
94
|
|
|
* |
95
|
|
|
* This option allows you to wrap global decorators |
96
|
|
|
* ("\Aimeos\Client\JsonApi\Common\Decorator\*") around the JsonApi |
97
|
|
|
* client. |
98
|
|
|
* |
99
|
|
|
* client/jsonapi/customer/review/decorators/global = array( 'decorator1' ) |
100
|
|
|
* |
101
|
|
|
* This would add the decorator named "decorator1" defined by |
102
|
|
|
* "\Aimeos\Client\JsonApi\Common\Decorator\Decorator1" only to the |
103
|
|
|
* "customer" JsonApi client. |
104
|
|
|
* |
105
|
|
|
* @param array List of decorator names |
106
|
|
|
* @since 2017.07 |
107
|
|
|
* @category Developer |
108
|
|
|
* @see client/jsonapi/common/decorators/default |
109
|
|
|
* @see client/jsonapi/customer/review/decorators/excludes |
110
|
|
|
* @see client/jsonapi/customer/review/decorators/local |
111
|
|
|
*/ |
112
|
|
|
|
113
|
|
|
/** client/jsonapi/customer/review/decorators/local |
114
|
|
|
* Adds a list of local decorators only to the JsonApi client |
115
|
|
|
* |
116
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
117
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
118
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
119
|
|
|
* modify what is returned to the caller. |
120
|
|
|
* |
121
|
|
|
* This option allows you to wrap local decorators |
122
|
|
|
* ("\Aimeos\Client\JsonApi\Customer\Review\Decorator\*") around the JsonApi |
123
|
|
|
* client. |
124
|
|
|
* |
125
|
|
|
* client/jsonapi/customer/review/decorators/local = array( 'decorator2' ) |
126
|
|
|
* |
127
|
|
|
* This would add the decorator named "decorator2" defined by |
128
|
|
|
* "\Aimeos\Client\JsonApi\Customer\Review\Decorator\Decorator2" only to the |
129
|
|
|
* "customer review" JsonApi client. |
130
|
|
|
* |
131
|
|
|
* @param array List of decorator names |
132
|
|
|
* @since 2017.07 |
133
|
|
|
* @category Developer |
134
|
|
|
* @see client/jsonapi/common/decorators/default |
135
|
|
|
* @see client/jsonapi/customer/review/decorators/excludes |
136
|
|
|
* @see client/jsonapi/customer/review/decorators/global |
137
|
|
|
*/ |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Deletes the resource or the resource list |
142
|
|
|
* |
143
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
144
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
145
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
146
|
|
|
*/ |
147
|
|
|
public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
148
|
|
|
{ |
149
|
|
|
$view = $this->view(); |
150
|
|
|
$context = $this->context(); |
151
|
|
|
|
152
|
|
|
try |
153
|
|
|
{ |
154
|
|
|
$body = (string) $request->getBody(); |
155
|
|
|
$cntl = \Aimeos\Controller\Frontend::create( $context, 'review' ); |
156
|
|
|
|
157
|
|
|
if( ( $relId = $view->param( 'relatedid' ) ) === null ) |
158
|
|
|
{ |
159
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
160
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if( !is_array( $payload->data ) ) { |
164
|
|
|
$payload->data = [$payload->data]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$cntl->delete( array_column( $payload->data, 'id' ) ); |
168
|
|
|
} |
169
|
|
|
else |
170
|
|
|
{ |
171
|
|
|
$cntl->delete( $relId ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$status = 200; |
175
|
|
|
} |
176
|
|
|
catch( \Aimeos\Controller\Frontend\Review\Exception $e ) |
177
|
|
|
{ |
178
|
|
|
$status = 403; |
179
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
180
|
|
|
} |
181
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
182
|
|
|
{ |
183
|
|
|
$status = 404; |
184
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
185
|
|
|
} |
186
|
|
|
catch( \Exception $e ) |
187
|
|
|
{ |
188
|
|
|
$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
189
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this->render( $response, $view, $status ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns the resource or the resource list |
198
|
|
|
* |
199
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
200
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
201
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
202
|
|
|
*/ |
203
|
|
|
public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
204
|
|
|
{ |
205
|
|
|
$view = $this->view(); |
206
|
|
|
$context = $this->context(); |
207
|
|
|
|
208
|
|
|
try |
209
|
|
|
{ |
210
|
|
|
$cntl = \Aimeos\Controller\Frontend::create( $context, 'review' ); |
211
|
|
|
|
212
|
|
|
if( ( $relId = $view->param( 'relatedid' ) ) == null ) |
213
|
|
|
{ |
214
|
|
|
$total = 0; |
215
|
|
|
$view->items = $cntl->parse( $view->param( 'filter', [] ) )->list( $total ); |
216
|
|
|
$view->total = $total; |
217
|
|
|
} |
218
|
|
|
else |
219
|
|
|
{ |
220
|
|
|
$view->items = $cntl->get( $relId ); |
221
|
|
|
$view->total = 1; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$status = 200; |
225
|
|
|
} |
226
|
|
|
catch( \Aimeos\Controller\Frontend\Review\Exception $e ) |
227
|
|
|
{ |
228
|
|
|
$status = 403; |
229
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
230
|
|
|
} |
231
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
232
|
|
|
{ |
233
|
|
|
$status = 404; |
234
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
235
|
|
|
} |
236
|
|
|
catch( \Exception $e ) |
237
|
|
|
{ |
238
|
|
|
$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
239
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $this->render( $response, $view, $status ); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Updates the resource or the resource list partitially |
248
|
|
|
* |
249
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
250
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
251
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
252
|
|
|
*/ |
253
|
|
|
public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
254
|
|
|
{ |
255
|
|
|
$view = $this->view(); |
256
|
|
|
$context = $this->context(); |
257
|
|
|
|
258
|
|
|
try |
259
|
|
|
{ |
260
|
|
|
$body = (string) $request->getBody(); |
261
|
|
|
|
262
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
263
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
if( ( $id = $view->param( 'relatedid' ) ) === null ) { |
267
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( 'Required parameter "relatedid" is missing', 400 ); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$cntl = \Aimeos\Controller\Frontend::create( $context, 'review' ); |
271
|
|
|
$item = $cntl->create( (array) $payload->data->attributes )->setId( $id ); |
272
|
|
|
|
273
|
|
|
$view->items = $cntl->save( $item ); |
274
|
|
|
$view->total = 1; |
275
|
|
|
$status = 200; |
276
|
|
|
} |
277
|
|
|
catch( \Aimeos\Controller\Frontend\Review\Exception $e ) |
278
|
|
|
{ |
279
|
|
|
$status = 403; |
280
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
281
|
|
|
} |
282
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
283
|
|
|
{ |
284
|
|
|
$status = 404; |
285
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
286
|
|
|
} |
287
|
|
|
catch( \Exception $e ) |
288
|
|
|
{ |
289
|
|
|
$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
290
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $this->render( $response, $view, $status ); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Creates or updates the resource or the resource list |
299
|
|
|
* |
300
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
301
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
302
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
303
|
|
|
*/ |
304
|
|
|
public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
305
|
|
|
{ |
306
|
|
|
$view = $this->view(); |
307
|
|
|
$context = $this->context(); |
308
|
|
|
|
309
|
|
|
try |
310
|
|
|
{ |
311
|
|
|
$body = (string) $request->getBody(); |
312
|
|
|
|
313
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
314
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 ); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
if( !is_array( $payload->data ) ) { |
318
|
|
|
$payload->data = [$payload->data]; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$items = []; |
322
|
|
|
$cntl = \Aimeos\Controller\Frontend::create( $context, 'review' ); |
323
|
|
|
|
324
|
|
|
foreach( $payload->data as $entry ) |
325
|
|
|
{ |
326
|
|
|
if( !isset( $entry->attributes ) ) { |
327
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( 'Attributes are missing', 400 ); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$items[] = $cntl->save( $cntl->create( (array) $entry->attributes ) ); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$view->total = count( $items ); |
334
|
|
|
$view->items = map( $items ); |
335
|
|
|
$status = 201; |
336
|
|
|
} |
337
|
|
|
catch( \Aimeos\Controller\Frontend\Review\Exception $e ) |
338
|
|
|
{ |
339
|
|
|
$status = 403; |
340
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
341
|
|
|
} |
342
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
343
|
|
|
{ |
344
|
|
|
$status = 404; |
345
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
346
|
|
|
} |
347
|
|
|
catch( \Exception $e ) |
348
|
|
|
{ |
349
|
|
|
$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
350
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return $this->render( $response, $view, $status ); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Returns the available REST verbs and the available parameters |
359
|
|
|
* |
360
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
361
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
362
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
363
|
|
|
*/ |
364
|
|
|
public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
365
|
|
|
{ |
366
|
|
|
$view = $this->view(); |
367
|
|
|
|
368
|
|
|
$view->attributes = [ |
369
|
|
|
'review.domain' => [ |
370
|
|
|
'label' => 'Domain of the reviewed item', |
371
|
|
|
'type' => 'string', 'default' => '', 'required' => true, |
372
|
|
|
], |
373
|
|
|
'review.refid' => [ |
374
|
|
|
'label' => 'ID of the reviewd item', |
375
|
|
|
'type' => 'string', 'default' => '', 'required' => true, |
376
|
|
|
], |
377
|
|
|
'review.rating' => [ |
378
|
|
|
'label' => 'Rating from 0 to 5', |
379
|
|
|
'type' => 'string', 'default' => '', 'required' => true, |
380
|
|
|
], |
381
|
|
|
'review.name' => [ |
382
|
|
|
'label' => 'Name of the reviewer', |
383
|
|
|
'type' => 'string', 'default' => '', 'required' => false, |
384
|
|
|
], |
385
|
|
|
'review.comment' => [ |
386
|
|
|
'label' => 'Comment for the rating', |
387
|
|
|
'type' => 'string', 'default' => '', 'required' => false, |
388
|
|
|
], |
389
|
|
|
]; |
390
|
|
|
|
391
|
|
|
$tplconf = 'client/jsonapi/template-options'; |
392
|
|
|
$default = 'options-standard'; |
393
|
|
|
|
394
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
395
|
|
|
|
396
|
|
|
return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
397
|
|
|
->withHeader( 'Cache-Control', 'max-age=300' ) |
398
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
399
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
400
|
|
|
->withStatus( 200 ); |
|
|
|
|
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Returns the response object with the rendered header and body |
406
|
|
|
* |
407
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
408
|
|
|
* @param \Aimeos\Base\View\Iface $view View instance |
409
|
|
|
* @param int $status HTTP status code |
410
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
411
|
|
|
*/ |
412
|
|
|
protected function render( ResponseInterface $response, \Aimeos\Base\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface |
413
|
|
|
{ |
414
|
|
|
/** client/jsonapi/customer/review/template |
415
|
|
|
* Relative path to the customer review JSON API template |
416
|
|
|
* |
417
|
|
|
* The template file contains the code and processing instructions |
418
|
|
|
* to generate the result shown in the JSON API body. The |
419
|
|
|
* configuration string is the path to the template file relative |
420
|
|
|
* to the templates directory (usually in templates/client/jsonapi). |
421
|
|
|
* |
422
|
|
|
* You can overwrite the template file configuration in extensions and |
423
|
|
|
* provide alternative templates. These alternative templates should be |
424
|
|
|
* named like the default one but with the string "standard" replaced by |
425
|
|
|
* an unique name. You may use the name of your project for this. If |
426
|
|
|
* you've implemented an alternative client class as well, "standard" |
427
|
|
|
* should be replaced by the name of the new class. |
428
|
|
|
* |
429
|
|
|
* @param string Relative path to the template creating the body for the JSON API |
430
|
|
|
* @since 2017.07 |
431
|
|
|
* @category Developer |
432
|
|
|
*/ |
433
|
|
|
$tplconf = 'client/jsonapi/customer/review/template'; |
434
|
|
|
$default = 'customer/review/standard'; |
435
|
|
|
|
436
|
|
|
$view->customerid = $this->context()->user(); |
437
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
438
|
|
|
|
439
|
|
|
return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
440
|
|
|
->withHeader( 'Cache-Control', 'no-cache, private' ) |
441
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
442
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
443
|
|
|
->withStatus( $status ); |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|