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