1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
6
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlHelper; |
7
|
|
|
use seregazhuk\PinterestBot\Helpers\PaginationHelper; |
8
|
|
|
|
9
|
|
|
abstract class SearchProvider extends Provider |
10
|
|
|
{ |
11
|
|
|
use PaginationHelper; |
12
|
|
|
protected $moduleSearchPage = "SearchPage"; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
abstract protected function getScope(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Executes search to API. Query - search string. |
21
|
|
|
* |
22
|
|
|
* @param string $query |
23
|
|
|
* @param string $scope |
24
|
|
|
* @param array $bookmarks |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function searchCall($query, $scope, $bookmarks = []) |
28
|
|
|
{ |
29
|
|
|
$url = UrlHelper::getSearchUrl(! empty($bookmarks)); |
30
|
|
|
$get = $this->createSearchRequest($query, $scope, $bookmarks); |
31
|
|
|
$url = $url.'?'.UrlHelper::buildRequestString($get); |
32
|
|
|
$response = $this->request->exec($url); |
33
|
|
|
|
34
|
|
|
return $this->response->parseSearchResponse($response, ! empty($bookmarks)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Executes search to API with pagination. |
39
|
|
|
* |
40
|
|
|
* @param string $query |
41
|
|
|
* @param int $batchesLimit |
42
|
|
|
* @return \Iterator |
43
|
|
|
*/ |
44
|
|
|
public function searchWithPagination($query, $batchesLimit) |
45
|
|
|
{ |
46
|
|
|
return $this->getPaginatedData( |
47
|
|
|
[$this, 'searchCall'], [ |
48
|
|
|
'query' => $query, |
49
|
|
|
'scope' => $this->getScope(), |
50
|
|
|
], $batchesLimit |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Creates Pinterest API search request |
56
|
|
|
* |
57
|
|
|
* @param $query |
58
|
|
|
* @param $scope |
59
|
|
|
* @param array $bookmarks |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function createSearchRequest($query, $scope, $bookmarks = []) |
63
|
|
|
{ |
64
|
|
|
$options = [ |
65
|
|
|
"scope" => $scope, |
66
|
|
|
"query" => $query, |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
$dataJson = ["options" => $options]; |
70
|
|
|
|
71
|
|
|
if ( ! empty($bookmarks)) { |
72
|
|
|
$dataJson['options']['bookmarks'] = $bookmarks; |
73
|
|
|
} else { |
74
|
|
|
$dataJson = array_merge( |
75
|
|
|
$dataJson, [ |
76
|
|
|
'module' => [ |
77
|
|
|
"name" => $this->moduleSearchPage, |
78
|
|
|
"options" => $options, |
79
|
|
|
], |
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return Request::createRequestData( |
85
|
|
|
$dataJson, "/search/$scope/?q=".$query |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Search entities by search query |
91
|
|
|
* |
92
|
|
|
* @param string $query |
93
|
|
|
* @param int $batchesLimit |
94
|
|
|
* @return \Iterator |
95
|
|
|
*/ |
96
|
|
|
public function search($query, $batchesLimit = 0) |
97
|
|
|
{ |
98
|
|
|
return $this->searchWithPagination($query, $batchesLimit); |
99
|
|
|
} |
100
|
|
|
} |