|
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\Basket\Service; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* JSON API basket/service 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
|
|
|
private $controller; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Initializes the client |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context MShop context object |
|
34
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
|
35
|
|
|
* @param array $templatePaths List of file system paths where the templates are stored |
|
36
|
|
|
* @param string $path Name of the client, e.g "basket/service" |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MW\View\Iface $view, array $templatePaths, $path ) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct( $context, $view, $templatePaths, $path ); |
|
41
|
|
|
|
|
42
|
|
|
$this->controller = \Aimeos\Controller\Frontend\Basket\Factory::createController( $this->getContext() ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Deletes the resource or the resource list |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
50
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
51
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
52
|
|
|
*/ |
|
53
|
|
|
public function delete( ServerRequestInterface $request, ResponseInterface $response ) |
|
54
|
|
|
{ |
|
55
|
|
|
$view = $this->getView(); |
|
56
|
|
|
|
|
57
|
|
|
try |
|
58
|
|
|
{ |
|
59
|
|
|
$relId = $view->param( 'relatedid' ); |
|
60
|
|
|
$body = (string) $request->getBody(); |
|
61
|
|
|
$this->controller->setType( $view->param( 'id', 'default' ) ); |
|
62
|
|
|
|
|
63
|
|
|
if( $relId === '' || $relId === null ) |
|
64
|
|
|
{ |
|
65
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
|
66
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if( !is_array( $payload->data ) ) { |
|
70
|
|
|
$payload->data = [$payload->data]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
foreach( $payload->data as $entry ) |
|
74
|
|
|
{ |
|
75
|
|
|
if( !isset( $entry->id ) ) { |
|
76
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Type (ID) is missing' ) ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->controller->setService( $entry->id, null ); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
else |
|
83
|
|
|
{ |
|
84
|
|
|
$this->controller->setService( $relId, null ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$view->item = $this->controller->get(); |
|
89
|
|
|
$status = 200; |
|
90
|
|
|
} |
|
91
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
92
|
|
|
{ |
|
93
|
|
|
$status = 404; |
|
94
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
|
95
|
|
|
} |
|
96
|
|
|
catch( \Exception $e ) |
|
97
|
|
|
{ |
|
98
|
|
|
$status = 500; |
|
99
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->render( $response, $view, $status ); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Creates or updates the resource or the resource list |
|
108
|
|
|
* |
|
109
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
110
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
111
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
112
|
|
|
*/ |
|
113
|
|
|
public function post( ServerRequestInterface $request, ResponseInterface $response ) |
|
114
|
|
|
{ |
|
115
|
|
|
$view = $this->getView(); |
|
116
|
|
|
|
|
117
|
|
|
try |
|
118
|
|
|
{ |
|
119
|
|
|
$body = (string) $request->getBody(); |
|
120
|
|
|
$this->controller->setType( $view->param( 'id', 'default' ) ); |
|
121
|
|
|
|
|
122
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
|
123
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if( !is_array( $payload->data ) ) { |
|
127
|
|
|
$payload->data = [$payload->data]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
foreach( $payload->data as $entry ) |
|
131
|
|
|
{ |
|
132
|
|
|
if( !isset( $entry->id ) ) { |
|
133
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Service type (ID) is missing' ) ); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if( !isset( $entry->attributes ) ) { |
|
137
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Service attributes are missing' ) ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if( !isset( $entry->attributes->{'service.id'} ) ) { |
|
141
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Service ID in attributes is missing' ) ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$serviceId = $entry->attributes->{'service.id'}; |
|
145
|
|
|
unset( $entry->attributes->{'service.id'} ); |
|
146
|
|
|
|
|
147
|
|
|
$this->controller->setService( $entry->id, $serviceId, (array) $entry->attributes ); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
$view->item = $this->controller->get(); |
|
152
|
|
|
$status = 201; |
|
153
|
|
|
} |
|
154
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
155
|
|
|
{ |
|
156
|
|
|
$status = 404; |
|
157
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
|
158
|
|
|
} |
|
159
|
|
|
catch( \Exception $e ) |
|
160
|
|
|
{ |
|
161
|
|
|
$status = 500; |
|
162
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $this->render( $response, $view, $status ); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns the available REST verbs and the available parameters |
|
171
|
|
|
* |
|
172
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
173
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
174
|
|
|
* @param string|null $prefix Form parameter prefix when nesting parameters is required |
|
175
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
176
|
|
|
*/ |
|
177
|
|
|
public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null ) |
|
178
|
|
|
{ |
|
179
|
|
|
$view = $this->getView(); |
|
180
|
|
|
$view->attributes = [ |
|
181
|
|
|
'service.id' => [ |
|
182
|
|
|
'label' => 'ID of the payment or delivery service item', |
|
183
|
|
|
'type' => 'string', 'default' => '', 'required' => false, |
|
184
|
|
|
], |
|
185
|
|
|
]; |
|
186
|
|
|
|
|
187
|
|
|
$tplconf = 'client/jsonapi/standard/template-options'; |
|
188
|
|
|
$default = 'options-standard.php'; |
|
189
|
|
|
|
|
190
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
|
191
|
|
|
|
|
192
|
|
|
return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
|
193
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
|
194
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
|
195
|
|
|
->withStatus( 200 ); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Returns the response object with the rendered header and body |
|
201
|
|
|
* |
|
202
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
203
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
|
204
|
|
|
* @param integer $status HTTP status code |
|
205
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) |
|
208
|
|
|
{ |
|
209
|
|
|
$tplconf = 'client/jsonapi/basket/standard/template'; |
|
210
|
|
|
$default = 'basket/standard.php'; |
|
211
|
|
|
|
|
212
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
|
213
|
|
|
|
|
214
|
|
|
return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
|
215
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
|
216
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
|
217
|
|
|
->withStatus( $status ); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|