1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace GSoares\GoogleTrends\Search; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use GSoares\GoogleTrends\Error\GoogleTrendsException; |
7
|
|
|
use GSoares\GoogleTrends\Result\InterestOverTimeCollection; |
8
|
|
|
use GSoares\GoogleTrends\Result\InterestOverTimeResult; |
9
|
|
|
use GSoares\GoogleTrends\Result\ResultCollectionInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Gabriel Felipe Soares <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class InterestOverTimeSearch implements SearchInterface |
15
|
|
|
{ |
16
|
|
|
private const SEARCH_URL = 'https://trends.google.com/trends/api/widgetdata/multiline'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ExploreSearch |
20
|
|
|
*/ |
21
|
|
|
protected $exploreSearch; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var SearchRequest |
25
|
|
|
*/ |
26
|
|
|
protected $searchRequest; |
27
|
|
|
|
28
|
3 |
|
public function __construct(ExploreSearch $exploreSearch = null, SearchRequest $searchRequest = null) |
29
|
|
|
{ |
30
|
3 |
|
$this->searchRequest = $searchRequest ?: new SearchRequest(); |
31
|
3 |
|
$this->exploreSearch = $exploreSearch ?: new ExploreSearch($this->searchRequest); |
32
|
3 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param SearchFilter $searchFilter |
36
|
|
|
* |
37
|
|
|
* @return InterestOverTimeCollection |
38
|
|
|
* |
39
|
|
|
* @throws GoogleTrendsException |
40
|
|
|
*/ |
41
|
3 |
|
public function search(SearchFilter $searchFilter): ResultCollectionInterface |
42
|
|
|
{ |
43
|
3 |
|
$token = $this->exploreSearch |
44
|
3 |
|
->search($searchFilter) |
45
|
3 |
|
->getInterestOverTimeResult() |
46
|
3 |
|
->getToken(); |
47
|
|
|
|
48
|
3 |
|
$searchUrl = $this->buildQuery($searchFilter->withToken($token)); |
49
|
|
|
|
50
|
3 |
|
$responseDecoded = $this->searchRequest->search($searchUrl); |
51
|
|
|
|
52
|
3 |
|
if (!isset($responseDecoded['default']['timelineData'])) { |
53
|
1 |
|
throw new GoogleTrendsException( |
54
|
1 |
|
sprintf( |
55
|
1 |
|
'Invalid google response body "%s"...', |
56
|
1 |
|
substr(var_export($responseDecoded, true), 100) |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
$results = []; |
62
|
|
|
|
63
|
2 |
|
foreach ($responseDecoded['default']['timelineData'] ?? [] as $row) { |
64
|
2 |
|
if (!isset($row['time'], $row['value'])) { |
65
|
1 |
|
throw new GoogleTrendsException( |
66
|
1 |
|
sprintf( |
67
|
1 |
|
'Google timeline list does not contain all keys. Only has: %s', |
68
|
1 |
|
implode(', ', array_keys($row)) |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$results[] = new InterestOverTimeResult( |
74
|
1 |
|
(new DateTimeImmutable(date('Y-m-d H:i:s', (int)$row['time']))), |
75
|
1 |
|
$row['value'], |
76
|
1 |
|
(bool)($row['hasData'] ?? false) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return new InterestOverTimeCollection($searchUrl, ...$results); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
private function buildQuery(SearchFilter $searchFilter): string |
84
|
|
|
{ |
85
|
|
|
$request = [ |
86
|
3 |
|
'time' => $searchFilter->getTime(), |
87
|
3 |
|
'resolution' => 'DAY', |
88
|
3 |
|
'locale' => $searchFilter->getLanguage(), |
|
|
|
|
89
|
|
|
'comparisonItem' => [ |
90
|
|
|
[ |
91
|
|
|
'geo' => [ |
92
|
3 |
|
'country' => $searchFilter->getLocation(), |
93
|
|
|
], |
94
|
|
|
], |
95
|
|
|
], |
96
|
|
|
'requestOptions' => [ |
97
|
3 |
|
'property' => '', |
98
|
3 |
|
'backend' => 'IZG', |
99
|
3 |
|
'category' => $searchFilter->getCategory(), |
100
|
|
|
] |
101
|
|
|
]; |
102
|
|
|
|
103
|
3 |
|
if (!empty($searchFilter->getSearchTerm())) { |
104
|
3 |
|
$request['comparisonItem'][0]['complexKeywordsRestriction'] = [ |
105
|
|
|
'keyword' => [ |
106
|
|
|
[ |
107
|
3 |
|
'type' => 'BROAD', |
108
|
3 |
|
'value' => $searchFilter->getSearchTerm(), |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$query = [ |
115
|
3 |
|
'hl' => $searchFilter->getLanguage(), |
|
|
|
|
116
|
3 |
|
'tz' => '-60', |
117
|
3 |
|
'req' => json_encode($request), |
118
|
3 |
|
'token' => $searchFilter->getToken() |
119
|
|
|
]; |
120
|
|
|
|
121
|
3 |
|
$queryString = str_replace( |
122
|
|
|
[ |
123
|
3 |
|
'%3A', |
124
|
|
|
'%2C', |
125
|
|
|
'%2B' |
126
|
|
|
], |
127
|
|
|
[ |
128
|
3 |
|
':', |
129
|
|
|
',', |
130
|
|
|
'+', |
131
|
|
|
], |
132
|
3 |
|
http_build_query($query) |
133
|
|
|
); |
134
|
|
|
|
135
|
3 |
|
return self::SEARCH_URL . '?' . $queryString; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.