1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Radowoj\Searcher\SearchProvider;
|
4
|
|
|
|
5
|
|
|
use stdClass;
|
6
|
|
|
use Exception;
|
7
|
|
|
|
8
|
|
|
use GuzzleHttp\Client as GuzzleClient;
|
9
|
|
|
|
10
|
|
|
use Radowoj\Searcher\SearchResult\Collection;
|
11
|
|
|
use Radowoj\Searcher\SearchResult\ICollection;
|
12
|
|
|
use Radowoj\Searcher\SearchResult\Item;
|
13
|
|
|
use Radowoj\Searcher\SearchResult\IItem;
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class Google extends SearchProvider implements ISearchProvider
|
17
|
|
|
{
|
18
|
|
|
const URI = 'https://www.googleapis.com/customsearch/v1?';
|
19
|
|
|
|
20
|
|
|
protected $apiKey = null;
|
21
|
|
|
|
22
|
|
|
protected $cx = null;
|
23
|
|
|
|
24
|
|
|
protected $guzzle = null;
|
25
|
|
|
|
26
|
7 |
|
public function __construct(GuzzleClient $guzzle, string $apiKey, string $cx)
|
27
|
|
|
{
|
28
|
7 |
|
$this->apiKey = $apiKey;
|
29
|
7 |
|
$this->cx = $cx;
|
30
|
7 |
|
$this->guzzle = $guzzle;
|
31
|
7 |
|
}
|
32
|
|
|
|
33
|
|
|
|
34
|
6 |
|
protected function getSearchQueryString(string $query, int $limit, int $offset)
|
|
|
|
|
35
|
|
|
{
|
36
|
|
|
$params = [
|
37
|
6 |
|
'key' => $this->apiKey,
|
38
|
6 |
|
'q' => urlencode($query),
|
39
|
6 |
|
'cx' => $this->cx,
|
40
|
|
|
];
|
41
|
|
|
|
42
|
6 |
|
if ($offset) {
|
43
|
1 |
|
$params['start'] = $offset;
|
44
|
|
|
}
|
45
|
|
|
|
46
|
6 |
|
$paramsMerged = [];
|
47
|
|
|
|
48
|
6 |
|
foreach($params as $key => $value) {
|
49
|
6 |
|
$paramsMerged[] = "{$key}={$value}";
|
50
|
|
|
}
|
51
|
|
|
|
52
|
6 |
|
return implode('&', $paramsMerged);
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
|
56
|
6 |
|
protected function searchRequest(string $query, int $limit, int $offset) : stdClass
|
57
|
|
|
{
|
58
|
6 |
|
$uri = self::URI . $this->getSearchQueryString($query, $limit, $offset);
|
59
|
|
|
|
60
|
6 |
|
$result = $this->guzzle->request('GET', $uri);
|
61
|
6 |
|
return json_decode($result->getBody());
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
|
65
|
5 |
|
protected function enforceLimit(stdClass $result, int $limit) : stdClass
|
66
|
|
|
{
|
67
|
5 |
|
if (!isset($result->items)) {
|
68
|
1 |
|
return $result;
|
69
|
|
|
}
|
70
|
4 |
|
$result->items = array_slice($result->items, 0, $limit);
|
71
|
4 |
|
return $result;
|
72
|
|
|
}
|
73
|
|
|
|
74
|
|
|
|
75
|
6 |
|
protected function validateRequestResult(stdClass $result)
|
76
|
|
|
{
|
77
|
6 |
|
if (!isset($result->kind) || $result->kind !== "customsearch#search") {
|
78
|
1 |
|
throw new Exception("Invalid Google API response: " . print_r($result, 1));
|
79
|
|
|
}
|
80
|
5 |
|
}
|
81
|
|
|
|
82
|
|
|
|
83
|
5 |
|
protected function extractResults(stdClass $result) : array
|
84
|
|
|
{
|
85
|
5 |
|
return isset($result->items)
|
86
|
4 |
|
? $result->items
|
87
|
5 |
|
: [];
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
|
91
|
5 |
|
protected function extractTotalMatches(stdClass $result) : int
|
92
|
|
|
{
|
93
|
5 |
|
return isset($result->searchInformation->totalResults)
|
94
|
4 |
|
? $result->searchInformation->totalResults
|
95
|
5 |
|
: 0;
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
|
99
|
1 |
|
protected function populateItem(stdClass $item) : IItem
|
100
|
|
|
{
|
101
|
1 |
|
return new Item([
|
102
|
1 |
|
'url' => $item->link,
|
103
|
1 |
|
'title' => $item->title,
|
104
|
1 |
|
'description' => $item->snippet,
|
105
|
|
|
]);
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
}
|
109
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.