PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / apimatic / core / src / Request / Request.php
ameliabooking / vendor / apimatic / core / src / Request Last commit date
Parameters 1 year ago Request.php 1 year ago RequestBuilder.php 1 year ago
Request.php
268 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Core\Request;
6
7 use Closure;
8 use Core\Client;
9 use Core\Request\Parameters\MultipleParams;
10 use Core\Types\Sdk\CoreFileWrapper;
11 use Core\Utils\CoreHelper;
12 use CoreInterfaces\Core\Format;
13 use CoreInterfaces\Core\Request\RequestMethod;
14 use CoreInterfaces\Core\Request\RequestSetterInterface;
15 use CoreInterfaces\Http\RetryOption;
16
17 class Request implements RequestSetterInterface
18 {
19 private $converter;
20 private $queryUrl;
21 private $requestMethod = RequestMethod::GET;
22 private $headers = [];
23 private $parameters = [];
24 private $parametersEncoded = [];
25 private $parametersMultipart = [];
26 private $body;
27 private $retryOption = RetryOption::USE_GLOBAL_SETTINGS;
28 private $allowContentType = true;
29
30 /**
31 * Creates a new Request object.
32 */
33 public function __construct(string $queryUrl, ?Client $client = null, ?MultipleParams $globalParams = null)
34 {
35 $this->queryUrl = $queryUrl;
36 $this->converter = Client::getConverter($client);
37 if ($globalParams != null) {
38 $globalParams->apply($this);
39 }
40 $this->queryUrl = CoreHelper::validateUrl($this->queryUrl);
41 }
42
43 /**
44 * Returns the http method to be used for the call.
45 */
46 public function getHttpMethod(): string
47 {
48 return $this->requestMethod;
49 }
50
51 /**
52 * Returns the query URL for the request.
53 */
54 public function getQueryUrl(): string
55 {
56 return $this->queryUrl;
57 }
58
59 /**
60 * Returns the headers associated with the request.
61 */
62 public function getHeaders(): array
63 {
64 return $this->headers;
65 }
66
67 /**
68 * Returns the parameters for the request.
69 */
70 public function getParameters(): array
71 {
72 return $this->parameters;
73 }
74
75 /**
76 * Returns encoded parameters associated the request.
77 */
78 public function getEncodedParameters(): array
79 {
80 return $this->parametersEncoded;
81 }
82
83 /**
84 * Returns multipart parameters associated with the request.
85 */
86 public function getMultipartParameters(): array
87 {
88 return $this->parametersMultipart;
89 }
90
91 /**
92 * Returns body associated with the request.
93 */
94 public function getBody()
95 {
96 return $this->body;
97 }
98
99 /**
100 * Returns the state of retryOption for the request.
101 */
102 public function getRetryOption(): string
103 {
104 return $this->retryOption;
105 }
106
107 /**
108 * Converts the request to HttpRequest.
109 */
110 public function convert()
111 {
112 return $this->converter->createHttpRequest($this);
113 }
114
115 /**
116 * Creates an ApiException with the message provided.
117 */
118 public function toApiException(string $message)
119 {
120 return $this->converter->createApiException($message, $this, null);
121 }
122
123 /**
124 * Adds accept header to the request.
125 */
126 public function addAcceptHeader(string $accept): void
127 {
128 if (!$this->allowContentType) {
129 return;
130 }
131 if ($accept == Format::SCALAR) {
132 return;
133 }
134 $this->addHeader('Accept', $accept);
135 }
136
137 /**
138 * Sets the Http Method to be used for current request.
139 */
140 public function setHttpMethod(string $requestMethod): void
141 {
142 $this->requestMethod = $requestMethod;
143 }
144
145 /**
146 * Appends path to the query URL.
147 */
148 public function appendPath(string $path): void
149 {
150 $this->queryUrl .= $path;
151 }
152
153 /**
154 * Add or replace a single header
155 *
156 * @param string $key key for the header
157 * @param mixed $value value of the header
158 */
159 public function addHeader(string $key, $value): void
160 {
161 $this->headers[$key] = $value;
162 }
163
164 /**
165 * Adds template param value to the query URL, corresponding to the key provided.
166 */
167 public function addTemplate(string $key, $value): void
168 {
169 $this->queryUrl = str_replace("{{$key}}", $value, $this->queryUrl);
170 }
171
172 /**
173 * Adds an encoded form param to the request.
174 */
175 public function addEncodedFormParam(string $key, $value, $realValue): void
176 {
177 $this->parametersEncoded[$key] = $value;
178 $this->parameters[$key] = $realValue;
179 }
180
181 /**
182 * Adds a multipart form param to the request.
183 */
184 public function addMultipartFormParam(string $key, $value): void
185 {
186 $this->parametersMultipart[$key] = $value;
187 $this->parameters[$key] = $value;
188 }
189
190 /**
191 * Adds a body param to the current request.
192 */
193 public function addBodyParam($value, string $key = ''): void
194 {
195 if (empty($key)) {
196 $this->body = $value;
197 return;
198 }
199 if (is_array($this->body)) {
200 $this->body[$key] = $value;
201 } else {
202 $this->body = [$key => $value];
203 }
204 }
205
206 private function addContentType(string $format): void
207 {
208 if (!$this->allowContentType) {
209 return;
210 }
211 if (array_key_exists('content-type', array_change_key_case($this->headers))) {
212 return;
213 }
214 // if request has body, and content-type header is not already added
215 // then add content-type, based on type and format of body
216 if ($this->body instanceof CoreFileWrapper) {
217 $this->addHeader('content-type', 'application/octet-stream');
218 return;
219 }
220 if ($format != Format::JSON) {
221 $this->addHeader('content-type', $format);
222 return;
223 }
224 if (is_array($this->body)) {
225 $this->addHeader('content-type', Format::JSON);
226 return;
227 }
228 if (is_object($this->body)) {
229 $this->addHeader('content-type', Format::JSON);
230 return;
231 }
232 $this->addHeader('content-type', Format::SCALAR);
233 }
234
235 /**
236 * Sets body format for the request and returns the body in a serialized format.
237 */
238 public function setBodyFormat(string $format, callable $serializer): void
239 {
240 if (!empty($this->parameters)) {
241 return;
242 }
243
244 if (is_null($this->body)) {
245 return;
246 }
247
248 $this->addContentType($format);
249 $this->body = Closure::fromCallable($serializer)($this->body);
250 }
251
252 /**
253 * Sets value for retryOption for the request.
254 */
255 public function setRetryOption(string $retryOption): void
256 {
257 $this->retryOption = $retryOption;
258 }
259
260 /**
261 * Sets if the request has an allowContentType header or not.
262 */
263 public function shouldAddContentType(bool $allowContentType): void
264 {
265 $this->allowContentType = $allowContentType;
266 }
267 }
268