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