1 | <?php |
||
13 | trait HasHttpRequests |
||
14 | { |
||
15 | use ResponseCastable; |
||
16 | |||
17 | /** |
||
18 | * @var \GuzzleHttp\ClientInterface |
||
19 | */ |
||
20 | protected $httpClient; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $middlewares = []; |
||
26 | |||
27 | /** |
||
28 | * @var \GuzzleHttp\HandlerStack |
||
29 | */ |
||
30 | protected $handlerStack; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected static $defaults = [ |
||
36 | 'curl' => [ |
||
37 | CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, |
||
38 | ], |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Set guzzle default settings. |
||
43 | * |
||
44 | * @param array $defaults |
||
45 | */ |
||
46 | public static function setDefaultOptions($defaults = []) |
||
50 | |||
51 | /** |
||
52 | * Return current guzzle default settings. |
||
53 | * |
||
54 | * @return array |
||
55 | */ |
||
56 | public static function getDefaultOptions(): array |
||
60 | |||
61 | /** |
||
62 | * Set GuzzleHttp\Product. |
||
63 | * |
||
64 | * @param \GuzzleHttp\ClientInterface $httpClient |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function setHttpClient(ClientInterface $httpClient) |
||
74 | |||
75 | /** |
||
76 | * Return GuzzleHttp\ClientInterface instance. |
||
77 | * |
||
78 | * @return ClientInterface |
||
79 | */ |
||
80 | public function getHttpClient(): ClientInterface |
||
92 | |||
93 | /** |
||
94 | * Add a middleware. |
||
95 | * |
||
96 | * @param callable $middleware |
||
97 | * @param string|null $name |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function pushMiddleware(callable $middleware, string $name = null) |
||
111 | |||
112 | /** |
||
113 | * Return all middlewares. |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | public function getMiddlewares(): array |
||
121 | |||
122 | /** |
||
123 | * Make a request. |
||
124 | * |
||
125 | * @param string $url |
||
126 | * @param string $method |
||
127 | * @param array $options |
||
128 | * |
||
129 | * @return \Psr\Http\Message\ResponseInterface|\Kaylyu\Alipay\Kernel\Support\Collection|array|object|string |
||
130 | */ |
||
131 | public function request($url, $method = 'GET', $options = []): ResponseInterface |
||
148 | |||
149 | /** |
||
150 | * @param \GuzzleHttp\HandlerStack $handlerStack |
||
151 | * |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function setHandlerStack(HandlerStack $handlerStack) |
||
160 | |||
161 | /** |
||
162 | * Build a handler stack. |
||
163 | * |
||
164 | * @return \GuzzleHttp\HandlerStack |
||
165 | */ |
||
166 | public function getHandlerStack(): HandlerStack |
||
180 | |||
181 | /** |
||
182 | * @param array $options |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | protected function fixJsonIssue(array $options): array |
||
202 | |||
203 | /** |
||
204 | * Get guzzle handler. |
||
205 | * |
||
206 | * @return callable |
||
207 | */ |
||
208 | protected function getGuzzleHandler() |
||
218 | } |
||
219 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: