1 | <?php |
||
8 | class Response implements PaginatedResponse |
||
9 | { |
||
10 | /** |
||
11 | * @var mixed |
||
12 | */ |
||
13 | protected $data = []; |
||
14 | |||
15 | /** |
||
16 | * @var array|null |
||
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) |
||
32 | |||
33 | /** |
||
34 | * @param mixed $data |
||
35 | * @return $this |
||
36 | */ |
||
37 | public function fill($data) |
||
45 | |||
46 | /** |
||
47 | * @param string $json |
||
48 | * @return static |
||
49 | */ |
||
50 | public function fillFromJson($json) |
||
54 | |||
55 | /** |
||
56 | * Check if specified data exists in response. |
||
57 | * |
||
58 | * @param null $key |
||
59 | * @return array|bool |
||
60 | */ |
||
61 | public function getResponseData($key = null) |
||
62 | { |
||
63 | if ($this->hasErrors()) return false; |
||
64 | |||
65 | return $this->parseResponseData($key); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string $key |
||
70 | * @param null $default |
||
71 | * @return mixed |
||
72 | */ |
||
73 | public function getData($key = '', $default = null) |
||
77 | |||
78 | /** |
||
79 | * @param string $key |
||
80 | * @return bool |
||
81 | */ |
||
82 | public function hasData($key = '') |
||
86 | |||
87 | /** |
||
88 | * Parse data from Pinterest Api response. |
||
89 | * Data is stored in ['resource_response']['data'] array. |
||
90 | * |
||
91 | * @param string $key |
||
92 | * @return bool|array |
||
93 | */ |
||
94 | protected function parseResponseData($key) |
||
104 | |||
105 | /** |
||
106 | * Checks if response is empty. |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function isEmpty() |
||
114 | |||
115 | /** |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function isOk() |
||
122 | |||
123 | /** |
||
124 | * Check for error info in api response and save |
||
125 | * it. |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function hasErrors() |
||
133 | |||
134 | /** |
||
135 | * Parse bookmarks from response. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | public function getBookmarks() |
||
149 | |||
150 | /** |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function hasBookmarks() |
||
157 | |||
158 | protected function getRawBookmarksData() |
||
162 | |||
163 | /** |
||
164 | * Checks Pinterest API paginated response, and parses data |
||
165 | * with bookmarks info from it. |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | public function getPaginationData() |
||
170 | { |
||
171 | if ($this->hasErrors()) return []; |
||
172 | |||
173 | if ($data = $this->getResponseData()) { |
||
174 | return [ |
||
175 | 'data' => $data, |
||
176 | 'bookmarks' => $this->getBookmarks(), |
||
177 | ]; |
||
178 | } |
||
179 | |||
180 | return []; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return array |
||
185 | */ |
||
186 | public function getLastError() |
||
190 | |||
191 | /** |
||
192 | * @return array|null |
||
193 | */ |
||
194 | public function getClientInfo() |
||
198 | |||
199 | /** |
||
200 | * @return mixed |
||
201 | */ |
||
202 | public function getRawData() |
||
206 | |||
207 | public function clear() |
||
211 | |||
212 | protected function fillError() |
||
220 | |||
221 | protected function fillClientInfo() |
||
227 | } |
||
228 |