|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
class SearchHelper extends RequestHelper |
|
6
|
|
|
{ |
|
7
|
|
|
const MODULE_SEARCH_PAGE = "SearchPage"; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Creates Pinterest API search request |
|
11
|
|
|
* |
|
12
|
|
|
* @param $query |
|
13
|
|
|
* @param $scope |
|
14
|
|
|
* @param array $bookmarks |
|
15
|
|
|
* @return array |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function createSearchRequest($query, $scope, $bookmarks = []) |
|
18
|
|
|
{ |
|
19
|
|
|
$options = [ |
|
20
|
|
|
"scope" => $scope, |
|
21
|
|
|
"query" => $query, |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
$dataJson = ["options" => $options]; |
|
25
|
|
|
|
|
26
|
|
|
if ( ! empty($bookmarks)) { |
|
27
|
|
|
$dataJson['options']['bookmarks'] = $bookmarks; |
|
28
|
|
|
} else { |
|
29
|
|
|
$dataJson = array_merge( |
|
30
|
|
|
$dataJson, [ |
|
31
|
|
|
'module' => [ |
|
32
|
|
|
"name" => self::MODULE_SEARCH_PAGE, |
|
33
|
|
|
"options" => $options, |
|
34
|
|
|
], |
|
35
|
|
|
] |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return self::createRequestData( |
|
40
|
|
|
$dataJson, "/search/$scope/?q=" . $query |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Parses Pinterest search API response for data and bookmarks |
|
46
|
|
|
* for next pagination page |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $res |
|
49
|
|
|
* @param bool $bookmarksUsed |
|
50
|
|
|
* @return array|null |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function parseSearchResponse($res, $bookmarksUsed) |
|
53
|
|
|
{ |
|
54
|
|
|
if ($res === null || ! $bookmarksUsed) { |
|
55
|
|
|
return self::parseSimpledSearchResponse($res); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return self::parsePaginatedResponse($res); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Parses simple Pinterest search API response |
|
63
|
|
|
* on request with bookmarks |
|
64
|
|
|
* |
|
65
|
|
|
* @param $res |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function parseSimpledSearchResponse($res) |
|
69
|
|
|
{ |
|
70
|
|
|
$bookmarks = []; |
|
71
|
|
|
|
|
72
|
|
|
if (isset($res['module']['tree']['resource']['options']['bookmarks'][0])) { |
|
73
|
|
|
$bookmarks = $res['module']['tree']['resource']['options']['bookmarks'][0]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ( ! empty($res['module']['tree']['data']['results'])) { |
|
77
|
|
|
return ['data' => $res['module']['tree']['data']['results'], 'bookmarks' => [$bookmarks]]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return []; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|