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 | * |
||
22 | * @param array $response |
||
23 | * @param null $key |
||
24 | * |
||
25 | * @return array|bool |
||
26 | */ |
||
27 | public function getData($response, $key = null) |
||
28 | { |
||
29 | if (!$this->checkErrorInResponse($response)) { |
||
30 | return false; |
||
31 | } |
||
32 | |||
33 | return $this->parseData($response, $key); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Parse data from Pinterest Api response. |
||
38 | * Data stores in ['resource_response']['data'] array. |
||
39 | * |
||
40 | * @param array $response |
||
41 | * @param string $key |
||
42 | * |
||
43 | * @return bool|array |
||
44 | */ |
||
45 | protected function parseData($response, $key) |
||
46 | { |
||
47 | if (isset($response['resource_response']['data'])) { |
||
48 | $data = $response['resource_response']['data']; |
||
49 | |||
50 | if ($key) { |
||
51 | return array_key_exists($key, $data) ? $data[$key] : false; |
||
52 | } |
||
53 | |||
54 | return $data; |
||
55 | } |
||
56 | |||
57 | return false; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Checks if response is not empty. |
||
62 | * |
||
63 | * @param array $response |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function notEmpty($response) |
||
71 | |||
72 | /** |
||
73 | * Check for error info in api response and save |
||
74 | * it. |
||
75 | * |
||
76 | * @param array $response |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function checkErrorInResponse($response) |
||
92 | |||
93 | /** |
||
94 | * Parse bookmarks from response. |
||
95 | * |
||
96 | * @param array $response |
||
97 | * |
||
98 | * @return array|null |
||
99 | */ |
||
100 | public function getBookmarksFromResponse($response) |
||
108 | |||
109 | |||
110 | /** |
||
111 | * Parses Pinterest search API response for data and bookmarks |
||
112 | * for next pagination page. |
||
113 | * |
||
114 | * @param array $response |
||
115 | * @param bool $bookmarksUsed |
||
116 | * |
||
117 | * @return array|null |
||
118 | */ |
||
119 | public function parseSearchResponse($response, $bookmarksUsed = true) |
||
127 | |||
128 | /** |
||
129 | * Checks Pinterest API paginated response, and parses data |
||
130 | * with bookmarks info from it. |
||
131 | * |
||
132 | * @param array $response |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | public function getPaginationData($response) |
||
149 | |||
150 | /** |
||
151 | * Parses simple Pinterest search API response |
||
152 | * on request with bookmarks. |
||
153 | * |
||
154 | * @param array $response |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | public function parseSimpledSearchResponse($response) |
||
171 | |||
172 | /** |
||
173 | * @return array |
||
174 | */ |
||
175 | public function getLastError() |
||
179 | } |
||
180 |