1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers\Core; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\ProvidersContainer; |
6
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
7
|
|
|
use seregazhuk\PinterestBot\Api\Response; |
8
|
|
|
use seregazhuk\PinterestBot\Helpers\Pagination; |
9
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlBuilder; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Provider. |
13
|
|
|
*/ |
14
|
|
|
abstract class Provider |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* List of methods that require logged status. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $loginRequiredFor = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Instance of the API Request. |
25
|
|
|
* |
26
|
|
|
* @var Request |
27
|
|
|
*/ |
28
|
|
|
protected $request; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Response |
32
|
|
|
*/ |
33
|
|
|
protected $response; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ProvidersContainer |
37
|
|
|
*/ |
38
|
|
|
protected $container; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ProvidersContainer $container |
42
|
|
|
* @internal param Request $request |
43
|
|
|
* @internal param Response $response |
44
|
|
|
*/ |
45
|
|
|
public function __construct(ProvidersContainer $container) |
46
|
|
|
{ |
47
|
|
|
$this->container = $container; |
48
|
|
|
$this->request = $container->getRequest(); |
49
|
|
|
$this->response = $container->getResponse(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Executes a POST request to Pinterest API. |
54
|
|
|
* |
55
|
|
|
* @param array $requestOptions |
56
|
|
|
* @param string $resourceUrl |
57
|
|
|
* |
58
|
|
|
* @return Response|bool |
59
|
|
|
*/ |
60
|
|
|
protected function post($requestOptions, $resourceUrl) |
61
|
|
|
{ |
62
|
|
|
$postString = Request::createQuery($requestOptions); |
63
|
|
|
|
64
|
|
|
$this->execute($resourceUrl, $postString); |
65
|
|
|
|
66
|
|
|
return $this->response->isOk(); |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Executes a GET request to Pinterest API. |
72
|
|
|
* |
73
|
|
|
* @param array $requestOptions |
74
|
|
|
* @param string $resourceUrl |
75
|
|
|
* @return array|bool|Response |
76
|
|
|
*/ |
77
|
|
|
protected function get(array $requestOptions = [], $resourceUrl = '') |
78
|
|
|
{ |
79
|
|
|
$query = Request::createQuery( |
80
|
|
|
$requestOptions, |
81
|
|
|
$this->response->getBookmarks() |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$this->execute($resourceUrl . '?' . $query); |
85
|
|
|
|
86
|
|
|
return $this->response->getResponseData(); |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $url |
92
|
|
|
* @param string $postString |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
protected function execute($url, $postString = "") |
96
|
|
|
{ |
97
|
|
|
$result = $this->request->exec($url, $postString); |
98
|
|
|
|
99
|
|
|
$this->response->fillFromJson($result); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $method |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function checkMethodRequiresLogin($method) |
110
|
|
|
{ |
111
|
|
|
return in_array($method, $this->loginRequiredFor); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function isLoggedIn() |
118
|
|
|
{ |
119
|
|
|
return $this->request->isLoggedIn(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param mixed $data |
124
|
|
|
* @param string $resourceUrl |
125
|
|
|
* @param int $limit |
126
|
|
|
* |
127
|
|
|
* @return Pagination |
128
|
|
|
*/ |
129
|
|
|
protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LIMIT) |
130
|
|
|
{ |
131
|
|
|
return $this |
132
|
|
|
->paginateCustom(function () use ($data, $resourceUrl) { |
133
|
|
|
$this->get($data, $resourceUrl); |
134
|
|
|
return $this->response; |
135
|
|
|
})->take($limit); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Accepts callback which should return PaginatedResponse object. |
140
|
|
|
* |
141
|
|
|
* @param callable $callback |
142
|
|
|
* @param int $limit |
143
|
|
|
* @return Pagination |
144
|
|
|
*/ |
145
|
|
|
protected function paginateCustom(callable $callback, $limit = Pagination::DEFAULT_LIMIT) |
146
|
|
|
{ |
147
|
|
|
$this->response->clear(); |
148
|
|
|
|
149
|
|
|
return (new Pagination) |
150
|
|
|
->paginateOver($callback) |
151
|
|
|
->take($limit); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|