|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace GSoares\GoogleTrends\Search; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
|
6
|
|
|
use GSoares\GoogleTrends\Error\GoogleTrendsException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Gabriel Felipe Soares <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class SearchFilter |
|
12
|
|
|
{ |
|
13
|
|
|
private const DEFAULT_LANG = 'en-US'; |
|
14
|
|
|
private const DEFAULT_COUNTRY = 'US'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $token; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $location; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $language; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
private $category; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $searchTerm; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string[] |
|
43
|
|
|
*/ |
|
44
|
|
|
private $metrics; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $time; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $compareTime; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
private $searchType; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
private $keywordType; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var DateTimeImmutable |
|
68
|
|
|
*/ |
|
69
|
|
|
private $currentDate; |
|
70
|
|
|
|
|
71
|
33 |
|
public function __construct(DateTimeImmutable $currentDate = null) |
|
72
|
|
|
{ |
|
73
|
33 |
|
$this->metrics = []; |
|
74
|
33 |
|
$this->searchTerm = ''; |
|
75
|
33 |
|
$this->category = 0; |
|
76
|
33 |
|
$this->currentDate = $currentDate ?? new DateTimeImmutable(); |
|
77
|
|
|
|
|
78
|
33 |
|
$this->withinInterval($this->currentDate->modify('-1 month'), $this->currentDate) |
|
79
|
33 |
|
->withLanguage(self::DEFAULT_LANG) |
|
80
|
33 |
|
->withLocation(self::DEFAULT_COUNTRY) |
|
81
|
33 |
|
->considerWebSearch(); |
|
82
|
33 |
|
} |
|
83
|
|
|
|
|
84
|
15 |
|
public function withToken(string $token): self |
|
85
|
|
|
{ |
|
86
|
15 |
|
$this->token = $token; |
|
87
|
|
|
|
|
88
|
15 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param DateTimeImmutable $from |
|
93
|
|
|
* @param DateTimeImmutable $to |
|
94
|
|
|
* |
|
95
|
|
|
* @return $this |
|
96
|
|
|
* |
|
97
|
|
|
* @throws GoogleTrendsException |
|
98
|
|
|
*/ |
|
99
|
33 |
|
public function withinInterval(DateTimeImmutable $from, DateTimeImmutable $to): self |
|
100
|
|
|
{ |
|
101
|
33 |
|
if ($from >= $to || $from->format('Ymd') === $to->format('Ymd')) { |
|
102
|
2 |
|
throw new GoogleTrendsException( |
|
103
|
2 |
|
sprintf( |
|
104
|
2 |
|
'Invalid interval. From %s to %s', |
|
105
|
2 |
|
$from->format(DATE_ATOM), |
|
106
|
2 |
|
$to->format(DATE_ATOM) |
|
107
|
|
|
) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
33 |
|
$from = $from->setTime(0, 0, 0); |
|
112
|
33 |
|
$to = $to->setTime(23, 59, 50); |
|
113
|
|
|
|
|
114
|
33 |
|
$this->time = $from->format('Y-m-d') . ' ' . $to->format('Y-m-d'); |
|
115
|
|
|
|
|
116
|
33 |
|
$daysDifference = (int)ceil(($to->getTimestamp() - $from->getTimestamp()) / 60 / 60 / 24); |
|
117
|
|
|
|
|
118
|
33 |
|
$this->compareTime = $from->modify('-' . $daysDifference . ' days') |
|
119
|
33 |
|
->format('Y-m-d') |
|
120
|
33 |
|
. ' ' |
|
121
|
33 |
|
. $to->modify('-' . $daysDifference . ' days') |
|
122
|
33 |
|
->format('Y-m-d'); |
|
123
|
|
|
|
|
124
|
33 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
public function considerImageSearch(): self |
|
128
|
|
|
{ |
|
129
|
1 |
|
$this->searchType = 'images'; |
|
130
|
|
|
|
|
131
|
1 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
public function considerGoogleShoppingSearch(): self |
|
135
|
|
|
{ |
|
136
|
1 |
|
$this->searchType = 'frgoogle'; |
|
137
|
|
|
|
|
138
|
1 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
public function considerYoutubeSearch(): self |
|
142
|
|
|
{ |
|
143
|
1 |
|
$this->searchType = 'youtube'; |
|
144
|
|
|
|
|
145
|
1 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
public function considerNewsSearch(): self |
|
149
|
|
|
{ |
|
150
|
1 |
|
$this->searchType = 'news'; |
|
151
|
|
|
|
|
152
|
1 |
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
33 |
|
public function considerWebSearch(): self |
|
156
|
|
|
{ |
|
157
|
33 |
|
$this->searchType = ''; |
|
158
|
|
|
|
|
159
|
33 |
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
9 |
|
public function withRisingMetrics(): self |
|
163
|
|
|
{ |
|
164
|
9 |
|
$this->metrics[] = 'RISING'; |
|
165
|
|
|
|
|
166
|
9 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
9 |
|
public function withTopMetrics(): self |
|
170
|
|
|
{ |
|
171
|
9 |
|
$this->metrics[] = 'TOP'; |
|
172
|
|
|
|
|
173
|
9 |
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
33 |
|
public function withLanguage(string $language): self |
|
177
|
|
|
{ |
|
178
|
33 |
|
$this->language = $language; |
|
179
|
|
|
|
|
180
|
33 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
public function withCategory(int $category): self |
|
184
|
|
|
{ |
|
185
|
1 |
|
$this->category = $category; |
|
186
|
|
|
|
|
187
|
1 |
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
33 |
|
public function withLocation(string $location): self |
|
191
|
|
|
{ |
|
192
|
33 |
|
$this->location = $location; |
|
193
|
|
|
|
|
194
|
33 |
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
1 |
|
public function withSearchTerm(string $searchTerm): self |
|
198
|
|
|
{ |
|
199
|
1 |
|
$this->searchTerm = $searchTerm; |
|
200
|
|
|
|
|
201
|
1 |
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
15 |
|
public function getToken(): string |
|
205
|
|
|
{ |
|
206
|
15 |
|
return (string)$this->token; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
18 |
|
public function getLocation(): string |
|
210
|
|
|
{ |
|
211
|
18 |
|
return $this->location; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
18 |
|
public function getLanguage(): string |
|
215
|
|
|
{ |
|
216
|
18 |
|
return $this->language; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
18 |
|
public function getCategory(): int |
|
220
|
|
|
{ |
|
221
|
18 |
|
return $this->category; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
18 |
|
public function getSearchTerm(): string |
|
225
|
|
|
{ |
|
226
|
18 |
|
return $this->searchTerm; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
16 |
|
public function getSearchType(): string |
|
230
|
|
|
{ |
|
231
|
16 |
|
return $this->searchType; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
18 |
|
public function getTime(): string |
|
235
|
|
|
{ |
|
236
|
18 |
|
return $this->time; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
10 |
|
public function getCompareTime(): string |
|
240
|
|
|
{ |
|
241
|
10 |
|
return $this->compareTime; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
2 |
|
public function getMetrics(): array |
|
245
|
|
|
{ |
|
246
|
2 |
|
return $this->metrics; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
7 |
|
public function isConsideringTopMetrics(): bool |
|
250
|
|
|
{ |
|
251
|
7 |
|
return in_array('TOP', $this->metrics); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
9 |
|
public function isConsideringRisingMetrics(): bool |
|
255
|
|
|
{ |
|
256
|
9 |
|
return in_array('RISING', $this->metrics); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|