1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stack Exchange Api Client library. |
5
|
|
|
* |
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace BenatEspina\StackExchangeApiClient\Api; |
15
|
|
|
|
16
|
|
|
use BenatEspina\StackExchangeApiClient\Authentication\Authentication; |
17
|
|
|
use BenatEspina\StackExchangeApiClient\Http\HttpClient; |
18
|
|
|
use BenatEspina\StackExchangeApiClient\Model\User; |
19
|
|
|
use BenatEspina\StackExchangeApiClient\Serializer\UserSerializer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The user api class. |
23
|
|
|
* |
24
|
|
|
* @author Beñat Espiña <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class UserApi |
27
|
|
|
{ |
28
|
|
|
const URL = 'users/'; |
29
|
|
|
const QUERY_PARAMS = [ |
30
|
|
|
'order' => 'desc', |
31
|
|
|
'sort' => 'reputation', |
32
|
|
|
'site' => 'stackoverflow', |
33
|
|
|
'filter' => HttpClient::FILTER_ALL, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The authentication. |
38
|
|
|
* |
39
|
|
|
* @var Authentication|null |
40
|
|
|
*/ |
41
|
|
|
private $authentication; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param Authentication|null $anAuthentication The authentication |
47
|
|
|
*/ |
48
|
|
|
public function __construct(Authentication $anAuthentication = null) |
49
|
|
|
{ |
50
|
|
|
$this->authentication = $anAuthentication; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns all users on a site. |
55
|
|
|
* |
56
|
|
|
* More info: https://api.stackexchange.com/docs/users |
57
|
|
|
* |
58
|
|
|
* @param array $params QueryString parameter(s), it admits page and pagesize; by default is null |
59
|
|
|
* @param bool $serialize Checks if the result will be serialize or not, by default is true |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function all($params = [], $serialize = true) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
if ($this->authentication instanceof Authentication) { |
66
|
|
|
if (true === empty($params)) { |
67
|
|
|
$params = array_merge($params, self::QUERY_PARAMS); |
68
|
|
|
} |
69
|
|
|
$params = array_merge($params, $this->authentication->toArray()); |
70
|
|
|
} |
71
|
|
|
$response = HttpClient::instance()->get( |
|
|
|
|
72
|
|
|
self::URL, $params |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
return $serialize === true ? UserSerializer::serialize($response) : $response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Gets the users identified in ids in {ids}. |
80
|
|
|
* |
81
|
|
|
* More info: https://api.stackexchange.com/docs/users-by-ids |
82
|
|
|
* |
83
|
|
|
* @param string|array $ids Array which contains the ids delimited by semicolon, or a simple id |
84
|
|
|
* @param array $params QueryString parameter(s) |
85
|
|
|
* @param bool $serialize Checks if the result will be serialize or not, by default is true |
86
|
|
|
* |
87
|
|
|
* @return array|User |
88
|
|
|
*/ |
89
|
|
|
public function getOfIds($ids, array $params = [], $serialize = true) |
90
|
|
|
{ |
91
|
|
|
if ($this->authentication instanceof Authentication) { |
92
|
|
|
if (true === empty($params)) { |
93
|
|
|
$params = array_merge($params, self::QUERY_PARAMS); |
94
|
|
|
} |
95
|
|
|
$params = array_merge($params, $this->authentication->toArray()); |
96
|
|
|
} |
97
|
|
|
$response = HttpClient::instance()->get( |
|
|
|
|
98
|
|
|
self::URL . (is_array($ids) ? implode(';', $ids) : $ids), $params |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
return $serialize === true ? UserSerializer::serialize($response) : $response; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns the user associated with the passed access_token. |
106
|
|
|
* |
107
|
|
|
* More info: https://api.stackexchange.com/docs/me |
108
|
|
|
* |
109
|
|
|
* @param array $params QueryString parameter(s) |
110
|
|
|
* @param bool $serialize Checks if the result will be serialize or not, by default is true |
111
|
|
|
* |
112
|
|
|
* @throws \Exception when the auth is null |
113
|
|
|
* |
114
|
|
|
* @return User |
115
|
|
|
*/ |
116
|
|
|
public function me(array $params = self::QUERY_PARAMS, $serialize = true) |
117
|
|
|
{ |
118
|
|
|
if (!$this->authentication instanceof Authentication) { |
119
|
|
|
throw new \Exception('Authentication is required'); |
120
|
|
|
} |
121
|
|
|
$response = HttpClient::instance()->get( |
|
|
|
|
122
|
|
|
'me', array_merge($params, $this->authentication->toArray()) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $serialize === true ? UserSerializer::serialize($response) : $response; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Gets those users on a site who can exercise moderation powers. |
130
|
|
|
* |
131
|
|
|
* More info: https://api.stackexchange.com/docs/moderators |
132
|
|
|
* |
133
|
|
|
* @param array $params QueryString parameter(s), it admits page and pagesize; by default is null |
134
|
|
|
* @param bool $serialize Checks if the result will be serialize or not, by default is true |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function moderators(array $params = [], $serialize = true) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
if ($this->authentication instanceof Authentication) { |
141
|
|
|
if (true === empty($params)) { |
142
|
|
|
$params = array_merge($params, self::QUERY_PARAMS); |
143
|
|
|
} |
144
|
|
|
$params = array_merge($params, $this->authentication->toArray()); |
145
|
|
|
} |
146
|
|
|
$response = HttpClient::instance()->get( |
|
|
|
|
147
|
|
|
self::URL . 'moderators', $params |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
return $serialize === true ? UserSerializer::serialize($response) : $response; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns those users on a site who both have moderator powers, and were actually elected. |
155
|
|
|
* |
156
|
|
|
* More info: https://api.stackexchange.com/docs/elected-moderators |
157
|
|
|
* |
158
|
|
|
* @param array $params QueryString parameter(s), it admits page and pagesize; by default is null |
159
|
|
|
* @param bool $serialize Checks if the result will be serialize or not, by default is true |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
View Code Duplication |
public function electedModerators(array $params = [], $serialize = true) |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
if ($this->authentication instanceof Authentication) { |
166
|
|
|
if (true === empty($params)) { |
167
|
|
|
$params = array_merge($params, self::QUERY_PARAMS); |
168
|
|
|
} |
169
|
|
|
$params = array_merge($params, $this->authentication->toArray()); |
170
|
|
|
} |
171
|
|
|
$response = HttpClient::instance()->get( |
|
|
|
|
172
|
|
|
self::URL . 'moderators/elected', $params |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
return $serialize === true ? UserSerializer::serialize($response) : $response; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.