1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class HTTPClientAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource HTTPClientAbstract.php |
6
|
|
|
* @created 22.02.2019 |
7
|
|
|
* @package chillerlan\HTTP\Psr18 |
8
|
|
|
* @author smiley <[email protected]> |
9
|
|
|
* @copyright 2019 smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\HTTP\Psr18; |
14
|
|
|
|
15
|
|
|
use chillerlan\HTTP\{HTTPOptions, Psr7, Psr17}; |
16
|
|
|
use chillerlan\HTTP\Psr7\Request; |
17
|
|
|
use chillerlan\HTTP\Psr17\ResponseFactory; |
18
|
|
|
use chillerlan\Settings\SettingsContainerInterface; |
19
|
|
|
use Psr\Http\Message\{ResponseFactoryInterface, ResponseInterface}; |
20
|
|
|
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger}; |
21
|
|
|
|
22
|
|
|
abstract class HTTPClientAbstract implements HTTPClientInterface, LoggerAwareInterface{ |
23
|
|
|
use LoggerAwareTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \chillerlan\HTTP\HTTPOptions |
27
|
|
|
*/ |
28
|
|
|
protected $options; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \Psr\Http\Message\RequestFactoryInterface |
32
|
|
|
*/ |
33
|
|
|
protected $requestFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \Psr\Http\Message\ResponseFactoryInterface |
37
|
|
|
*/ |
38
|
|
|
protected $responseFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* CurlClient constructor. |
42
|
|
|
* |
43
|
|
|
* @param \chillerlan\Settings\SettingsContainerInterface|null $options |
44
|
|
|
* @param \Psr\Http\Message\ResponseFactoryInterface|null $responseFactory |
45
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
SettingsContainerInterface $options = null, |
49
|
|
|
ResponseFactoryInterface $responseFactory = null, |
50
|
|
|
LoggerInterface $logger = null |
51
|
|
|
){ |
52
|
|
|
$this->options = $options ?? new HTTPOptions; |
|
|
|
|
53
|
|
|
$this->responseFactory = $responseFactory ?? new ResponseFactory; |
54
|
|
|
$this->logger = $logger ?? new NullLogger; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @todo: files, content-type |
59
|
|
|
* |
60
|
|
|
* @param string $uri |
61
|
|
|
* @param string|null $method |
62
|
|
|
* @param array|null $query |
63
|
|
|
* @param mixed|null $body |
64
|
|
|
* @param array|null $headers |
65
|
|
|
* |
66
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
67
|
|
|
*/ |
68
|
|
|
public function request(string $uri, string $method = null, array $query = null, $body = null, array $headers = null):ResponseInterface{ |
69
|
|
|
$method = strtoupper($method ?? 'GET'); |
70
|
|
|
$headers = Psr7\normalize_request_headers($headers); |
|
|
|
|
71
|
|
|
$request = new Request($method, Psr7\merge_query($uri, $query ?? [])); |
72
|
|
|
|
73
|
|
|
if(in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true) && $body !== null){ |
74
|
|
|
|
75
|
|
|
if(is_array($body) || is_object($body)){ |
76
|
|
|
|
77
|
|
|
if(!isset($headers['Content-type'])){ |
78
|
|
|
$headers['Content-type'] = 'application/x-www-form-urlencoded'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if($headers['Content-type'] === 'application/x-www-form-urlencoded'){ |
82
|
|
|
$body = http_build_query($body, '', '&', PHP_QUERY_RFC1738); |
83
|
|
|
} |
84
|
|
|
elseif($headers['Content-type'] === 'application/json'){ |
85
|
|
|
$body = json_encode($body); |
86
|
|
|
} |
87
|
|
|
else{ |
88
|
|
|
$body = null; // @todo |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$request = $request->withBody(Psr17\create_stream((string)$body)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
foreach($headers as $header => $value){ |
97
|
|
|
$request = $request->withAddedHeader($header, $value); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->sendRequest($request); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.