1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @category Brownie/HttpClient |
4
|
|
|
* @author Brownie <[email protected]> |
5
|
|
|
* @license https://opensource.org/licenses/MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Brownie\HttpClient; |
9
|
|
|
|
10
|
|
|
use Brownie\HttpClient\Cookie\Cookie; |
11
|
|
|
use Brownie\HttpClient\Header\Header; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* HTTP client. |
15
|
|
|
* Target API for HTTP request. |
16
|
|
|
*/ |
17
|
|
|
class HttpClient |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Client adapter. |
22
|
|
|
* |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Sets incoming data. |
29
|
|
|
* |
30
|
|
|
* @param Client $client Client adapter. |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct(Client $client) |
33
|
|
|
{ |
34
|
4 |
|
$this->setClient($client); |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Performs a network request. |
39
|
|
|
* Returns the response. |
40
|
|
|
* |
41
|
|
|
* @param Request $request HTTP request params. |
42
|
|
|
* |
43
|
|
|
* @return Response |
44
|
|
|
* |
45
|
|
|
* @throws Exception\ClientException |
46
|
|
|
* @throws Exception\ValidateException |
47
|
|
|
*/ |
48
|
1 |
|
public function request(Request $request) |
49
|
|
|
{ |
50
|
1 |
|
$request->validate(); |
51
|
1 |
|
return $this->getClient()->httpRequest($request); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns HTTP client request. |
56
|
|
|
* |
57
|
|
|
* @return Request |
58
|
|
|
*/ |
59
|
1 |
|
public function createRequest() |
60
|
|
|
{ |
61
|
1 |
|
return new Request(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns HTTP cookie. |
66
|
|
|
* |
67
|
|
|
* @param string $name The name of the cookie. |
68
|
|
|
* @param string $value The value of the cookie. |
69
|
|
|
* |
70
|
|
|
* @return Cookie |
71
|
|
|
*/ |
72
|
1 |
|
public function createCookie($name, $value) |
73
|
|
|
{ |
74
|
1 |
|
return new Cookie(array( |
75
|
1 |
|
'name' => $name, |
76
|
1 |
|
'value' => $value |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns HTTP header. |
82
|
|
|
* |
83
|
|
|
* @param string $name Header name. |
84
|
|
|
* @param string $value Header value. |
85
|
|
|
* |
86
|
|
|
* @return Header |
87
|
|
|
*/ |
88
|
1 |
|
public function createHeader($name, $value) |
89
|
|
|
{ |
90
|
1 |
|
return new Header(array( |
91
|
1 |
|
'name' => $name, |
92
|
1 |
|
'value' => $value |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sets the request client. |
98
|
|
|
* Returns the current object. |
99
|
|
|
* |
100
|
|
|
* @param Client $client Client adapter. |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
4 |
|
private function setClient(Client $client) |
105
|
|
|
{ |
106
|
4 |
|
$this->client = $client; |
107
|
4 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns request client. |
112
|
|
|
* |
113
|
|
|
* @return Client |
114
|
|
|
*/ |
115
|
1 |
|
private function getClient() |
116
|
|
|
{ |
117
|
1 |
|
return $this->client; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|