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\Service; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* JSON API standard 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
|
|
|
/** |
28
|
|
|
* Returns the resource or the resource list |
29
|
|
|
* |
30
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
31
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
32
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
33
|
|
|
*/ |
34
|
|
|
public function get( ServerRequestInterface $request, ResponseInterface $response ) |
35
|
|
|
{ |
36
|
|
|
$view = $this->getView(); |
37
|
|
|
|
38
|
|
|
try |
39
|
|
|
{ |
40
|
|
|
$ref = $view->param( 'include', ['media', 'price', 'text'] ); |
41
|
|
|
|
42
|
|
|
if( is_string( $ref ) ) { |
43
|
|
|
$ref = explode( ',', $ref ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'service' ); |
47
|
|
|
$basketCntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'basket' ); |
48
|
|
|
$basket = $basketCntl->get(); |
49
|
|
|
|
50
|
|
|
if( ( $id = $view->param( 'id' ) ) != '' ) |
51
|
|
|
{ |
52
|
|
|
$provider = $cntl->getProvider( $id, $ref ); |
53
|
|
|
|
54
|
|
|
if( $provider->isAvailable( $basket ) === true ) |
55
|
|
|
{ |
56
|
|
|
$view->attributes = [$id => $provider->getConfigFE( $basket )]; |
57
|
|
|
$view->prices = [$id => $provider->calcPrice( $basket )]; |
58
|
|
|
$view->items = $provider->getServiceItem(); |
59
|
|
|
$view->total = 1; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
else |
63
|
|
|
{ |
64
|
|
|
$attributes = $prices = $items = []; |
65
|
|
|
$type = $view->param( 'filter/cs_type' ); |
66
|
|
|
|
67
|
|
|
foreach( $cntl->getProviders( $type, $ref ) as $id => $provider ) |
68
|
|
|
{ |
69
|
|
|
if( $provider->isAvailable( $basket ) === true ) |
70
|
|
|
{ |
71
|
|
|
$attributes[$id] = $provider->getConfigFE( $basket ); |
72
|
|
|
$prices[$id] = $provider->calcPrice( $basket ); |
73
|
|
|
$items[$id] = $provider->getServiceItem(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$view->attributes = $attributes; |
78
|
|
|
$view->prices = $prices; |
79
|
|
|
$view->items = $items; |
80
|
|
|
$view->total = count( $items ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$status = 200; |
84
|
|
|
} |
85
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
86
|
|
|
{ |
87
|
|
|
$status = 404; |
88
|
|
|
$view->errors = array( array( |
89
|
|
|
'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ), |
90
|
|
|
'detail' => $e->getTraceAsString(), |
91
|
|
|
) ); |
92
|
|
|
} |
93
|
|
|
catch( \Exception $e ) |
94
|
|
|
{ |
95
|
|
|
$status = 500; |
96
|
|
|
$view->errors = array( array( |
97
|
|
|
'title' => $e->getMessage(), |
98
|
|
|
'detail' => $e->getTraceAsString(), |
99
|
|
|
) ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** client/jsonapi/service/standard/template |
103
|
|
|
* Relative path to the service JSON API template |
104
|
|
|
* |
105
|
|
|
* The template file contains the code and processing instructions |
106
|
|
|
* to generate the result shown in the JSON API body. The |
107
|
|
|
* configuration string is the path to the template file relative |
108
|
|
|
* to the templates directory (usually in client/jsonapi/templates). |
109
|
|
|
* |
110
|
|
|
* You can overwrite the template file configuration in extensions and |
111
|
|
|
* provide alternative templates. These alternative templates should be |
112
|
|
|
* named like the default one but with the string "standard" replaced by |
113
|
|
|
* an unique name. You may use the name of your project for this. If |
114
|
|
|
* you've implemented an alternative client class as well, "standard" |
115
|
|
|
* should be replaced by the name of the new class. |
116
|
|
|
* |
117
|
|
|
* @param string Relative path to the template creating the body for the GET method of the JSON API |
118
|
|
|
* @since 2017.03 |
119
|
|
|
* @category Developer |
120
|
|
|
*/ |
121
|
|
|
$tplconf = 'client/jsonapi/service/standard/template'; |
122
|
|
|
$default = 'service/standard.php'; |
123
|
|
|
|
124
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
125
|
|
|
|
126
|
|
|
return $response->withHeader( 'Allow', 'GET' ) |
127
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
128
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
129
|
|
|
->withStatus( $status ); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|