|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Request; |
|
6
|
|
|
use seregazhuk\PinterestBot\Api\Response; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Provider. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class Provider |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* List of methods that require logged status. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $loginRequiredFor = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Instance of the API Request. |
|
22
|
|
|
* |
|
23
|
|
|
* @var Request |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $request; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Response |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $response; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Request $request |
|
34
|
|
|
* @param Response $response |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Request $request, Response $response) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->request = $request; |
|
39
|
|
|
$this->response = $response; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Executes a POST request to Pinterest API. |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $requestOptions |
|
46
|
|
|
* @param string $resourceUrl |
|
47
|
|
|
* @param bool $returnResponse |
|
48
|
|
|
* |
|
49
|
|
|
* @return Response|bool |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function execPostRequest($requestOptions, $resourceUrl, $returnResponse = false) |
|
52
|
|
|
{ |
|
53
|
|
|
$postString = Request::createQuery($requestOptions); |
|
54
|
|
|
|
|
55
|
|
|
$this->execute($resourceUrl, $postString); |
|
56
|
|
|
|
|
57
|
|
|
return $returnResponse ? $this->response : $this->response->isOk(); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Executes a GET request to Pinterest API. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $requestOptions |
|
65
|
|
|
* @param string $resourceUrl |
|
66
|
|
|
* @param array $bookmarks |
|
67
|
|
|
* @return array|bool|Response |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function execGetRequest(array $requestOptions = [], $resourceUrl = '', $bookmarks = null) |
|
70
|
|
|
{ |
|
71
|
|
|
$query = Request::createQuery($requestOptions, $bookmarks); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$this->execute($resourceUrl . "?{$query}"); |
|
74
|
|
|
|
|
75
|
|
|
return is_null($bookmarks) ? |
|
76
|
|
|
$this->response->getResponseData() : |
|
77
|
|
|
$this->response; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $url |
|
82
|
|
|
* @param string $postString |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function execute($url, $postString = "") |
|
86
|
|
|
{ |
|
87
|
|
|
$result = $this->request->exec($url, $postString); |
|
88
|
|
|
|
|
89
|
|
|
$this->processResult($result); |
|
90
|
|
|
|
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $method |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function checkMethodRequiresLogin($method) |
|
100
|
|
|
{ |
|
101
|
|
|
return in_array($method, $this->loginRequiredFor); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
public function isLoggedIn() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->request->isLoggedIn(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Simply makes GET request to some url. |
|
114
|
|
|
* @param string $url |
|
115
|
|
|
* @return array|bool |
|
116
|
|
|
*/ |
|
117
|
|
|
public function visitPage($url = '') |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->execGetRequest([], $url); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $res |
|
124
|
|
|
* @return Response |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function processResult($res) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->response->fill(json_decode($res, true)); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.