Completed
Pull Request — master (#191)
by Sergey
02:27
created

Response::getPaginationData()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.2
cc 4
eloc 7
nc 3
nop 0
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
        if(!$data) return;
27
28
        $this->data = $data;
29
30
        $this->lastError = $this->getValueByKey('resource_response.error', $this->data);
31
32
        $this->clientInfo = $this->getValueByKey('client_context', $this->data);
33
    }
34
35
    /**
36
     * Check if specified data exists in response.
37
     *
38
     * @param null  $key
39
     *
40
     * @return array|bool
41
     */
42
    public function getResponseData($key = null)
43
    {
44
        if ($this->hasErrors()) {
45
            return false;
46
        }
47
48
        return $this->parseResponseData($key);
49
    }
50
51
    /**
52
     * @param string $key
53
     * @param null $default
54
     * @return mixed
55
     */
56
    public function getData($key = '', $default = null)
57
    {
58
        return $this->getValueByKey($key, $this->data, $default);
59
    }
60
61
    /**
62
     * @param string $key
63
     * @return bool
64
     */
65
    public function hasData($key = '')
66
    {
67
        return !is_null($this->getValueByKey($key, $this->data));
68
    }
69
70
    /**
71
     * Parse data from Pinterest Api response.
72
     * Data is stored in ['resource_response']['data'] array.
73
     *
74
     * @param string $key
75
     *
76
     * @return bool|array
77
     */
78
    protected function parseResponseData($key)
79
    {
80
        $responseData = $this->getValueByKey('resource_response.data', $this->data);
81
        if(!$responseData) return false;
82
83
        return $key ?
84
            $this->getValueByKey($key, $responseData) :
85
            $responseData;
86
    }
87
88
    /**
89
     * @param string $key
90
     * @param array $data
91
     * @param mixed $default
92
     * @return array|bool|mixed
93
     */
94
    protected function getValueByKey($key = '', $data, $default = null)
95
    {
96
        if(!is_array($data)) return null;
97
98
        if(empty($key)) return $data;
99
100
        $indexes = explode('.', $key);
101
        $value = $data;
102
103
        foreach ($indexes as $index) {
104
            if(!isset($value[$index])) return $default;
105
106
            $value = $value[$index];
107
        }
108
109
        return $value;
110
    }
111
112
    /**
113
     * Checks if response is empty.
114
     *
115
     * @return bool
116
     */
117
    public function isEmpty()
118
    {
119
        return empty($this->getResponseData());
120
    }
121
122
    /**
123
     * @return bool
124
     */
125
    public function isOk()
126
    {
127
        return !$this->hasErrors() && !$this->isEmpty();
128
    }
129
130
    /**
131
     * @return bool
132
     */
133
    public function hasResponseData()
134
    {
135
        return (bool)$this->getValueByKey('resource_response.data', $this->data);
136
    }
137
138
    /**
139
     * Remove 'module' data from response.
140
     *
141
     * @return array mixed
142
     */
143
    public function clearResponseFromMetaData()
144
    {
145
        if (isset($this->data['data'][0]['type']) && $this->data['data'][0]['type'] == 'module') {
146
            array_shift($this->data['data']);
147
        }
148
149
        return $this->data;
150
    }
151
152
    /**
153
     * Check for error info in api response and save
154
     * it.
155
     *
156
     * @return bool
157
     */
158
    public function hasErrors()
159
    {
160
        return !is_null($this->lastError);
161
    }
162
163
    /**
164
     * Parse bookmarks from response.
165
     *
166
     * @return array
167
     */
168
    public function getBookmarks()
169
    {
170
        $bookmarks = $this->getValueByKey('resource.options.bookmarks', $this->data,  []);
171
        return empty($bookmarks) ? [] : [$bookmarks[0]];
172
    }
173
174
    /**
175
     * Checks Pinterest API paginated response, and parses data
176
     * with bookmarks info from it.
177
     *
178
     * @return array
179
     */
180
    public function getPaginationData()
181
    {
182
        if ($this->isEmpty() && $this->hasErrors()) {
183
            return [];
184
        }
185
186
        $bookmarks = $this->getBookmarks();
187
        if ($data = $this->getResponseData()) {
188
            return ['data' => $data, 'bookmarks' => $bookmarks];
189
        }
190
191
        return [];
192
    }
193
194
    /**
195
     * @return array
196
     */
197
    public function getLastError()
198
    {
199
        return $this->lastError;
200
    }
201
202
    /**
203
     * @return array|null
204
     */
205
    public function getClientInfo()
206
    {
207
        return $this->clientInfo;
208
    }
209
210
    /**
211
     * @return string
212
     */
213
    public function __toString()
214
    {
215
        return $this->data;
216
    }
217
}
218