Completed
Pull Request — master (#172)
by Sergey
02:54
created

Response::hasData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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