1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
6
|
|
|
use seregazhuk\PinterestBot\Api\Response; |
7
|
|
|
use seregazhuk\PinterestBot\Contracts\RequestInterface; |
8
|
|
|
use seregazhuk\PinterestBot\Contracts\ResponseInterface; |
9
|
|
|
use seregazhuk\PinterestBot\Helpers\Providers\Traits\ProviderTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Provider. |
13
|
|
|
*/ |
14
|
|
|
abstract class Provider |
15
|
|
|
{ |
16
|
|
|
use ProviderTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* List of methods that require logged status. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $loginRequired = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Instance of the API RequestInterface. |
27
|
|
|
* |
28
|
|
|
* @var RequestInterface |
29
|
|
|
*/ |
30
|
|
|
protected $request; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Instance of the API ResponseInterface. |
34
|
|
|
* |
35
|
|
|
* @var ResponseInterface |
36
|
|
|
*/ |
37
|
|
|
protected $response; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param RequestInterface $request |
41
|
|
|
* @param ResponseInterface $response |
42
|
|
|
*/ |
43
|
|
|
public function __construct(RequestInterface $request, ResponseInterface $response) |
44
|
|
|
{ |
45
|
|
|
$this->request = $request; |
46
|
|
|
$this->response = $response; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Executes a POST request to Pinterest API. |
51
|
|
|
* |
52
|
|
|
* @param array $requestOptions |
53
|
|
|
* @param string $resourceUrl |
54
|
|
|
* @param bool $returnData |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
public function execPostRequest($requestOptions, $resourceUrl, $returnData = false) |
59
|
|
|
{ |
60
|
|
|
$data = ['options' => $requestOptions]; |
61
|
|
|
$postString = Request::createQuery($data); |
62
|
|
|
$response = $this->request->exec($resourceUrl, $postString); |
63
|
|
|
|
64
|
|
|
if ($returnData) { |
65
|
|
|
return $this->response->getData($response); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->response->checkResponse($response); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Request |
73
|
|
|
*/ |
74
|
|
|
public function getRequest() |
75
|
|
|
{ |
76
|
|
|
return $this->request; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return Response |
81
|
|
|
*/ |
82
|
|
|
public function getResponse() |
83
|
|
|
{ |
84
|
|
|
return $this->response; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $method |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function checkMethodRequiresLogin($method) |
93
|
|
|
{ |
94
|
|
|
return in_array($method, $this->loginRequired); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getPaginatedData($data, $url, $sourceUrl, $bookmarks = []) |
98
|
|
|
{ |
99
|
|
|
$data['options'] = $data; |
100
|
|
|
$response = $this->getRequest()->exec($url . '?' . Request::createQuery($data, $sourceUrl, $bookmarks)); |
101
|
|
|
|
102
|
|
|
return $this->getResponse()->getPaginationData($response); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|