|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse; |
|
6
|
|
|
use function seregazhuk\PinterestBot\get_array_data; |
|
7
|
|
|
|
|
8
|
|
|
class Response implements PaginatedResponse |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var mixed |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $data = []; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Error |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $lastError; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array|null |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $clientInfo; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param mixed $data |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($data = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->lastError = new Error(); |
|
31
|
|
|
|
|
32
|
|
|
if($data) { |
|
33
|
|
|
$this->fill($data); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param mixed $data |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
|
|
public function fill($data) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->data = $data; |
|
44
|
|
|
|
|
45
|
|
|
return $this |
|
46
|
|
|
->fillError() |
|
47
|
|
|
->fillClientInfo(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $json |
|
52
|
|
|
* @return static |
|
53
|
|
|
*/ |
|
54
|
|
|
public function fillFromJson($json) |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->fill(json_decode($json, true)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Check if specified data exists in response. |
|
61
|
|
|
* |
|
62
|
|
|
* @param null $key |
|
63
|
|
|
* @return array|bool |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getResponseData($key = null) |
|
66
|
|
|
{ |
|
67
|
|
|
if ($this->hasErrors()) { |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->parseResponseData($key); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $key |
|
76
|
|
|
* @param null $default |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getData($key = '', $default = null) |
|
80
|
|
|
{ |
|
81
|
|
|
return get_array_data($key, $this->data, $default); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $key |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
public function hasData($key = '') |
|
89
|
|
|
{ |
|
90
|
|
|
return get_array_data($key, $this->data) !== null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Parse data from Pinterest Api response. |
|
95
|
|
|
* Data is stored in ['resource_response']['data'] array. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $key |
|
98
|
|
|
* @return bool|array |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function parseResponseData($key) |
|
101
|
|
|
{ |
|
102
|
|
|
$responseData = get_array_data('resource_response.data', $this->data); |
|
103
|
|
|
|
|
104
|
|
|
if (!$responseData) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $key ? |
|
109
|
|
|
get_array_data($key, $responseData) : |
|
110
|
|
|
$responseData; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Checks if response is empty. |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public function isEmpty() |
|
119
|
|
|
{ |
|
120
|
|
|
return empty($this->getResponseData()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function isOk() |
|
127
|
|
|
{ |
|
128
|
|
|
return !$this->hasErrors(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Check for error info in api response and save it. |
|
133
|
|
|
* |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
|
|
public function hasErrors() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->lastError && !$this->lastError->isEmpty(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Parse bookmarks from response. |
|
143
|
|
|
* |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getBookmarks() |
|
147
|
|
|
{ |
|
148
|
|
|
$bookmarks = $this->getRawBookmarksData(); |
|
149
|
|
|
|
|
150
|
|
|
if (empty($bookmarks)) { |
|
151
|
|
|
return []; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
if ($bookmarks[0] === '-end-') { |
|
155
|
|
|
return []; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $bookmarks; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
|
|
public function hasBookmarks() |
|
165
|
|
|
{ |
|
166
|
|
|
return (bool) $this->getBookmarks(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
protected function getRawBookmarksData() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->getData('resource.options.bookmarks', []); |
|
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->hasErrors()) return []; |
|
183
|
|
|
|
|
184
|
|
|
if ($data = $this->getResponseData()) { |
|
185
|
|
|
return [ |
|
186
|
|
|
'data' => $data, |
|
187
|
|
|
'bookmarks' => $this->getBookmarks(), |
|
188
|
|
|
]; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return []; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return string|null |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getLastErrorText() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->lastError->getText(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return array|null |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getClientInfo() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->clientInfo; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return mixed |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getRawData() |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->data; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function clear() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->fill([]); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
protected function fillError() |
|
224
|
|
|
{ |
|
225
|
|
|
$errorData = get_array_data('resource_response.error', $this->data); |
|
226
|
|
|
|
|
227
|
|
|
if(!empty($errorData)) { |
|
228
|
|
|
$this->lastError = new Error($errorData); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $this; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
protected function fillClientInfo() |
|
235
|
|
|
{ |
|
236
|
|
|
$this->clientInfo = get_array_data('client_context', $this->data); |
|
237
|
|
|
|
|
238
|
|
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|