1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace GSoares\GoogleTrends\Search\Psr7; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use GSoares\GoogleTrends\Search\SearchFilter; |
7
|
|
|
use GSoares\GoogleTrends\Search\SearchInterface; |
8
|
|
|
use GuzzleHttp\Psr7\Response; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Throwable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Gabriel Felipe Soares <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Search |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var SearchInterface |
20
|
|
|
*/ |
21
|
|
|
private $search; |
22
|
|
|
|
23
|
8 |
|
public function __construct(SearchInterface $search) |
24
|
|
|
{ |
25
|
8 |
|
$this->search = $search; |
26
|
8 |
|
} |
27
|
|
|
|
28
|
8 |
|
public function search(ServerRequestInterface $request): ResponseInterface |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
8 |
|
$params = $request->getQueryParams(); |
32
|
|
|
|
33
|
8 |
|
$searchFilter = (new SearchFilter()) |
|
|
|
|
34
|
8 |
|
->withCategory((int)($params["categoryId"] ?? 0)) |
35
|
8 |
|
->withSearchTerm($params["searchTerm"] ?? 'google') |
36
|
8 |
|
->withLocation($params["location"] ?? 'US') |
37
|
8 |
|
->withinInterval( |
38
|
8 |
|
new DateTimeImmutable($params["intervalFrom"] ?? 'now -7 days'), |
39
|
8 |
|
new DateTimeImmutable($params["intervalTo"] ?? 'yesterday') |
40
|
|
|
) |
41
|
8 |
|
->withLanguage($params["language"] ?? 'en-US'); |
42
|
|
|
|
43
|
8 |
|
$this->configureSearchSource($params["searchSource"] ?? 'web', $searchFilter); |
44
|
|
|
|
45
|
8 |
|
if (filter_var($params["withTopMetrics"] ?? false, FILTER_VALIDATE_BOOLEAN)) { |
46
|
1 |
|
$searchFilter->withTopMetrics(); |
47
|
|
|
} |
48
|
|
|
|
49
|
8 |
|
if (filter_var($params["withRisingMetrics"] ?? false, FILTER_VALIDATE_BOOLEAN)) { |
50
|
1 |
|
$searchFilter->withRisingMetrics(); |
51
|
|
|
} |
52
|
|
|
|
53
|
8 |
|
return new Response( |
54
|
8 |
|
200, |
55
|
|
|
[ |
56
|
8 |
|
'Content-Type' => 'application/json', |
57
|
|
|
], |
58
|
8 |
|
json_encode($this->search->search($searchFilter)->jsonSerialize()) |
59
|
|
|
); |
60
|
2 |
|
} catch (Throwable $exception) { |
61
|
2 |
|
return new Response( |
62
|
2 |
|
400, |
63
|
|
|
[ |
64
|
2 |
|
'Content-Type' => 'application/json', |
65
|
|
|
], |
66
|
2 |
|
json_encode( |
67
|
|
|
[ |
68
|
|
|
'errors' => [ |
69
|
|
|
[ |
70
|
2 |
|
'status' => 400, |
71
|
2 |
|
'code' => $exception->getCode(), |
72
|
2 |
|
'title' => $exception->getMessage(), |
73
|
2 |
|
'details' => $exception->getMessage(), |
74
|
|
|
] |
75
|
|
|
] |
76
|
|
|
] |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
8 |
|
private function configureSearchSource(string $searchSource, SearchFilter $searchFilter): void |
83
|
|
|
{ |
84
|
8 |
|
if ($searchSource === SearchFilter::SEARCH_SOURCE_NEWS) { |
85
|
1 |
|
$searchFilter->considerNewsSearch(); |
86
|
|
|
|
87
|
1 |
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
7 |
|
if ($searchSource === SearchFilter::SEARCH_SOURCE_IMAGES) { |
91
|
1 |
|
$searchFilter->considerImageSearch(); |
92
|
|
|
|
93
|
1 |
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
6 |
|
if ($searchSource === SearchFilter::SEARCH_SOURCE_YOUTUBE) { |
97
|
2 |
|
$searchFilter->considerYoutubeSearch(); |
98
|
|
|
|
99
|
2 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
if ($searchSource === SearchFilter::SEARCH_SOURCE_GOOGLE_SHOPPING) { |
103
|
1 |
|
$searchFilter->considerGoogleShoppingSearch(); |
104
|
|
|
|
105
|
1 |
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
$searchFilter->considerWebSearch(); |
109
|
3 |
|
} |
110
|
|
|
} |
111
|
|
|
|
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.