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\Cookie\CookieJar; |
10
|
|
|
use GuzzleHttp\Psr7\Request; |
11
|
|
|
use GuzzleHttp\RequestOptions; |
12
|
|
|
use GuzzleHttp\TransferStats; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use Xparse\ElementFinder\ElementFinder; |
17
|
|
|
use Xparse\ElementFinder\ElementFinderInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Ivan Shcherbak <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Parser implements ParserInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @var ElementFinder|null |
28
|
|
|
*/ |
29
|
|
|
protected $lastPage; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ClientInterface |
33
|
|
|
*/ |
34
|
|
|
protected $client; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ElementFinderFactoryInterface |
38
|
|
|
*/ |
39
|
|
|
protected $elementFinderFactory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var null|ResponseInterface |
43
|
|
|
*/ |
44
|
|
|
protected $lastResponse; |
45
|
|
|
|
46
|
|
|
|
47
|
10 |
|
public function __construct(ClientInterface $client = null, ElementFinderFactoryInterface $elementFinderFactory = null) |
48
|
10 |
|
{ |
49
|
3 |
|
if ($client === null) { |
50
|
3 |
|
$client = new Client([ |
51
|
3 |
|
RequestOptions::ALLOW_REDIRECTS => true, |
52
|
3 |
|
RequestOptions::COOKIES => new CookieJar(), |
53
|
3 |
|
]); |
54
|
|
|
} |
55
|
10 |
|
$this->client = $client; |
56
|
10 |
|
$this->elementFinderFactory = $elementFinderFactory ?? new ElementFinderFactory(); |
57
|
10 |
|
} |
58
|
|
|
|
59
|
10 |
|
|
60
|
|
|
/** |
61
|
10 |
|
* @throws InvalidArgumentException |
62
|
10 |
|
*/ |
63
|
|
|
public function get(string $url, array $options = []): ElementFinderInterface |
64
|
|
|
{ |
65
|
|
|
if ($url === '') { |
66
|
|
|
throw new InvalidArgumentException('Url can\'t be empty'); |
67
|
|
|
} |
68
|
|
|
return $this->send( |
69
|
|
|
new Request('GET', $url), |
70
|
|
|
$options |
71
|
|
|
); |
72
|
5 |
|
} |
73
|
5 |
|
|
74
|
1 |
|
public function send(RequestInterface $request, array $options = []): ElementFinderInterface |
75
|
|
|
{ |
76
|
|
|
|
77
|
4 |
|
$prevCallback = !empty($options['on_stats']) ? $options['on_stats'] : null; |
78
|
4 |
|
|
79
|
|
|
$effectiveUrl = ''; |
80
|
|
|
$options['on_stats'] = function (TransferStats $stats) use (&$effectiveUrl, $prevCallback) { |
81
|
|
|
$effectiveUrl = $stats->getEffectiveUri()->__toString(); |
82
|
|
|
if ($prevCallback !== null) { |
83
|
|
|
/** @var callable $prevCallback */ |
84
|
|
|
$prevCallback($stats); |
85
|
|
|
} |
86
|
|
|
}; |
87
|
|
|
|
88
|
|
|
$response = $this->client->send($request, $options); |
89
|
|
|
|
90
|
2 |
|
$guzzleEffectiveUrl = $response->getHeaderLine('X-GUZZLE-EFFECTIVE-URL'); |
91
|
|
|
if ($guzzleEffectiveUrl !== '') { |
92
|
2 |
|
$effectiveUrl = $guzzleEffectiveUrl; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
$elementFinder = $this->elementFinderFactory->create($response, $effectiveUrl); |
96
|
1 |
|
$this->lastPage = $elementFinder; |
|
|
|
|
97
|
|
|
$this->lastResponse = $response; |
98
|
1 |
|
|
99
|
|
|
return $elementFinder; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @throws InvalidArgumentException |
104
|
|
|
*/ |
105
|
2 |
|
public function post(string $url, array $options = []): ElementFinderInterface |
106
|
2 |
|
{ |
107
|
|
|
if ($url === '') { |
108
|
|
|
throw new InvalidArgumentException('Url can\'t be empty'); |
109
|
|
|
} |
110
|
|
|
return $this->send( |
111
|
|
|
new Request('POST', $url), |
112
|
|
|
$options |
113
|
|
|
); |
114
|
5 |
|
} |
115
|
5 |
|
|
116
|
5 |
|
public function getLastPage(): ?ElementFinderInterface |
117
|
|
|
{ |
118
|
|
|
return $this->lastPage; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getLastResponse(): ?ResponseInterface |
122
|
|
|
{ |
123
|
2 |
|
return $this->lastResponse; |
124
|
2 |
|
} |
125
|
|
|
|
126
|
|
|
public function getClient(): ClientInterface |
127
|
|
|
{ |
128
|
|
|
return $this->client; |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
public function getElementFinderFactory(): ElementFinderFactoryInterface |
132
|
4 |
|
{ |
133
|
|
|
return $this->elementFinderFactory; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..