1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
4
|
|
|
|
5
|
|
|
use Iterator; |
6
|
|
|
use LogicException; |
7
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlHelper; |
8
|
|
|
use seregazhuk\PinterestBot\Helpers\Pagination; |
9
|
|
|
use seregazhuk\PinterestBot\Exceptions\AuthException; |
10
|
|
|
use seregazhuk\PinterestBot\Helpers\Requests\PinnerHelper; |
11
|
|
|
use seregazhuk\PinterestBot\Helpers\Providers\Traits\Followable; |
12
|
|
|
use seregazhuk\PinterestBot\Helpers\Providers\Traits\Searchable; |
13
|
|
|
use seregazhuk\PinterestBot\Helpers\Providers\Traits\HasFollowers; |
14
|
|
|
|
15
|
|
|
class Pinners extends Provider |
16
|
|
|
{ |
17
|
|
|
use Searchable, Followable, HasFollowers; |
18
|
|
|
|
19
|
|
|
protected $loginRequiredFor = ['follow', 'unFollow']; |
20
|
|
|
|
21
|
|
|
protected $searchScope = 'people'; |
22
|
|
|
protected $entityIdName = 'user_id'; |
23
|
|
|
protected $followersFor = 'username'; |
24
|
|
|
|
25
|
|
|
protected $followUrl = UrlHelper::RESOURCE_FOLLOW_USER; |
26
|
|
|
protected $unFollowUrl = UrlHelper::RESOURCE_UNFOLLOW_USER; |
27
|
|
|
protected $followersUrl = UrlHelper::RESOURCE_USER_FOLLOWERS; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get user info. |
31
|
|
|
* If username param is not specified, will |
32
|
|
|
* return info for logged user. |
33
|
|
|
* |
34
|
|
|
* @param string $username |
35
|
|
|
* |
36
|
|
|
* @return null|array |
37
|
|
|
*/ |
38
|
|
|
public function info($username) |
39
|
|
|
{ |
40
|
|
|
return $this->execGetRequest(['username' => $username], UrlHelper::RESOURCE_USER_INFO); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get pinner following other pinners. |
45
|
|
|
* |
46
|
|
|
* @param string $username |
47
|
|
|
* @param int $limit |
48
|
|
|
* |
49
|
|
|
* @return Iterator |
50
|
|
|
*/ |
51
|
|
|
public function following($username, $limit = 0) |
52
|
|
|
{ |
53
|
|
|
return $this->paginate( |
54
|
|
|
$username, UrlHelper::RESOURCE_USER_FOLLOWING, $limit |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get pinner pins. |
60
|
|
|
* |
61
|
|
|
* @param string $username |
62
|
|
|
* @param int $limit |
63
|
|
|
* |
64
|
|
|
* @return Iterator |
65
|
|
|
*/ |
66
|
|
|
public function pins($username, $limit = 0) |
67
|
|
|
{ |
68
|
|
|
return $this->paginate( |
69
|
|
|
$username, UrlHelper::RESOURCE_USER_PINS, $limit |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Login as pinner. |
75
|
|
|
* |
76
|
|
|
* @param string $username |
77
|
|
|
* @param string $password |
78
|
|
|
* |
79
|
|
|
* @throws AuthException |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function login($username, $password) |
84
|
|
|
{ |
85
|
|
|
if ($this->request->isLoggedIn()) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->checkCredentials($username, $password); |
90
|
|
|
|
91
|
|
|
$postString = PinnerHelper::createLoginQuery($username, $password); |
92
|
|
|
$this->request->clearToken(); |
93
|
|
|
|
94
|
|
|
$response = $this->request->exec(UrlHelper::RESOURCE_LOGIN, $postString); |
95
|
|
|
$result = $this->response->hasErrors($response); |
96
|
|
|
if (!$result) { |
97
|
|
|
throw new AuthException($this->response->getLastError()['message']); |
98
|
|
|
} |
99
|
|
|
$this->request->login(); |
100
|
|
|
|
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function logout() |
105
|
|
|
{ |
106
|
|
|
$this->request->logout(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Validates password and login. |
111
|
|
|
* |
112
|
|
|
* @param string $username |
113
|
|
|
* @param string $password |
114
|
|
|
*/ |
115
|
|
|
protected function checkCredentials($username, $password) |
116
|
|
|
{ |
117
|
|
|
if (!$username || !$password) { |
118
|
|
|
throw new LogicException('You must set username and password to login.'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $username |
124
|
|
|
* @param string $url |
125
|
|
|
* @param int $limit |
126
|
|
|
* |
127
|
|
|
* @return Iterator |
128
|
|
|
*/ |
129
|
|
|
protected function paginate($username, $url, $limit) |
130
|
|
|
{ |
131
|
|
|
$params = [ |
132
|
|
|
'data' => ['username' => $username], |
133
|
|
|
'url' => $url, |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
return (new Pagination($this))->paginateOver('getPaginatedData', $params, $limit); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|