1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
6
|
|
|
* @package Client |
7
|
|
|
* @subpackage JsonApi |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Client\JsonApi; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* JSON API common client |
19
|
|
|
* |
20
|
|
|
* @package Client |
21
|
|
|
* @subpackage JsonApi |
22
|
|
|
*/ |
23
|
|
|
abstract class Base |
24
|
|
|
{ |
25
|
|
|
private $view; |
26
|
|
|
private $context; |
27
|
|
|
private $templatePaths; |
28
|
|
|
private $path; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initializes the client |
33
|
|
|
* |
34
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context MShop context object |
35
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
36
|
|
|
* @param array $templatePaths List of file system paths where the templates are stored |
37
|
|
|
* @param string $path Name of the client separated by slashes, e.g "catalog/lists" |
38
|
|
|
*/ |
39
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MW\View\Iface $view, array $templatePaths, $path ) |
40
|
|
|
{ |
41
|
|
|
$this->view = $view; |
42
|
|
|
$this->context = $context; |
43
|
|
|
$this->templatePaths = $templatePaths; |
44
|
|
|
$this->path = $path; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Catch unknown methods |
50
|
|
|
* |
51
|
|
|
* @param string $name Name of the method |
52
|
|
|
* @param array $param List of method parameter |
53
|
|
|
* @throws \Aimeos\Client\JsonApi\Exception If method call failed |
54
|
|
|
*/ |
55
|
|
|
public function __call( $name, array $param ) |
56
|
|
|
{ |
57
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Unable to call method "%1$s"', $name ) ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Deletes the resource or the resource list |
63
|
|
|
* |
64
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
65
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
66
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
67
|
|
|
*/ |
68
|
|
|
public function delete( ServerRequestInterface $request, ResponseInterface $response ) |
69
|
|
|
{ |
70
|
|
|
return $this->defaultAction( $request, $response ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Retrieves the resource or the resource list |
76
|
|
|
* |
77
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
78
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
79
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
80
|
|
|
*/ |
81
|
|
|
public function get( ServerRequestInterface $request, ResponseInterface $response ) |
82
|
|
|
{ |
83
|
|
|
return $this->defaultAction( $request, $response ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Updates the resource or the resource list partitially |
89
|
|
|
* |
90
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
91
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
92
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
93
|
|
|
*/ |
94
|
|
|
public function patch( ServerRequestInterface $request, ResponseInterface $response ) |
95
|
|
|
{ |
96
|
|
|
return $this->defaultAction( $request, $response ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Creates or updates the resource or the resource list |
102
|
|
|
* |
103
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
104
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
105
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
106
|
|
|
*/ |
107
|
|
|
public function post( ServerRequestInterface $request, ResponseInterface $response ) |
108
|
|
|
{ |
109
|
|
|
return $this->defaultAction( $request, $response ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Creates or updates the resource or the resource list |
115
|
|
|
* |
116
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
117
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
118
|
|
|
* @param string|null $prefix Form parameter prefix when nesting parameters is required |
|
|
|
|
119
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
120
|
|
|
*/ |
121
|
|
|
public function put( ServerRequestInterface $request, ResponseInterface $response ) |
122
|
|
|
{ |
123
|
|
|
return $this->defaultAction( $request, $response ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Creates or updates the resource or the resource list |
129
|
|
|
* |
130
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
131
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
132
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
133
|
|
|
*/ |
134
|
|
|
public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null ) |
135
|
|
|
{ |
136
|
|
|
return $this->defaultAction( $request, $response ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns the default response for the resource |
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
|
|
|
protected function defaultAction( ServerRequestInterface $request, ResponseInterface $response ) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$status = 403; |
150
|
|
|
$view = $this->getView(); |
151
|
|
|
|
152
|
|
|
$view->errors = array( array( |
153
|
|
|
'title' => $this->getContext()->getI18n()->dt( 'client/jsonapi', 'Not allowed for this resource' ), |
154
|
|
|
) ); |
155
|
|
|
|
156
|
|
|
/** client/jsonapi/standard/template-error |
157
|
|
|
* Relative path to the default JSON API template |
158
|
|
|
* |
159
|
|
|
* The template file contains the code and processing instructions |
160
|
|
|
* to generate the result shown in the JSON API body. The |
161
|
|
|
* configuration string is the path to the template file relative |
162
|
|
|
* to the templates directory (usually in client/jsonapi/templates). |
163
|
|
|
* |
164
|
|
|
* You can overwrite the template file configuration in extensions and |
165
|
|
|
* provide alternative templates. These alternative templates should be |
166
|
|
|
* named like the default one but with the string "standard" replaced by |
167
|
|
|
* an unique name. You may use the name of your project for this. If |
168
|
|
|
* you've implemented an alternative client class as well, "standard" |
169
|
|
|
* should be replaced by the name of the new class. |
170
|
|
|
* |
171
|
|
|
* @param string Relative path to the template creating the body for the JSON API response |
172
|
|
|
* @since 2017.02 |
173
|
|
|
* @category Developer |
174
|
|
|
* @see client/jsonapi/standard/template-delete |
175
|
|
|
* @see client/jsonapi/standard/template-patch |
176
|
|
|
* @see client/jsonapi/standard/template-post |
177
|
|
|
* @see client/jsonapi/standard/template-get |
178
|
|
|
* @see client/jsonapi/standard/template-options |
179
|
|
|
*/ |
180
|
|
|
$tplconf = 'client/jsonapi/standard/template-error'; |
181
|
|
|
$default = 'error-standard.php'; |
182
|
|
|
|
183
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
184
|
|
|
|
185
|
|
|
return $response->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
186
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
187
|
|
|
->withStatus( $status ); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Returns the context item object |
193
|
|
|
* |
194
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Context object |
195
|
|
|
*/ |
196
|
|
|
protected function getContext() |
197
|
|
|
{ |
198
|
|
|
return $this->context; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns the translated title and the details of the error |
204
|
|
|
* |
205
|
|
|
* @param \Exception $e Thrown exception |
206
|
|
|
* @param string|null $domain Translation domain |
207
|
|
|
* @return array Associative list with "title" and "detail" key (if debug config is enabled) |
208
|
|
|
*/ |
209
|
|
|
protected function getErrorDetails( \Exception $e, $domain = null ) |
210
|
|
|
{ |
211
|
|
|
$details = []; |
212
|
|
|
|
213
|
|
|
if( $domain !== null ) { |
214
|
|
|
$details['title'] = $this->context->getI18n()->dt( $domain, $e->getMessage() ); |
215
|
|
|
} else { |
216
|
|
|
$details['title'] = $e->getMessage(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if( $this->context->getConfig()->get( 'client/jsonapi/debug', false ) == true ) { |
220
|
|
|
$details['detail'] = $e->getTraceAsString(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $details; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns the path to the client |
229
|
|
|
* |
230
|
|
|
* @return string Client path, e.g. "product/property" |
231
|
|
|
*/ |
232
|
|
|
protected function getPath() |
233
|
|
|
{ |
234
|
|
|
return $this->path; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns the paths to the template files |
240
|
|
|
* |
241
|
|
|
* @return array List of file system paths |
242
|
|
|
*/ |
243
|
|
|
protected function getTemplatePaths() |
244
|
|
|
{ |
245
|
|
|
return $this->templatePaths; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Returns the view object |
251
|
|
|
* |
252
|
|
|
* @return \Aimeos\MW\View\Iface View object |
253
|
|
|
*/ |
254
|
|
|
protected function getView() |
255
|
|
|
{ |
256
|
|
|
return $this->view; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Initializes the criteria object based on the given parameter |
262
|
|
|
* |
263
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
264
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
265
|
|
|
* @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
266
|
|
|
*/ |
267
|
|
|
protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
268
|
|
|
{ |
269
|
|
|
$this->initCriteriaConditions( $criteria, $params ); |
270
|
|
|
$this->initCriteriaSortations( $criteria, $params ); |
271
|
|
|
$this->initCriteriaSlice( $criteria, $params ); |
272
|
|
|
|
273
|
|
|
return $criteria; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Initializes the criteria object with conditions based on the given parameter |
279
|
|
|
* |
280
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
281
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
282
|
|
|
* @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
283
|
|
|
*/ |
284
|
|
|
protected function initCriteriaConditions( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
285
|
|
|
{ |
286
|
|
|
if( !isset( $params['filter'] ) ) { |
287
|
|
|
return $criteria; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$existing = $criteria->getConditions(); |
291
|
|
|
$criteria->setConditions( $criteria->toConditions( (array) $params['filter'] ) ); |
292
|
|
|
|
293
|
|
|
$expr = array( $criteria->getConditions(), $existing ); |
294
|
|
|
return $criteria->setConditions( $criteria->combine( '&&', $expr ) ); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Initializes the criteria object with the slice based on the given parameter. |
300
|
|
|
* |
301
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
302
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
303
|
|
|
* @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
304
|
|
|
*/ |
305
|
|
|
protected function initCriteriaSlice( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
306
|
|
|
{ |
307
|
|
|
$start = ( isset( $params['page']['offset'] ) ? (int) $params['page']['offset'] : 0 ); |
308
|
|
|
$size = ( isset( $params['page']['limit'] ) ? (int) $params['page']['limit'] : 25 ); |
309
|
|
|
|
310
|
|
|
return $criteria->setSlice( $start, $size ); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Initializes the criteria object with sortations based on the given parameter |
316
|
|
|
* |
317
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
318
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
319
|
|
|
* @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
|
|
|
|
320
|
|
|
*/ |
321
|
|
|
protected function initCriteriaSortations( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
322
|
|
|
{ |
323
|
|
|
if( !isset( $params['sort'] ) ) { |
324
|
|
|
return; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$sortation = []; |
328
|
|
|
|
329
|
|
|
foreach( explode( ',', $params['sort'] ) as $sort ) |
330
|
|
|
{ |
331
|
|
|
if( $sort[0] === '-' ) { |
332
|
|
|
$sortation[] = $criteria->sort( '-', substr( $sort, 1 ) ); |
333
|
|
|
} else { |
334
|
|
|
$sortation[] = $criteria->sort( '+', $sort ); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
return $criteria->setSortations( $sortation ); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Returns the available REST verbs and the available parameters |
344
|
|
|
* |
345
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
346
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
347
|
|
|
* @param string|null $prefix Form parameter prefix when nesting parameters is required |
348
|
|
|
* @param string $allow Allowed HTTP methods |
349
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
350
|
|
|
*/ |
351
|
|
|
public function getOptionsResponse( ServerRequestInterface $request, ResponseInterface $response, $prefix, $allow ) |
|
|
|
|
352
|
|
|
{ |
353
|
|
|
$view = $this->getView(); |
354
|
|
|
$view->prefix = $prefix; |
355
|
|
|
|
356
|
|
|
$tplconf = 'client/jsonapi/standard/template-options'; |
357
|
|
|
$default = 'options-standard.php'; |
358
|
|
|
|
359
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
360
|
|
|
|
361
|
|
|
return $response->withHeader( 'Allow', $allow ) |
362
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
363
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
364
|
|
|
->withStatus( 200 ); |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.