Completed
Pull Request — master (#142)
by Sergey
03:08
created

Searchable::parseSearchResult()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Helpers\UrlHelper;
7
use seregazhuk\PinterestBot\Api\SearchResponse;
8
use seregazhuk\PinterestBot\Helpers\Pagination;
9
10
/**
11
 * Trait Searchable
12
 * @package seregazhuk\PinterestBot\Api\Traits
13
 *
14
 * @property string $searchScope
15
 */
16
trait Searchable
17
{
18
    use HandlesRequest;
19
    
20
    private $moduleSearchPage = 'SearchPage';
21
22
    /**
23
     * @return string
24
     */
25
    protected function getSearchScope()
26
    {
27
        return property_exists($this, 'searchScope') ? $this->searchScope : '';
28
    }
29
30
    /**
31
     * Executes search to API. Query - search string.
32
     *
33
     * @param string $query
34
     * @param string $scope
35
     * @param array  $bookmarks
36
     *
37
     * @return SearchResponse
38
     */
39
    public function searchCall($query, $scope, $bookmarks = [])
40
    {
41
        $url = UrlHelper::getSearchUrl($bookmarks);
42
        $get = $this->createSearchQuery($query, $scope, $bookmarks);
43
        $response = $this->getRequest()->exec($url . '?' . $get);
44
45
        return new SearchResponse($response);
46
    }
47
48
    /**
49
     * Creates Pinterest API search request.
50
     *
51
     * @param string $query
52
     * @param string $scope
53
     * @param array $bookmarks
54
     *
55
     * @return array
56
     */
57
    protected function createSearchQuery($query, $scope, $bookmarks = [])
58
    {
59
        $dataJson = $this->appendBookMarks(
60
            $bookmarks,
61
            [
62
                'scope' => $scope,
63
                'query' => $query
64
            ]
65
        );
66
67
        $request = Request::createRequestData($dataJson, $bookmarks);
68
69
        return UrlHelper::buildRequestString($request);
70
    }
71
72
    /**
73
     * Search entities by search query.
74
     *
75
     * @param string $query
76
     * @param int $limit
77
     *
78
     * @return \Iterator
79
     */
80
    public function search($query, $limit = 0)
81
    {
82
        return (new Pagination($this))->paginateOver(
83
            'searchCall',
84
            [
85
                'query' => $query,
86
                'scope' => $this->getSearchScope(),
87
            ],
88
            $limit
89
        );
90
    }
91
92
    /**
93
     * @param array $bookmarks
94
     * @param array $options
95
     *
96
     * @return array
97
     */
98
    protected function appendBookMarks($bookmarks, $options)
99
    {
100
        $dataJson = ['options' => $options];
101
        if (!empty($bookmarks)) {
102
            $dataJson['options']['bookmarks'] = $bookmarks;
103
104
            return $dataJson;
105
        } else {
106
            $dataJson = array_merge(
107
                $dataJson, [
108
                    'module' => [
109
                        "name"    => $this->moduleSearchPage,
110
                        "options" => $options,
111
                    ],
112
                ]
113
            );
114
115
            return $dataJson;
116
        }
117
    }
118
}
119