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