1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Commercetools\Core\Request\ShippingMethods; |
4
|
|
|
|
5
|
|
|
use Commercetools\Core\Client\HttpMethod; |
6
|
|
|
use Commercetools\Core\Client\HttpRequest; |
7
|
|
|
use Commercetools\Core\Model\Common\Collection; |
8
|
|
|
use Commercetools\Core\Model\Common\Context; |
9
|
|
|
use Commercetools\Core\Model\JsonObjectMapper; |
10
|
|
|
use Commercetools\Core\Model\MapperInterface; |
11
|
|
|
use Commercetools\Core\Model\ShippingMethod\ShippingMethod; |
12
|
|
|
use Commercetools\Core\Model\ShippingMethod\ShippingMethodCollection; |
13
|
|
|
use Commercetools\Core\Request\AbstractApiRequest; |
14
|
|
|
use Commercetools\Core\Request\ExpandTrait; |
15
|
|
|
use Commercetools\Core\Response\ApiResponseInterface; |
16
|
|
|
use Commercetools\Core\Response\PagedQueryResponse; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @package Commercetools\Core\Request\ShippingMethods |
21
|
|
|
* @link https://docs.commercetools.com/http-api-projects-shippingMethods.html#get-shippingmethods-for-a-location |
22
|
|
|
* @method ShippingMethodCollection mapResponse(ApiResponseInterface $response) |
23
|
|
|
* @method ShippingMethodCollection mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null) |
24
|
|
|
*/ |
25
|
|
|
class ShippingMethodByMatchingLocationGetRequest extends AbstractApiRequest |
26
|
|
|
{ |
27
|
|
|
use ExpandTrait; |
28
|
|
|
|
29
|
|
|
protected $resultClass = ShippingMethodCollection::class; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $country; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $state; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $currency; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $country |
48
|
|
|
* @param Context $context |
49
|
|
|
*/ |
50
|
10 |
|
public function __construct($country, Context $context = null) |
51
|
|
|
{ |
52
|
10 |
|
parent::__construct(ShippingMethodsEndpoint::endpoint(), $context); |
53
|
10 |
|
$this->withCountry($country); |
54
|
10 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $country |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
10 |
|
public function withCountry($country) |
61
|
|
|
{ |
62
|
10 |
|
return $this->addParam('country', $country); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $state |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
3 |
|
public function withState($state) |
70
|
|
|
{ |
71
|
3 |
|
return $this->addParam('state', $state); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $currency |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
2 |
|
public function withCurrency($currency) |
79
|
|
|
{ |
80
|
2 |
|
return $this->addParam('currency', $currency); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $country |
85
|
|
|
* @param Context $context |
86
|
|
|
* @return static |
87
|
|
|
*/ |
88
|
10 |
|
public static function ofCountry($country, Context $context = null) |
89
|
|
|
{ |
90
|
10 |
|
return new static($country, $context); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return HttpRequest |
95
|
|
|
* @internal |
96
|
|
|
*/ |
97
|
7 |
|
public function httpRequest() |
98
|
|
|
{ |
99
|
7 |
|
return new HttpRequest(HttpMethod::GET, $this->getPath()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param ResponseInterface $response |
104
|
|
|
* @return PagedQueryResponse |
105
|
|
|
*/ |
106
|
2 |
|
public function buildResponse(ResponseInterface $response) |
107
|
|
|
{ |
108
|
2 |
|
return new PagedQueryResponse($response, $this, $this->getContext()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
* @internal |
114
|
|
|
*/ |
115
|
7 |
|
protected function getPath() |
116
|
|
|
{ |
117
|
7 |
|
return (string)$this->getEndpoint() . '/matching-location' . $this->getParamString(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $result |
122
|
|
|
* @param Context $context |
123
|
|
|
* @param MapperInterface $mapper |
124
|
|
|
* @return Collection |
125
|
|
|
*/ |
126
|
3 |
|
public function map(array $result, Context $context = null, MapperInterface $mapper = null) |
127
|
|
|
{ |
128
|
3 |
|
$data = []; |
129
|
3 |
|
if (!empty($result['results'])) { |
130
|
1 |
|
$data = $result['results']; |
131
|
|
|
} |
132
|
3 |
|
if (is_null($mapper)) { |
133
|
3 |
|
$mapper = JsonObjectMapper::of($context); |
134
|
|
|
} |
135
|
3 |
|
return $mapper->map($data, $this->resultClass); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|