Completed
Pull Request — master (#223)
by Sergey
02:23
created

Response::parseResponseData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse;
6
7
class Response implements PaginatedResponse
8
{
9
    /**
10
     * @var mixed
11
     */
12
    protected $data = [];
13
14
    /**
15
     * @var array|null
16
     */
17
    protected $lastError;
18
19
    /**
20
     * @var array|null
21
     */
22
    protected $clientInfo;
23
24
    public function fill($data)
25
    {
26
        $this->data = $data;
27
28
        $this->lastError = $this->getValueByKey('resource_response.error', $this->data);
29
30
        $this->clientInfo = $this->getValueByKey('client_context', $this->data);
31
    }
32
33
    /**
34
     * Check if specified data exists in response.
35
     *
36
     * @param null  $key
37
     *
38
     * @return array|bool
39
     */
40
    public function getResponseData($key = null)
41
    {
42
        if ($this->hasErrors()) {
43
            return false;
44
        }
45
46
        return $this->parseResponseData($key);
47
    }
48
49
    /**
50
     * @param string $key
51
     * @param null $default
52
     * @return mixed
53
     */
54
    public function getData($key = '', $default = null)
55
    {
56
        return $this->getValueByKey($key, $this->data, $default);
57
    }
58
59
    /**
60
     * @param string $key
61
     * @return bool
62
     */
63
    public function hasData($key = '')
64
    {
65
        return !is_null($this->getValueByKey($key, $this->data));
66
    }
67
68
    /**
69
     * Parse data from Pinterest Api response.
70
     * Data is stored in ['resource_response']['data'] array.
71
     *
72
     * @param string $key
73
     *
74
     * @return bool|array
75
     */
76
    protected function parseResponseData($key)
77
    {
78
        $responseData = $this->getValueByKey('resource_response.data', $this->data);
79
        if(!$responseData) return false;
80
81
        return $key ?
82
            $this->getValueByKey($key, $responseData) :
83
            $responseData;
84
    }
85
86
    /**
87
     * @param string $key
88
     * @param array $data
89
     * @param mixed $default
90
     * @return array|bool|mixed
91
     */
92
    protected function getValueByKey($key = '', $data, $default = null)
93
    {
94
        if(empty($key)) return $data;
95
96
        $indexes = explode('.', $key);
97
        $value = $data;
98
99
        foreach ($indexes as $index) {
100
            if(!isset($value[$index])) return $default;
101
102
            $value = $value[$index];
103
        }
104
105
        return $value;
106
    }
107
108
    /**
109
     * Checks if response is empty.
110
     *
111
     * @return bool
112
     */
113
    public function isEmpty()
114
    {
115
        return empty($this->getResponseData());
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    public function isOk()
122
    {
123
        return !$this->hasErrors() && !$this->isEmpty();
124
    }
125
126
    /**
127
     * Check for error info in api response and save
128
     * it.
129
     *
130
     * @return bool
131
     */
132
    public function hasErrors()
133
    {
134
        return !is_null($this->lastError);
135
    }
136
137
    /**
138
     * Parse bookmarks from response.
139
     *
140
     * @return array
141
     */
142
    public function getBookmarks()
143
    {
144
        $bookmarks = $this->getValueByKey('resource.options.bookmarks', $this->data,  []);
145
        return empty($bookmarks) ? [] : [$bookmarks[0]];
146
    }
147
148
    /**
149
     * Checks Pinterest API paginated response, and parses data
150
     * with bookmarks info from it.
151
     *
152
     * @return array
153
     */
154
    public function getPaginationData()
155
    {
156
        if (!$this->isOk()) return [];
157
158
        $bookmarks = $this->getBookmarks();
159
        if ($data = $this->getResponseData()) {
160
            return ['data' => $data, 'bookmarks' => $bookmarks];
161
        }
162
163
        return [];
164
    }
165
166
    /**
167
     * @return array
168
     */
169
    public function getLastError()
170
    {
171
        return $this->lastError;
172
    }
173
174
    /**
175
     * @return array|null
176
     */
177
    public function getClientInfo()
178
    {
179
        return $this->clientInfo;
180
    }
181
}
182