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

Response::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
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(!is_array($data)) return null;
95
96
        if(empty($key)) return $data;
97
98
        $indexes = explode('.', $key);
99
        $value = $data;
100
101
        foreach ($indexes as $index) {
102
            if(!isset($value[$index])) return $default;
103
104
            $value = $value[$index];
105
        }
106
107
        return $value;
108
    }
109
110
    /**
111
     * Checks if response is empty.
112
     *
113
     * @return bool
114
     */
115
    public function isEmpty()
116
    {
117
        return empty($this->getResponseData());
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function isOk()
124
    {
125
        return !$this->hasErrors() && !$this->isEmpty();
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public function hasResponseData()
132
    {
133
        return (bool)$this->getValueByKey('resource_response.data', $this->data);
134
    }
135
136
    /**
137
     * Remove 'module' data from response.
138
     *
139
     * @return array mixed
140
     */
141
    public function clearResponseFromMetaData()
142
    {
143
        if (isset($this->data['data'][0]['type']) && $this->data['data'][0]['type'] == 'module') {
144
            array_shift($this->data['data']);
145
        }
146
147
        return $this->data;
148
    }
149
150
    /**
151
     * Check for error info in api response and save
152
     * it.
153
     *
154
     * @return bool
155
     */
156
    public function hasErrors()
157
    {
158
        return !is_null($this->lastError);
159
    }
160
161
    /**
162
     * Parse bookmarks from response.
163
     *
164
     * @return array
165
     */
166
    public function getBookmarks()
167
    {
168
        $bookmarks = $this->getValueByKey('resource.options.bookmarks', $this->data,  []);
169
        return empty($bookmarks) ? [] : [$bookmarks[0]];
170
    }
171
172
    /**
173
     * Checks Pinterest API paginated response, and parses data
174
     * with bookmarks info from it.
175
     *
176
     * @return array
177
     */
178
    public function getPaginationData()
179
    {
180
        if ($this->isEmpty() && $this->hasErrors()) {
181
            return [];
182
        }
183
184
        $bookmarks = $this->getBookmarks();
185
        if ($data = $this->getResponseData()) {
186
            return ['data' => $data, 'bookmarks' => $bookmarks];
187
        }
188
189
        return [];
190
    }
191
192
    /**
193
     * @return array
194
     */
195
    public function getLastError()
196
    {
197
        return $this->lastError;
198
    }
199
200
    /**
201
     * @return array|null
202
     */
203
    public function getClientInfo()
204
    {
205
        return $this->clientInfo;
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function __toString()
212
    {
213
        return $this->data;
214
    }
215
}
216