|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Saxulum\HttpClient\Vinelab; |
|
4
|
|
|
|
|
5
|
|
|
use Vinelab\Http\Client; |
|
6
|
|
|
use Vinelab\Http\Response as VinelabResponse; |
|
7
|
|
|
use Saxulum\HttpClient\HeaderConverter; |
|
8
|
|
|
use Saxulum\HttpClient\HttpClientInterface; |
|
9
|
|
|
use Saxulum\HttpClient\Request; |
|
10
|
|
|
use Saxulum\HttpClient\Response; |
|
11
|
|
|
|
|
12
|
|
|
class HttpClient implements HttpClientInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Client |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $client; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param Client $client |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(Client $client = null) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->client = null !== $client ? $client : new Client(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Request $request |
|
29
|
|
|
* @return Response |
|
30
|
|
|
*/ |
|
31
|
|
|
public function request(Request $request) |
|
32
|
|
|
{ |
|
33
|
|
|
$method = strtolower($request->getMethod()); |
|
34
|
|
|
$headers = HeaderConverter::convertAssociativeToRaw( |
|
35
|
|
|
$this->prepareHeaders($request) |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
/** @var VinelabResponse $vinelabResponse */ |
|
39
|
|
|
$vinelabResponse = $this->client->$method(array( |
|
40
|
|
|
'version' => $request->getProtocolVersion(), |
|
41
|
|
|
'url' => (string) $request->getUrl(), |
|
42
|
|
|
'headers' => $headers, |
|
43
|
|
|
'content' => $request->getContent() |
|
44
|
|
|
)); |
|
45
|
|
|
|
|
46
|
|
|
return new Response( |
|
47
|
|
|
$request->getProtocolVersion(), |
|
48
|
|
|
$vinelabResponse->statusCode(), |
|
49
|
|
|
$this->getStatusMessage($vinelabResponse->statusCode()), |
|
50
|
|
|
$vinelabResponse->headers(), |
|
|
|
|
|
|
51
|
|
|
$vinelabResponse->content() |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Request $request |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function prepareHeaders(Request $request) |
|
60
|
|
|
{ |
|
61
|
|
|
$headers = $request->getHeaders(); |
|
62
|
|
|
if (null !== $request->getContent() && !isset($headers['Content-Type'])) { |
|
63
|
|
|
$headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $headers; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param int $statusCode |
|
71
|
|
|
* @return string |
|
72
|
|
|
* @throws \Exception |
|
73
|
|
|
*/ |
|
74
|
|
|
protected static function getStatusMessage($statusCode) |
|
75
|
|
|
{ |
|
76
|
|
|
static $reflectionResponse; |
|
77
|
|
|
|
|
78
|
|
|
if (null === $reflectionResponse) { |
|
79
|
|
|
$responseClass = 'Saxulum\HttpClient\Response'; |
|
80
|
|
|
$reflectionResponse = new \ReflectionClass($responseClass); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$constantName = self::getCodeConstantName($statusCode); |
|
84
|
|
|
|
|
85
|
|
|
if (!$reflectionResponse->hasConstant($constantName)) { |
|
86
|
|
|
throw new \Exception("Unknown status code {$statusCode}!"); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $reflectionResponse->getConstant($constantName); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param int $statusCode |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
protected static function getCodeConstantName($statusCode) |
|
97
|
|
|
{ |
|
98
|
|
|
return 'STATUS_MESSAGE_' . $statusCode; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: