1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xparse\Parser; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\Psr7\Request; |
7
|
|
|
use GuzzleHttp\TransferStats; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Xparse\ElementFinder\ElementFinder; |
11
|
|
|
use Xparse\ElementFinder\Helper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* @package Xparse\Parser |
16
|
|
|
*/ |
17
|
|
|
class Parser implements \Xparse\ParserInterface\ParserInterface { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* @var null|\Xparse\ElementFinder\ElementFinder |
22
|
|
|
*/ |
23
|
|
|
protected $lastPage = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ClientInterface |
27
|
|
|
*/ |
28
|
|
|
protected $client = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ElementFinderFactory |
32
|
|
|
*/ |
33
|
|
|
protected $elementFinderFactory = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var null|ResponseInterface |
37
|
|
|
*/ |
38
|
|
|
protected $lastResponse = null; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ClientInterface|null $client |
43
|
|
|
* @param ElementFinderFactory|null $elementFinderFactory |
44
|
|
|
*/ |
45
|
7 |
|
public function __construct(ClientInterface $client = null, ElementFinderFactory $elementFinderFactory = null) { |
46
|
7 |
|
if (empty($client)) { |
47
|
1 |
|
$client = new \GuzzleHttp\Client([ |
48
|
1 |
|
\GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => true, |
49
|
1 |
|
]); |
50
|
|
|
|
51
|
1 |
|
} |
52
|
|
|
|
53
|
7 |
|
if ($elementFinderFactory === null) { |
54
|
7 |
|
$this->elementFinderFactory = new ElementFinderFactory(); |
55
|
7 |
|
} else { |
56
|
|
|
$this->elementFinderFactory = $elementFinderFactory; |
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
$this->client = $client; |
60
|
7 |
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $url |
65
|
|
|
* @return \Xparse\ElementFinder\ElementFinder |
66
|
|
|
* @throws \InvalidArgumentException |
67
|
|
|
*/ |
68
|
4 |
|
public function get($url) { |
69
|
4 |
|
if (empty($url) or !is_string($url)) { |
70
|
1 |
|
throw new \InvalidArgumentException('Url must be not empty and string.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
$request = new \GuzzleHttp\Psr7\Request('GET', $url); |
74
|
3 |
|
return $this->send($request); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $url |
80
|
|
|
* @param array $data |
81
|
|
|
* @return \Xparse\ElementFinder\ElementFinder |
82
|
|
|
* @throws \InvalidArgumentException |
83
|
|
|
*/ |
84
|
2 |
|
public function post($url, $data) { |
85
|
|
|
|
86
|
2 |
|
if (empty($url) or !is_string($url)) { |
87
|
1 |
|
throw new \InvalidArgumentException('Url must be not empty and string.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$request = new Request('POST', $url, [ |
91
|
1 |
|
'body' => $data, |
92
|
1 |
|
]); |
93
|
|
|
|
94
|
1 |
|
return $this->send($request); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \Xparse\ElementFinder\ElementFinder |
100
|
|
|
*/ |
101
|
2 |
|
public function getLastPage() { |
102
|
2 |
|
return $this->lastPage; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param \Xparse\ElementFinder\ElementFinder $lastPage |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
4 |
|
public function setLastPage($lastPage) { |
111
|
4 |
|
$this->lastPage = $lastPage; |
112
|
4 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return null|ResponseInterface |
118
|
|
|
*/ |
119
|
1 |
|
public function getLastResponse() { |
120
|
1 |
|
return $this->lastResponse; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return ClientInterface |
126
|
|
|
*/ |
127
|
3 |
|
public function getClient() { |
128
|
3 |
|
return $this->client; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $request |
134
|
|
|
* @param array $options |
135
|
|
|
* @return \Xparse\ElementFinder\ElementFinder |
136
|
|
|
* @throws \Exception |
137
|
|
|
*/ |
138
|
4 |
|
public function send(RequestInterface $request, array $options = []) { |
139
|
|
|
|
140
|
4 |
|
$prevCallback = !empty($options['on_stats']) ? $options['on_stats'] : null; |
141
|
|
|
|
142
|
4 |
|
$effectiveUri = null; |
143
|
4 |
|
$options['on_stats'] = function (TransferStats $stats) use (&$effectiveUri, $prevCallback) { |
144
|
4 |
|
$effectiveUri = $stats->getEffectiveUri(); |
145
|
4 |
|
if ($prevCallback !== null) { |
146
|
|
|
/** @var callable $prevCallback */ |
147
|
|
|
$prevCallback($stats); |
148
|
|
|
} |
149
|
4 |
|
}; |
150
|
|
|
|
151
|
4 |
|
$response = $this->client->send($request, $options); |
152
|
4 |
|
$page = $this->elementFinderFactory->create($response, $effectiveUri); |
|
|
|
|
153
|
|
|
|
154
|
4 |
|
$this->setLastPage($page); |
155
|
4 |
|
$this->lastResponse = $response; |
156
|
4 |
|
return $page; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.