1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Contracts\ResponseInterface; |
6
|
|
|
|
7
|
|
|
class Response implements ResponseInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $response; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array|null |
16
|
|
|
*/ |
17
|
|
|
protected $lastError; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Check if specified data exists in response |
21
|
|
|
* @param array $response |
22
|
|
|
* @param null $key |
23
|
|
|
* @return array|bool |
24
|
|
|
*/ |
25
|
|
|
public function getData($response, $key = null) |
26
|
|
|
{ |
27
|
|
|
if ( ! $this->checkErrorInResponse($response)) { |
28
|
|
|
return false; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this->parseData($response, $key); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Parse data from Pinterest Api response. |
36
|
|
|
* Data stores in ['resource_response']['data'] array |
37
|
|
|
* |
38
|
|
|
* @param array $response |
39
|
|
|
* @param string $key |
40
|
|
|
* @return bool|array |
41
|
|
|
*/ |
42
|
|
|
protected function parseData($response, $key) |
43
|
|
|
{ |
44
|
|
|
if (isset($response['resource_response']['data'])) { |
45
|
|
|
$data = $response['resource_response']['data']; |
46
|
|
|
|
47
|
|
|
if ($key) { |
48
|
|
|
return array_key_exists($key, $data) ? $data[$key] : false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $data; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Checks if response is not empty |
59
|
|
|
* |
60
|
|
|
* @param array $response |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function notEmpty($response) |
64
|
|
|
{ |
65
|
|
|
return ! empty($this->getData($response)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $response |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function checkResponse($response) |
73
|
|
|
{ |
74
|
|
|
return ($this->notEmpty($response) && $this->checkErrorInResponse($response)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Check for error info in api response and save |
79
|
|
|
* it. |
80
|
|
|
* |
81
|
|
|
* @param array $response |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function checkErrorInResponse($response) |
85
|
|
|
{ |
86
|
|
|
$this->lastError = null; |
87
|
|
|
|
88
|
|
|
if (isset($response['resource_response']['error']) && ! empty($response['resource_response']['error'])) { |
89
|
|
|
$this->lastError = $response['resource_response']['error']; |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Parse bookmarks from response |
98
|
|
|
* @param array $response |
99
|
|
|
* @return array|null |
100
|
|
|
*/ |
101
|
|
|
public function getBookmarksFromResponse($response) |
102
|
|
|
{ |
103
|
|
|
if ( ! $this->checkErrorInResponse($response)) { |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
if (isset($response['resource']['options']['bookmarks'][0])) { |
107
|
|
|
return [$response['resource']['options']['bookmarks'][0]]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Checks Pinterest API paginated response, and parses data |
115
|
|
|
* with bookmarks info from it. |
116
|
|
|
* |
117
|
|
|
* @param array $response |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function getPaginationData($response) |
121
|
|
|
{ |
122
|
|
|
if ( ! $this->checkResponse($response)) { |
123
|
|
|
return []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$bookmarks = $this->getBookmarksFromResponse($response); |
127
|
|
|
if ($data = self::getData($response)) { |
128
|
|
|
return ['data' => $data, 'bookmarks' => $bookmarks]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return []; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Parses Pinterest search API response for data and bookmarks |
136
|
|
|
* for next pagination page |
137
|
|
|
* |
138
|
|
|
* @param array $response |
139
|
|
|
* @param bool $bookmarksUsed |
140
|
|
|
* @return array|null |
141
|
|
|
*/ |
142
|
|
|
public function parseSearchResponse($response, $bookmarksUsed = true) |
143
|
|
|
{ |
144
|
|
|
if ($response === null || ! $bookmarksUsed) { |
145
|
|
|
return self::parseSimpledSearchResponse($response); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->getPaginationData($response); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Parses simple Pinterest search API response |
153
|
|
|
* on request with bookmarks |
154
|
|
|
* |
155
|
|
|
* @param array $response |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
public function parseSimpledSearchResponse($response) |
159
|
|
|
{ |
160
|
|
|
$bookmarks = []; |
161
|
|
|
if (isset($response['module']['tree']['resource']['options']['bookmarks'][0])) { |
162
|
|
|
$bookmarks = $response['module']['tree']['resource']['options']['bookmarks'][0]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ( ! empty($response['module']['tree']['data']['results'])) { |
166
|
|
|
return ['data' => $response['module']['tree']['data']['results'], 'bookmarks' => [$bookmarks]]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return []; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function getLastError() |
176
|
|
|
{ |
177
|
|
|
return $this->lastError; |
178
|
|
|
} |
179
|
|
|
} |