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