1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xparse\Parser; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\HandlerStack; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Xparse\ElementFinder\ElementFinder; |
11
|
|
|
use Xparse\ElementFinder\Helper; |
12
|
|
|
use Xparse\Parser\Helper\ElementFinderFactory; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
* @package Xparse\Parser |
17
|
|
|
*/ |
18
|
|
|
class Parser implements \Xparse\ParserInterface\ParserInterface { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Set true if we need to automatically convert relative links to absolute |
22
|
|
|
*/ |
23
|
|
|
protected $convertRelativeLinksState = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set true if we need automatically convert encoding to utf-8 |
27
|
|
|
*/ |
28
|
|
|
protected $convertEncodingState = true; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var null|\Xparse\ElementFinder\ElementFinder |
33
|
|
|
*/ |
34
|
|
|
protected $lastPage = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ClientInterface |
38
|
|
|
*/ |
39
|
|
|
protected $client = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ElementFinderFactory |
43
|
|
|
*/ |
44
|
|
|
protected $elementFinderFactory = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var null|ResponseInterface |
48
|
|
|
*/ |
49
|
|
|
protected $lastResponse = null; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ClientInterface|null $client |
54
|
|
|
* @param ElementFinderFactory|null $elementFinderFactory |
55
|
|
|
*/ |
56
|
6 |
|
public function __construct(ClientInterface $client = null, ElementFinderFactory $elementFinderFactory = null) { |
57
|
6 |
|
if (empty($client)) { |
58
|
1 |
|
$client = new \GuzzleHttp\Client([ |
59
|
1 |
|
\GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => true, |
60
|
1 |
|
]); |
61
|
|
|
|
62
|
1 |
|
} |
63
|
|
|
|
64
|
6 |
|
if (empty($elementFinderFactory)) { |
65
|
6 |
|
$elementFinderFactory = new ElementFinderFactory(); |
66
|
6 |
|
} |
67
|
|
|
|
68
|
6 |
|
$this->elementFinderFactory = $elementFinderFactory; |
69
|
6 |
|
$this->client = $client; |
70
|
6 |
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $url |
75
|
|
|
* @return \Xparse\ElementFinder\ElementFinder |
76
|
|
|
* @throws \InvalidArgumentException |
77
|
|
|
*/ |
78
|
3 |
|
public function get($url) { |
79
|
3 |
|
if (empty($url) or !is_string($url)) { |
80
|
1 |
|
throw new \InvalidArgumentException("Url must be not empty and string."); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
$request = new \GuzzleHttp\Psr7\Request('GET', $url); |
84
|
2 |
|
return $this->send($request); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $url |
90
|
|
|
* @param array $data |
91
|
|
|
* @return \Xparse\ElementFinder\ElementFinder |
92
|
|
|
* @throws \InvalidArgumentException |
93
|
|
|
*/ |
94
|
2 |
|
public function post($url, $data) { |
95
|
|
|
|
96
|
2 |
|
if (empty($url) or !is_string($url)) { |
97
|
1 |
|
throw new \InvalidArgumentException("Url must be not empty and string."); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$request = new Request('POST', $url, [ |
101
|
1 |
|
'body' => $data, |
102
|
1 |
|
]); |
103
|
|
|
|
104
|
1 |
|
return $this->send($request); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return \Xparse\ElementFinder\ElementFinder |
110
|
|
|
*/ |
111
|
2 |
|
public function getLastPage() { |
112
|
2 |
|
return $this->lastPage; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param \Xparse\ElementFinder\ElementFinder $lastPage |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
3 |
|
public function setLastPage($lastPage) { |
121
|
3 |
|
$this->lastPage = $lastPage; |
122
|
3 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return null|ResponseInterface |
128
|
|
|
*/ |
129
|
1 |
|
public function getLastResponse() { |
130
|
1 |
|
return $this->lastResponse; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return ClientInterface |
136
|
|
|
*/ |
137
|
3 |
|
public function getClient() { |
138
|
3 |
|
return $this->client; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* |
144
|
|
|
* @return boolean |
145
|
|
|
*/ |
146
|
|
|
public function getConvertRelativeLinksState() { |
147
|
|
|
return $this->convertRelativeLinksState; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param boolean $convertRelativeLinksState |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
|
|
public function setConvertRelativeLinksState($convertRelativeLinksState) { |
156
|
|
|
$this->convertRelativeLinksState = $convertRelativeLinksState; |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return boolean |
163
|
|
|
*/ |
164
|
|
|
public function getConvertEncodingState() { |
165
|
|
|
return $this->convertEncodingState; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param boolean $convertEncodingState |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
|
|
public function setConvertEncodingState($convertEncodingState) { |
174
|
|
|
$this->convertEncodingState = $convertEncodingState; |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $request |
181
|
|
|
* @param array $options |
182
|
|
|
* @return \Xparse\ElementFinder\ElementFinder |
183
|
|
|
* @throws \Exception |
184
|
|
|
*/ |
185
|
3 |
|
public function send(RequestInterface $request, $options = []) { |
186
|
|
|
/** @var RequestInterface $lastRequest */ |
187
|
3 |
|
$lastRequest = null; |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** @var HandlerStack $handler */ |
191
|
3 |
|
$stack = $this->client->getConfig('handler'); |
192
|
|
|
|
193
|
3 |
|
if (!empty($stack) and $stack instanceof HandlerStack) { |
194
|
|
|
$stack->remove('last_request'); |
195
|
|
|
$stack->push(\GuzzleHttp\Middleware::mapRequest(function (RequestInterface $request) use (&$lastRequest) { |
196
|
|
|
$lastRequest = $request; |
197
|
|
|
return $request; |
198
|
|
|
}), 'last_request'); |
199
|
|
|
|
200
|
|
|
} |
201
|
3 |
|
$response = $this->client->send($request, $options); |
202
|
|
|
|
203
|
3 |
|
$url = (!empty($lastRequest)) ? $lastRequest->getUri()->__toString() : ''; |
204
|
|
|
|
205
|
3 |
|
$page = $this->elementFinderFactory->create($response, $url); |
206
|
|
|
|
207
|
3 |
|
$this->setLastPage($page); |
208
|
3 |
|
$this->lastResponse = $response; |
209
|
3 |
|
return $page; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
|