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 Searchable |
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
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function searchCall($query, $scope, $bookmarks = []) |
30
|
|
|
{ |
31
|
|
|
$url = UrlHelper::getSearchUrl(!empty($bookmarks)); |
32
|
|
|
$get = $this->createSearchQuery($query, $scope, $bookmarks); |
33
|
|
|
$response = $this->getRequest()->exec($url . '?' . $get); |
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
|
|
|
* |
44
|
|
|
* @return \Iterator |
45
|
|
|
*/ |
46
|
|
|
public function searchWithPagination($query, $batchesLimit) |
47
|
|
|
{ |
48
|
|
|
return (new Pagination($this))->run( |
49
|
|
|
'searchCall', [ |
50
|
|
|
'query' => $query, |
51
|
|
|
'scope' => $this->getScope(), |
52
|
|
|
], $batchesLimit |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Creates Pinterest API search request. |
58
|
|
|
* |
59
|
|
|
* @param $query |
60
|
|
|
* @param $scope |
61
|
|
|
* @param array $bookmarks |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function createSearchQuery($query, $scope, $bookmarks = []) |
66
|
|
|
{ |
67
|
|
|
$options = ['scope' => $scope, 'query' => $query]; |
68
|
|
|
$dataJson = $this->appendBookMarks($bookmarks, $options); |
69
|
|
|
|
70
|
|
|
return Request::createQuery( |
71
|
|
|
$dataJson, "/search/$scope/?q=".$query |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Search entities by search query. |
77
|
|
|
* |
78
|
|
|
* @param string $query |
79
|
|
|
* @param int $batchesLimit |
80
|
|
|
* |
81
|
|
|
* @return \Iterator |
82
|
|
|
*/ |
83
|
|
|
public function search($query, $batchesLimit = 0) |
84
|
|
|
{ |
85
|
|
|
return $this->searchWithPagination($query, $batchesLimit); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $bookmarks |
90
|
|
|
* @param $options |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
protected function appendBookMarks($bookmarks, $options) |
95
|
|
|
{ |
96
|
|
|
$dataJson = ['options' => $options]; |
97
|
|
|
if (!empty($bookmarks)) { |
98
|
|
|
$dataJson['options']['bookmarks'] = $bookmarks; |
99
|
|
|
|
100
|
|
|
return $dataJson; |
101
|
|
|
} else { |
102
|
|
|
$dataJson = array_merge( |
103
|
|
|
$dataJson, [ |
104
|
|
|
'module' => [ |
105
|
|
|
'name' => $this->moduleSearchPage, |
106
|
|
|
'options' => $options, |
107
|
|
|
], |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $dataJson; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|