Completed
Pull Request — master (#197)
by Sergey
02:57
created

Keywords::recommendedFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
7
class Keywords extends Provider
8
{
9
    /**
10
     * Get recommendations for query word. 
11
     * 
12
     * @param $query
13
     * @return array|bool
14
     */
15
    public function recommendedFor($query)
16
    {
17
        $requestOptions = [
18
            'scope' => 'pins',
19
            'query' => $query,
20
        ];
21
22
        $result = $this->execGetRequest($requestOptions, UrlBuilder::getSearchUrl());
23
24
        return $this->getKeywordsFromRequest($result);
25
    }
26
27
    /**
28
     * @param bool|array $response
29
     * @return bool|array
30
     */
31
    protected function getKeywordsFromRequest($response)
32
    {
33
        if (!isset($response['guides'])) return [];
34
35
        $keywords = $response['guides'];
36
37
        return array_map(function ($keywordData) {
38
            return [
39
                'term'     => $keywordData['term'],
40
                'display'  => $keywordData['display'],
41
                'position' => $keywordData['position'],
42
            ];
43
        }, $keywords);
44
    }
45
}