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
|
|
|
private $moduleSearchPage = 'SearchPage'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
abstract protected function getScope(); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Executes search to API. Query - search string. |
20
|
|
|
* |
21
|
|
|
* @param string $query |
22
|
|
|
* @param string $scope |
23
|
|
|
* @param array $bookmarks |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function searchCall($query, $scope, $bookmarks = []) |
28
|
|
|
{ |
29
|
|
|
$url = UrlHelper::getSearchUrl($bookmarks); |
30
|
|
|
$get = $this->createSearchQuery($query, $scope, $bookmarks); |
31
|
|
|
$result = $this->getRequest()->exec($url . '?' . $get); |
|
|
|
|
32
|
|
|
|
33
|
|
|
/* |
34
|
|
|
* It was a first time search, we grab data and bookmarks for pagination. |
35
|
|
|
*/ |
36
|
|
|
if (empty($bookmarks)) { |
37
|
|
|
return $this->parseSearchResult($result); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/* |
41
|
|
|
* Process a response with bookmarks |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
return $this->getResponse()->getPaginationData($result); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Creates Pinterest API search request. |
49
|
|
|
* |
50
|
|
|
* @param $query |
51
|
|
|
* @param $scope |
52
|
|
|
* @param array $bookmarks |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
protected function createSearchQuery($query, $scope, $bookmarks = []) |
57
|
|
|
{ |
58
|
|
|
$options = ['scope' => $scope, 'query' => $query]; |
59
|
|
|
$dataJson = $this->appendBookMarks($bookmarks, $options); |
60
|
|
|
|
61
|
|
|
return Request::createQuery($dataJson); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Search entities by search query. |
66
|
|
|
* |
67
|
|
|
* @param string $query |
68
|
|
|
* @param int $limit |
69
|
|
|
* |
70
|
|
|
* @return \Iterator |
71
|
|
|
*/ |
72
|
|
|
public function search($query, $limit = 0) |
73
|
|
|
{ |
74
|
|
|
return (new Pagination($this))->paginateOver( |
75
|
|
|
'searchCall', [ |
76
|
|
|
'query' => $query, |
77
|
|
|
'scope' => $this->getScope(), |
78
|
|
|
], $limit |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $bookmarks |
84
|
|
|
* @param $options |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
protected function appendBookMarks($bookmarks, $options) |
89
|
|
|
{ |
90
|
|
|
$dataJson = ['options' => $options]; |
91
|
|
|
if (!empty($bookmarks)) { |
92
|
|
|
$dataJson['options']['bookmarks'] = $bookmarks; |
93
|
|
|
|
94
|
|
|
return $dataJson; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$dataJson = array_merge( |
98
|
|
|
$dataJson, [ |
99
|
|
|
'module' => [ |
100
|
|
|
'name' => $this->moduleSearchPage, |
101
|
|
|
'options' => $options, |
102
|
|
|
], |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
return $dataJson; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Parses simple Pinterest search API response |
111
|
|
|
* on request with bookmarks. |
112
|
|
|
* |
113
|
|
|
* @param array $response |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
protected function parseSearchResult($response) |
118
|
|
|
{ |
119
|
|
|
$bookmarks = []; |
120
|
|
|
|
121
|
|
|
if (isset($response['module']['tree']['resource']['options']['bookmarks'][0])) { |
122
|
|
|
$bookmarks = $response['module']['tree']['resource']['options']['bookmarks'][0]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!empty($response['module']['tree']['data']['results'])) { |
126
|
|
|
return ['data' => $response['module']['tree']['data']['results'], 'bookmarks' => [$bookmarks]]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return []; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.