1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Helpers\Providers; |
4
|
|
|
|
5
|
|
|
class RequestHelper |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @param array|object $data |
9
|
|
|
* @param string|null $sourceUrl |
10
|
|
|
* @param array $bookmarks |
11
|
|
|
* @return array |
12
|
|
|
*/ |
13
|
|
|
public static function createRequestData($data, $sourceUrl = null, $bookmarks = []) |
14
|
|
|
{ |
15
|
|
|
if ( ! empty($bookmarks)) { |
16
|
|
|
$data["options"]["bookmarks"] = $bookmarks; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
return [ |
20
|
|
|
"source_url" => $sourceUrl, |
21
|
|
|
"data" => json_encode($data), |
22
|
|
|
]; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Check if specified data exists in response |
27
|
|
|
* @param $response |
28
|
|
|
* @param null $key |
29
|
|
|
* @return array|bool |
30
|
|
|
*/ |
31
|
|
|
public static function getDataFromResponse($response, $key = null) |
32
|
|
|
{ |
33
|
|
|
if (isset($response['resource_response']['data'])) { |
34
|
|
|
$data = $response['resource_response']['data']; |
35
|
|
|
|
36
|
|
|
if ($key) { |
37
|
|
|
return array_key_exists($key, $data) ? $data[$key] : false; |
38
|
|
|
} |
39
|
|
|
return $data; |
40
|
|
|
} |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Parse bookmarks from response |
46
|
|
|
* @param array $response |
47
|
|
|
* @return string|null |
48
|
|
|
*/ |
49
|
|
|
protected static function _getBookmarksFromResponse($response) |
50
|
|
|
{ |
51
|
|
|
if (isset($response['resource']['options']['bookmarks'][0])) { |
52
|
|
|
return [$response['resource']['options']['bookmarks'][0]]; |
53
|
|
|
} |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Checks result of PIN-methods |
59
|
|
|
* |
60
|
|
|
* @param array $res |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public static function checkMethodCallResult($res) |
64
|
|
|
{ |
65
|
|
|
if ($res !== null && isset($res['resource_response'])) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Checks Pinterest API paginated response, and parses data |
74
|
|
|
* with bookmarks info from it. |
75
|
|
|
* |
76
|
|
|
* @param array $res |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public static function parsePaginatedResponse($res) |
80
|
|
|
{ |
81
|
|
|
if ( ! self::checkMethodCallResult($res)) { |
82
|
|
|
return []; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$bookmarks = self::_getBookmarksFromResponse($res); |
86
|
|
|
if ($data = self::getDataFromResponse($res)) { |
87
|
|
|
return ['data' => $data, 'bookmarks' => $bookmarks]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return []; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |