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\URLFiltersEnum; |
12
|
|
|
use SteamMarketProviders\ParserManager\Enum\URLPaginationEnum; |
13
|
|
|
use SteamMarketProviders\ParserManager\Exception\BuilderNotSetParamException; |
14
|
|
|
use SteamMarketProviders\ParserManager\Exception\InvalidArgumentException; |
15
|
|
|
use SteamMarketProviders\ParserManager\Contract\UrlBuilderInterface; |
16
|
|
|
use stdClass; |
17
|
|
|
|
18
|
|
|
class SearchUrlBuilder implements UrlBuilderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var stdClass |
22
|
|
|
*/ |
23
|
|
|
protected stdClass $params; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
protected function reset(): void |
29
|
|
|
{ |
30
|
|
|
$this->params = new stdClass(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param SteamApp $steamApp |
35
|
|
|
* @return UrlBuilderInterface |
36
|
|
|
*/ |
37
|
|
|
public function setAppId(SteamApp $steamApp): UrlBuilderInterface |
38
|
|
|
{ |
39
|
|
|
$this->reset(); |
40
|
|
|
$this->params->appId = $steamApp->value; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @throws BuilderNotSetParamException |
47
|
|
|
*/ |
48
|
|
|
public function setLanguage(SteamLanguage $steamLanguage = SteamLanguage::English): UrlBuilderInterface |
49
|
|
|
{ |
50
|
|
|
$this->checkIfAppIdNotEmpty(); |
51
|
|
|
$this->params->language = $steamLanguage->value; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param SteamCurrency $steamCurrency |
58
|
|
|
* @return UrlBuilderInterface |
59
|
|
|
* @throws BuilderNotSetParamException |
60
|
|
|
*/ |
61
|
|
|
public function setCurrency(SteamCurrency $steamCurrency = SteamCurrency::USD): UrlBuilderInterface |
62
|
|
|
{ |
63
|
|
|
$this->checkIfAppIdNotEmpty(); |
64
|
|
|
$this->params->currency = $steamCurrency->value; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws BuilderNotSetParamException |
71
|
|
|
*/ |
72
|
|
|
public function setPage(int $page = 1): UrlBuilderInterface |
73
|
|
|
{ |
74
|
|
|
$this->checkIfAppIdNotEmpty(); |
75
|
|
|
$this->params->page = $page; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int $count |
82
|
|
|
* @return UrlBuilderInterface |
83
|
|
|
* @throws BuilderNotSetParamException |
84
|
|
|
*/ |
85
|
|
|
public function setCount(int $count = 10): UrlBuilderInterface |
86
|
|
|
{ |
87
|
|
|
$this->checkIfAppIdNotEmpty(); |
88
|
|
|
$this->params->count = $count; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
* @throws InvalidArgumentException |
96
|
|
|
*/ |
97
|
|
|
public function build(): string |
98
|
|
|
{ |
99
|
|
|
$url = SteamConfigEnum::MarketSearchUrl->value; |
100
|
|
|
|
101
|
|
|
$params = []; |
102
|
|
|
|
103
|
|
|
$params[URLFiltersEnum::AppId->value] = $this->params->appId; |
104
|
|
|
|
105
|
|
|
if (isset($this->params->language)) { |
106
|
|
|
$params[URLFiltersEnum::Language->value] = $this->params->language; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (isset($this->params->currency)) { |
110
|
|
|
$params[URLFiltersEnum::Currency->value] = $this->params->currency; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (isset($this->params->page) && isset($this->params->count)) { |
114
|
|
|
$params[] = URLPaginationEnum::Start->paginate($this->params->page, $this->params->count); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$url .= '?'; |
118
|
|
|
$url .= http_build_query($params); |
119
|
|
|
|
120
|
|
|
return $url; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return void |
125
|
|
|
* @throws BuilderNotSetParamException |
126
|
|
|
*/ |
127
|
|
|
private function checkIfAppIdNotEmpty(): void |
128
|
|
|
{ |
129
|
|
|
if (!isset($this->params->appId)) { |
130
|
|
|
throw new BuilderNotSetParamException("AppId cannot be empty"); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|