1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Helpers\Providers\Traits; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
6
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlHelper; |
7
|
|
|
use seregazhuk\PinterestBot\Helpers\Pagination; |
8
|
|
|
|
9
|
|
|
trait Searchable |
10
|
|
|
{ |
11
|
|
|
use HandlesRequestAndResponse; |
12
|
|
|
|
13
|
|
|
private $moduleSearchPage = 'SearchPage'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
protected function getSearchScope() |
19
|
|
|
{ |
20
|
|
|
return property_exists($this, 'searchScope') ? $this->searchScope : ''; |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Executes search to API. Query - search string. |
25
|
|
|
* |
26
|
|
|
* @param string $query |
27
|
|
|
* @param string $scope |
28
|
|
|
* @param array $bookmarks |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function searchCall($query, $scope, $bookmarks = []) |
33
|
|
|
{ |
34
|
|
|
$url = UrlHelper::getSearchUrl($bookmarks); |
35
|
|
|
$get = $this->createSearchQuery($query, $scope, $bookmarks); |
36
|
|
|
$result = $this->getRequest()->exec($url . '?' . $get); |
37
|
|
|
|
38
|
|
|
/* |
39
|
|
|
* It was a first time search, we grab data and bookmarks for pagination. |
40
|
|
|
*/ |
41
|
|
|
if (empty($bookmarks)) { |
42
|
|
|
return $this->parseSearchResult($result); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/* |
46
|
|
|
* Process a response with bookmarks |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
return $this->getResponse()->getPaginationData($result); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Creates Pinterest API search request. |
54
|
|
|
* |
55
|
|
|
* @param string $query |
56
|
|
|
* @param string $scope |
57
|
|
|
* @param array $bookmarks |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
protected function createSearchQuery($query, $scope, $bookmarks = []) |
62
|
|
|
{ |
63
|
|
|
$options = ['scope' => $scope, 'query' => $query]; |
64
|
|
|
$dataJson = $this->appendBookMarks($bookmarks, $options); |
65
|
|
|
|
66
|
|
|
return Request::createQuery($dataJson); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Search entities by search query. |
71
|
|
|
* |
72
|
|
|
* @param string $query |
73
|
|
|
* @param int $limit |
74
|
|
|
* |
75
|
|
|
* @return \Iterator |
76
|
|
|
*/ |
77
|
|
|
public function search($query, $limit = 0) |
78
|
|
|
{ |
79
|
|
|
return (new Pagination($this))->paginateOver( |
80
|
|
|
'searchCall', [ |
81
|
|
|
'query' => $query, |
82
|
|
|
'scope' => $this->getSearchScope(), |
83
|
|
|
], $limit |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $bookmarks |
89
|
|
|
* @param array $options |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
protected function appendBookMarks($bookmarks, $options) |
94
|
|
|
{ |
95
|
|
|
$dataJson = ['options' => $options]; |
96
|
|
|
if (!empty($bookmarks)) { |
97
|
|
|
$dataJson['options']['bookmarks'] = $bookmarks; |
98
|
|
|
|
99
|
|
|
return $dataJson; |
100
|
|
|
} |
101
|
|
|
|
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
|
|
|
* Parses simple Pinterest search API response |
116
|
|
|
* on request with bookmarks. |
117
|
|
|
* |
118
|
|
|
* @param array $response |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
protected function parseSearchResult($response) |
123
|
|
|
{ |
124
|
|
|
$bookmarks = []; |
125
|
|
|
|
126
|
|
|
if (isset($response['module']['tree']['resource']['options']['bookmarks'][0])) { |
127
|
|
|
$bookmarks = $response['module']['tree']['resource']['options']['bookmarks'][0]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (!empty($response['module']['tree']['data']['results'])) { |
131
|
|
|
return ['data' => $response['module']['tree']['data']['results'], 'bookmarks' => [$bookmarks]]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return []; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: