1 | <?php |
||
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) |
||
56 | |||
57 | /** |
||
58 | * Checks if response is not empty |
||
59 | * |
||
60 | * @param array $response |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function notEmpty($response) |
||
67 | |||
68 | public function checkResponse($response) |
||
72 | |||
73 | /** |
||
74 | * Check for error info in api response and save |
||
75 | * it. |
||
76 | * |
||
77 | * @param array $response |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function checkErrorInResponse($response) |
||
81 | { |
||
82 | $this->lastError = null; |
||
83 | |||
84 | if (isset($response['resource_response']['error']) && ! empty($response['resource_response']['error'])) { |
||
85 | $this->lastError = $response['resource_response']['error']; |
||
86 | return false; |
||
87 | } |
||
88 | |||
89 | return true; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Parse bookmarks from response |
||
94 | * @param array $response |
||
95 | * @return array|null |
||
96 | */ |
||
97 | public function getBookmarksFromResponse($response) |
||
98 | { |
||
99 | if ( ! $this->checkErrorInResponse($response)) { |
||
100 | return null; |
||
101 | } |
||
102 | if (isset($response['resource']['options']['bookmarks'][0])) { |
||
103 | return [$response['resource']['options']['bookmarks'][0]]; |
||
104 | } |
||
105 | |||
106 | return null; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Checks Pinterest API paginated response, and parses data |
||
111 | * with bookmarks info from it. |
||
112 | * |
||
113 | * @param array $response |
||
114 | * @return array |
||
115 | */ |
||
116 | public function getPaginationData($response) |
||
117 | { |
||
118 | if ( ! $this->checkResponse($response)) { |
||
119 | return []; |
||
120 | } |
||
121 | |||
122 | $bookmarks = $this->getBookmarksFromResponse($response); |
||
123 | if ($data = self::getData($response)) { |
||
124 | return ['data' => $data, 'bookmarks' => $bookmarks]; |
||
125 | } |
||
126 | |||
127 | return []; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Parses Pinterest search API response for data and bookmarks |
||
132 | * for next pagination page |
||
133 | * |
||
134 | * @param array $response |
||
135 | * @param bool $bookmarksUsed |
||
136 | * @return array|null |
||
137 | */ |
||
138 | public function parseSearchResponse($response, $bookmarksUsed = true) |
||
139 | { |
||
140 | if ($response === null || ! $bookmarksUsed) { |
||
141 | return self::parseSimpledSearchResponse($response); |
||
142 | } |
||
143 | |||
144 | return $this->getPaginationData($response); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Parses simple Pinterest search API response |
||
149 | * on request with bookmarks |
||
150 | * |
||
151 | * @param array $response |
||
152 | * @return array |
||
153 | */ |
||
154 | public function parseSimpledSearchResponse($response) |
||
155 | { |
||
156 | $bookmarks = []; |
||
157 | if (isset($response['module']['tree']['resource']['options']['bookmarks'][0])) { |
||
158 | $bookmarks = $response['module']['tree']['resource']['options']['bookmarks'][0]; |
||
159 | } |
||
160 | |||
161 | if ( ! empty($response['module']['tree']['data']['results'])) { |
||
162 | return ['data' => $response['module']['tree']['data']['results'], 'bookmarks' => [$bookmarks]]; |
||
163 | } |
||
164 | |||
165 | return []; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @return array |
||
170 | */ |
||
171 | public function getLastError() |
||
175 | } |