|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SteamMarketProviders\ParserManager\Parser\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use PHPHtmlParser\Dom; |
|
8
|
|
|
use PHPHtmlParser\Exceptions\ChildNotFoundException; |
|
9
|
|
|
use PHPHtmlParser\Exceptions\CircularException; |
|
10
|
|
|
use PHPHtmlParser\Exceptions\ContentLengthException; |
|
11
|
|
|
use PHPHtmlParser\Exceptions\LogicalException; |
|
12
|
|
|
use PHPHtmlParser\Exceptions\NotLoadedException; |
|
13
|
|
|
use PHPHtmlParser\Exceptions\StrictException; |
|
14
|
|
|
use SteamMarketProviders\ParserManager\Builder\ParseRulesBuilder; |
|
15
|
|
|
use SteamMarketProviders\ParserManager\Contract\StrategyInterface; |
|
16
|
|
|
use SteamMarketProviders\ParserManager\Contract\UrlBuilderInterface; |
|
17
|
|
|
use SteamMarketProviders\ParserManager\Http\Strategy\GuzzleStrategy; |
|
18
|
|
|
|
|
19
|
|
|
abstract class AbstractProvider |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ParseRulesBuilder|null |
|
23
|
|
|
*/ |
|
24
|
|
|
private null|ParseRulesBuilder $parseRulesBuilder = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var UrlBuilderInterface|null |
|
28
|
|
|
*/ |
|
29
|
|
|
private null|UrlBuilderInterface $urlBuilder = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Dom |
|
33
|
|
|
*/ |
|
34
|
|
|
private Dom $dom; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param StrategyInterface|null $strategy |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(private null|StrategyInterface $strategy = null) |
|
40
|
|
|
{ |
|
41
|
|
|
if (!$this->strategy) { |
|
42
|
|
|
$this->strategy = new GuzzleStrategy(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->dom = new Dom(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param int $page |
|
50
|
|
|
* @return UrlBuilderInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract protected function createUrl(int $page): UrlBuilderInterface; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return ParseRulesBuilder |
|
56
|
|
|
*/ |
|
57
|
|
|
abstract protected function createParseRules(): ParseRulesBuilder; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param UrlBuilderInterface $urlBuilder |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setUrl(UrlBuilderInterface $urlBuilder): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->urlBuilder = $urlBuilder; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param ParseRulesBuilder $parseRulesBuilder |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setParseRules(ParseRulesBuilder $parseRulesBuilder): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->parseRulesBuilder = $parseRulesBuilder; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
final public function start(int $page): array |
|
78
|
|
|
{ |
|
79
|
|
|
if (!$this->urlBuilder) { |
|
80
|
|
|
$this->urlBuilder = $this->createUrl($page); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!$this->parseRulesBuilder) { |
|
84
|
|
|
$this->parseRulesBuilder = $this->createParseRules(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->doParseRequest(); |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @throws ChildNotFoundException |
|
93
|
|
|
* @throws NotLoadedException |
|
94
|
|
|
* @throws ContentLengthException |
|
95
|
|
|
* @throws CircularException |
|
96
|
|
|
* @throws LogicalException |
|
97
|
|
|
* @throws StrictException |
|
98
|
|
|
*/ |
|
99
|
|
|
private function doParseRequest(): array |
|
100
|
|
|
{ |
|
101
|
|
|
$html = $this->strategy->sendRequest($this->urlBuilder->build()); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
$this->dom->loadStr($html); |
|
104
|
|
|
|
|
105
|
|
|
$contents = $this->dom->find('#searchResultsRows .market_listing_row_link'); |
|
106
|
|
|
|
|
107
|
|
|
$data = []; |
|
108
|
|
|
|
|
109
|
|
|
foreach ($contents as $row) { |
|
110
|
|
|
$link = $row->getAttribute('href'); |
|
111
|
|
|
$name = $row->find('.market_listing_item_name_block > span'); |
|
112
|
|
|
$prices = $row->find('.market_listing_their_price .normal_price span'); |
|
113
|
|
|
|
|
114
|
|
|
$data[] = [ |
|
115
|
|
|
'link'=> $link, |
|
116
|
|
|
'name' => $name->innerHtml, |
|
117
|
|
|
'price' => $prices->innerHtml |
|
118
|
|
|
]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $data; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.