1 | <?php |
||
19 | class Parser implements ParserInterface { |
||
20 | |||
21 | /** |
||
22 | * |
||
23 | * @var null|\Xparse\ElementFinder\ElementFinder |
||
24 | */ |
||
25 | protected $lastPage = null; |
||
26 | |||
27 | /** |
||
28 | * @var ClientInterface |
||
29 | */ |
||
30 | protected $client = null; |
||
31 | |||
32 | /** |
||
33 | * @var ElementFinderFactoryInterface |
||
34 | */ |
||
35 | protected $elementFinderFactory = null; |
||
36 | |||
37 | /** |
||
38 | * @var null|ResponseInterface |
||
39 | */ |
||
40 | protected $lastResponse = null; |
||
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) { |
|
63 | |||
64 | |||
65 | /** |
||
66 | * @param string $url |
||
67 | * @param array $options |
||
68 | * @return ElementFinder |
||
69 | * @throws \Exception |
||
70 | * @throws \InvalidArgumentException |
||
71 | */ |
||
72 | 5 | public function get($url, array $options = []) { |
|
80 | |||
81 | |||
82 | /** |
||
83 | * @param string $url |
||
84 | * @param string|resource|\Psr\Http\Message\StreamInterface $body Message body. |
||
85 | * @param array $options |
||
86 | * @return ElementFinder |
||
87 | * @throws \Exception |
||
88 | * @throws \InvalidArgumentException |
||
89 | */ |
||
90 | 2 | public function post($url, $body = null, array $options = []) { |
|
100 | |||
101 | |||
102 | /** |
||
103 | * @return \Xparse\ElementFinder\ElementFinder |
||
104 | */ |
||
105 | 2 | public function getLastPage() { |
|
108 | |||
109 | |||
110 | /** |
||
111 | * @param \Xparse\ElementFinder\ElementFinder $lastPage |
||
112 | * @return $this |
||
113 | */ |
||
114 | 5 | public function setLastPage($lastPage) { |
|
118 | |||
119 | |||
120 | /** |
||
121 | * @return null|ResponseInterface |
||
122 | */ |
||
123 | 2 | public function getLastResponse() { |
|
126 | |||
127 | |||
128 | /** |
||
129 | * @return ClientInterface |
||
130 | */ |
||
131 | 4 | public function getClient() { |
|
134 | |||
135 | |||
136 | /** |
||
137 | * @param $request |
||
138 | * @param array $options |
||
139 | * @return \Xparse\ElementFinder\ElementFinder |
||
140 | * @throws \Exception |
||
141 | */ |
||
142 | 5 | public function send(RequestInterface $request, array $options = []) { |
|
143 | |||
144 | 5 | $prevCallback = !empty($options['on_stats']) ? $options['on_stats'] : null; |
|
145 | |||
146 | 5 | $effectiveUri = null; |
|
147 | 5 | $options['on_stats'] = function (TransferStats $stats) use (&$effectiveUri, $prevCallback) { |
|
148 | 5 | $effectiveUri = $stats->getEffectiveUri(); |
|
149 | 5 | if ($prevCallback !== null) { |
|
150 | /** @var callable $prevCallback */ |
||
151 | $prevCallback($stats); |
||
152 | } |
||
153 | 5 | }; |
|
154 | |||
155 | 5 | $response = $this->client->send($request, $options); |
|
156 | |||
157 | 5 | $guzzleEffectiveUrl = $response->getHeaderLine('X-GUZZLE-EFFECTIVE-URL'); |
|
158 | 5 | if (!empty($guzzleEffectiveUrl)) { |
|
159 | 5 | $effectiveUri = $guzzleEffectiveUrl; |
|
160 | 5 | } |
|
161 | |||
162 | 5 | $page = $this->elementFinderFactory->create($response, $effectiveUri); |
|
163 | |||
164 | 5 | $this->setLastPage($page); |
|
165 | 5 | $this->lastResponse = $response; |
|
166 | 5 | return $page; |
|
167 | } |
||
168 | |||
169 | |||
170 | /** |
||
171 | * @return ElementFinderFactoryInterface |
||
172 | */ |
||
173 | 1 | public function getElementFinderFactory() { |
|
176 | |||
177 | } |
||
178 |