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