1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SteamMarketProviders\ParserManager\Builder; |
6
|
|
|
|
7
|
|
|
use SteamMarketProviders\Enums\SteamApp; |
8
|
|
|
use SteamMarketProviders\Enums\SteamCurrency; |
9
|
|
|
use SteamMarketProviders\Enums\SteamLanguage; |
10
|
|
|
use SteamMarketProviders\ParserManager\Enum\SteamConfigEnum; |
11
|
|
|
use SteamMarketProviders\ParserManager\Enum\BaseURLFiltersEnum; |
12
|
|
|
use SteamMarketProviders\ParserManager\Exception\BuilderNotSetParamException; |
13
|
|
|
use SteamMarketProviders\ParserManager\Exception\InvalidArgumentException; |
14
|
|
|
use SteamMarketProviders\ParserManager\Contract\UrlBuilderInterface; |
15
|
|
|
use stdClass; |
16
|
|
|
|
17
|
|
|
class SearchUrlBuilder implements UrlBuilderInterface |
18
|
|
|
{ |
19
|
|
|
protected stdClass $params; |
20
|
|
|
|
21
|
|
|
protected function reset(): void |
22
|
|
|
{ |
23
|
|
|
$this->params = new stdClass(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param SteamApp $steamApp |
28
|
|
|
* @return UrlBuilderInterface |
29
|
|
|
*/ |
30
|
|
|
public function setAppId(SteamApp $steamApp): UrlBuilderInterface |
31
|
|
|
{ |
32
|
|
|
$this->reset(); |
33
|
|
|
$this->params->appId = $steamApp->value; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @throws BuilderNotSetParamException |
40
|
|
|
*/ |
41
|
|
|
public function setLanguage(SteamLanguage $steamLanguage = SteamLanguage::English): UrlBuilderInterface |
42
|
|
|
{ |
43
|
|
|
$this->checkIfAppIdNotEmpty(); |
44
|
|
|
$this->params->language = $steamLanguage->value; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param SteamCurrency $steamCurrency |
51
|
|
|
* @return UrlBuilderInterface |
52
|
|
|
* @throws BuilderNotSetParamException |
53
|
|
|
*/ |
54
|
|
|
public function setCurrency(SteamCurrency $steamCurrency = SteamCurrency::USD): UrlBuilderInterface |
55
|
|
|
{ |
56
|
|
|
$this->checkIfAppIdNotEmpty(); |
57
|
|
|
$this->params->currency = $steamCurrency->value; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @throws BuilderNotSetParamException |
64
|
|
|
*/ |
65
|
|
|
public function setPage(int $page = 1): UrlBuilderInterface |
66
|
|
|
{ |
67
|
|
|
$this->checkIfAppIdNotEmpty(); |
68
|
|
|
$this->params->page = $page; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
* @throws InvalidArgumentException |
76
|
|
|
*/ |
77
|
|
|
public function build(): string |
78
|
|
|
{ |
79
|
|
|
$url = SteamConfigEnum::MarketSearchUrl->value; |
80
|
|
|
$url .= '/search?'; |
81
|
|
|
|
82
|
|
|
$params = []; |
83
|
|
|
|
84
|
|
|
$params[BaseURLFiltersEnum::AppId->value] = $this->params->appId; |
85
|
|
|
|
86
|
|
|
if (isset($this->params->language)) { |
87
|
|
|
$params[BaseURLFiltersEnum::Language->value] = $this->params->language; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (isset($this->params->currency)) { |
91
|
|
|
$params[BaseURLFiltersEnum::Currency->value] = $this->params->currency; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (isset($this->params->page)) { |
95
|
|
|
$params[BaseURLFiltersEnum::Pagination->value] = BaseURLFiltersEnum::Pagination->setPage($this->params->page); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$url .= http_build_query($params); |
99
|
|
|
return $url; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return void |
104
|
|
|
* @throws BuilderNotSetParamException |
105
|
|
|
*/ |
106
|
|
|
private function checkIfAppIdNotEmpty(): void |
107
|
|
|
{ |
108
|
|
|
if (!isset($this->params->appId)) { |
109
|
|
|
throw new BuilderNotSetParamException("AppId cannot be empty"); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|