|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Trello; |
|
4
|
|
|
|
|
5
|
|
|
use Trello\Api\ApiInterface; |
|
6
|
|
|
use Trello\Exception\InvalidArgumentException; |
|
7
|
|
|
use Trello\Exception\BadMethodCallException; |
|
8
|
|
|
use Trello\HttpClient\HttpClient; |
|
9
|
|
|
use Trello\HttpClient\HttpClientInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Simple PHP Trello client |
|
13
|
|
|
* |
|
14
|
|
|
* @method Api\Action action() |
|
15
|
|
|
* @method Api\Action actions() |
|
16
|
|
|
* @method Api\Board board() |
|
17
|
|
|
* @method Api\Board boards() |
|
18
|
|
|
* @method Api\Card card() |
|
19
|
|
|
* @method Api\Card cards() |
|
20
|
|
|
* @method Api\Checklist checklist() |
|
21
|
|
|
* @method Api\Checklist checklists() |
|
22
|
|
|
* @method Api\Cardlist list() |
|
23
|
|
|
* @method Api\Cardlist lists() |
|
24
|
|
|
* @method Api\Organization organization() |
|
25
|
|
|
* @method Api\Organization organizations() |
|
26
|
|
|
* @method Api\Member member() |
|
27
|
|
|
* @method Api\Member members() |
|
28
|
|
|
* @method Api\Token token() |
|
29
|
|
|
* @method Api\Token tokens() |
|
30
|
|
|
* @method Api\Webhook webhook() |
|
31
|
|
|
* @method Api\Webhook webhooks() |
|
32
|
|
|
*/ |
|
33
|
|
|
class Client implements ClientInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Constant for authentication method. Indicates the default, but deprecated |
|
37
|
|
|
* login with username and token in URL. |
|
38
|
|
|
*/ |
|
39
|
|
|
const AUTH_URL_TOKEN = 'url_token'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constant for authentication method. Not indicates the new login, but allows |
|
43
|
|
|
* usage of unauthenticated rate limited requests for given client_id + client_secret |
|
44
|
|
|
*/ |
|
45
|
|
|
const AUTH_URL_CLIENT_ID = 'url_client_id'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Constant for authentication method. Indicates the new favored login method |
|
49
|
|
|
* with username and password via HTTP Authentication. |
|
50
|
|
|
*/ |
|
51
|
|
|
const AUTH_HTTP_PASSWORD = 'http_password'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Constant for authentication method. Indicates the new login method with |
|
55
|
|
|
* with username and token via HTTP Authentication. |
|
56
|
|
|
*/ |
|
57
|
|
|
const AUTH_HTTP_TOKEN = 'http_token'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
private $options = array( |
|
63
|
|
|
'base_url' => 'https://api.trello.com/', |
|
64
|
|
|
'user_agent' => 'php-trello-api (http://github.com/cdaguerre/php-trello-api)', |
|
65
|
|
|
'timeout' => 10, |
|
66
|
|
|
'api_limit' => 5000, |
|
67
|
|
|
'api_version' => 1, |
|
68
|
|
|
'cache_dir' => null, |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The Buzz instance used to communicate with Trello |
|
73
|
|
|
* |
|
74
|
|
|
* @var HttpClientInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
private $httpClient; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Instantiate a new Trello client |
|
80
|
|
|
* |
|
81
|
|
|
* @param null|HttpClientInterface $httpClient Trello http client |
|
82
|
|
|
*/ |
|
83
|
347 |
|
public function __construct(HttpClientInterface $httpClient = null) |
|
84
|
|
|
{ |
|
85
|
347 |
|
$this->httpClient = $httpClient; |
|
86
|
347 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get an API by name |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $name |
|
92
|
|
|
* |
|
93
|
|
|
* @return ApiInterface |
|
94
|
|
|
* |
|
95
|
|
|
* @throws InvalidArgumentException if the requested api does not exist |
|
96
|
|
|
*/ |
|
97
|
42 |
|
public function api($name) |
|
98
|
|
|
{ |
|
99
|
|
|
switch ($name) { |
|
100
|
42 |
|
case 'action': |
|
101
|
42 |
|
case 'actions': |
|
102
|
4 |
|
$api = new Api\Action($this); |
|
103
|
4 |
|
break; |
|
104
|
38 |
|
case 'board': |
|
105
|
38 |
|
case 'boards': |
|
106
|
4 |
|
$api = new Api\Board($this); |
|
107
|
4 |
|
break; |
|
108
|
34 |
|
case 'card': |
|
109
|
34 |
|
case 'cards': |
|
110
|
4 |
|
$api = new Api\Card($this); |
|
111
|
4 |
|
break; |
|
112
|
30 |
|
case 'checklist': |
|
113
|
30 |
|
case 'checklists': |
|
114
|
4 |
|
$api = new Api\Checklist($this); |
|
115
|
4 |
|
break; |
|
116
|
26 |
|
case 'list': |
|
117
|
26 |
|
case 'lists': |
|
118
|
4 |
|
$api = new Api\Cardlist($this); |
|
119
|
4 |
|
break; |
|
120
|
22 |
|
case 'member': |
|
121
|
22 |
|
case 'members': |
|
122
|
4 |
|
$api = new Api\Member($this); |
|
123
|
4 |
|
break; |
|
124
|
18 |
|
case 'notification': |
|
125
|
18 |
|
case 'notifications': |
|
126
|
4 |
|
$api = new Api\Notification($this); |
|
127
|
4 |
|
break; |
|
128
|
14 |
|
case 'organization': |
|
129
|
14 |
|
case 'organizations': |
|
130
|
4 |
|
$api = new Api\Organization($this); |
|
131
|
4 |
|
break; |
|
132
|
10 |
|
case 'token': |
|
133
|
10 |
|
case 'tokens': |
|
134
|
4 |
|
$api = new Api\Token($this); |
|
135
|
4 |
|
break; |
|
136
|
6 |
|
case 'webhook': |
|
137
|
6 |
|
case 'webhooks': |
|
138
|
4 |
|
$api = new Api\Webhook($this); |
|
139
|
4 |
|
break; |
|
140
|
2 |
|
default: |
|
141
|
2 |
|
throw new InvalidArgumentException(sprintf('Undefined api called: "%s"', $name)); |
|
142
|
2 |
|
} |
|
143
|
|
|
|
|
144
|
40 |
|
return $api; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Authenticate a user for all next requests |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $tokenOrLogin Trello private token/username/client ID |
|
151
|
|
|
* @param null|string $password Trello password/secret (optionally can contain $authMethod) |
|
152
|
|
|
* @param null|string $authMethod One of the AUTH_* class constants |
|
153
|
|
|
* |
|
154
|
|
|
* @throws InvalidArgumentException If no authentication method was given |
|
155
|
|
|
*/ |
|
156
|
7 |
|
public function authenticate($tokenOrLogin, $password = null, $authMethod = null) |
|
157
|
|
|
{ |
|
158
|
7 |
|
if (null === $password && null === $authMethod) { |
|
159
|
1 |
|
throw new InvalidArgumentException('You need to specify authentication method!'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
6 |
|
if (null === $authMethod && in_array($password, array(self::AUTH_URL_TOKEN, self::AUTH_URL_CLIENT_ID, self::AUTH_HTTP_PASSWORD, self::AUTH_HTTP_TOKEN))) { |
|
163
|
2 |
|
$authMethod = $password; |
|
164
|
2 |
|
$password = null; |
|
165
|
2 |
|
} |
|
166
|
|
|
|
|
167
|
6 |
|
if (null === $authMethod) { |
|
168
|
|
|
$authMethod = self::AUTH_HTTP_PASSWORD; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
6 |
|
$this->getHttpClient()->authenticate($tokenOrLogin, $password, $authMethod); |
|
172
|
6 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Get Http Client |
|
176
|
|
|
* |
|
177
|
|
|
* @return HttpClient |
|
178
|
|
|
*/ |
|
179
|
15 |
|
public function getHttpClient() |
|
180
|
|
|
{ |
|
181
|
15 |
|
if (null === $this->httpClient) { |
|
182
|
1 |
|
$this->httpClient = new HttpClient($this->options); |
|
183
|
1 |
|
} |
|
184
|
|
|
|
|
185
|
15 |
|
return $this->httpClient; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Set Http Client |
|
190
|
|
|
* |
|
191
|
|
|
* @param HttpClientInterface $httpClient |
|
192
|
|
|
*/ |
|
193
|
294 |
|
public function setHttpClient(HttpClientInterface $httpClient) |
|
194
|
|
|
{ |
|
195
|
294 |
|
$this->httpClient = $httpClient; |
|
196
|
294 |
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Clears used headers |
|
200
|
|
|
*/ |
|
201
|
1 |
|
public function clearHeaders() |
|
202
|
|
|
{ |
|
203
|
1 |
|
$this->getHttpClient()->clearHeaders(); |
|
204
|
1 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Set headers |
|
208
|
|
|
* |
|
209
|
|
|
* @param array $headers |
|
210
|
|
|
*/ |
|
211
|
1 |
|
public function setHeaders(array $headers) |
|
212
|
|
|
{ |
|
213
|
1 |
|
$this->getHttpClient()->setHeaders($headers); |
|
214
|
1 |
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get option by name |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $name the option's name |
|
220
|
|
|
* |
|
221
|
|
|
* @return mixed |
|
222
|
|
|
* |
|
223
|
|
|
* @throws InvalidArgumentException |
|
224
|
|
|
*/ |
|
225
|
|
|
public function getOption($name) |
|
226
|
|
|
{ |
|
227
|
|
|
if (!array_key_exists($name, $this->options)) { |
|
228
|
|
|
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name)); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $this->options[$name]; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Set option |
|
236
|
|
|
* |
|
237
|
|
|
* @param string $name |
|
238
|
|
|
* @param mixed $value |
|
239
|
|
|
* |
|
240
|
|
|
* @throws InvalidArgumentException if the option is not defined |
|
241
|
|
|
* @throws InvalidArgumentException if the api version is set to an unsupported one |
|
242
|
|
|
*/ |
|
243
|
|
|
public function setOption($name, $value) |
|
244
|
|
|
{ |
|
245
|
|
|
if (!array_key_exists($name, $this->options)) { |
|
246
|
|
|
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name)); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
if ('api_version' == $name && !in_array($value, $this->getSupportedApiVersions())) { |
|
250
|
|
|
throw new InvalidArgumentException(sprintf('Invalid API version ("%s"), valid are: %s', $name, implode(', ', $supportedApiVersions))); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$this->options[$name] = $value; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Returns an array of valid API versions supported by this client. |
|
258
|
|
|
* |
|
259
|
|
|
* @return integer[] |
|
260
|
|
|
*/ |
|
261
|
|
|
public function getSupportedApiVersions() |
|
262
|
|
|
{ |
|
263
|
|
|
return array(1); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Proxies $this->members() to $this->api('members') |
|
268
|
|
|
* |
|
269
|
|
|
* @param string $name method name |
|
270
|
|
|
* @param array $args arguments |
|
271
|
|
|
* |
|
272
|
|
|
* @return ApiInterface |
|
273
|
|
|
* |
|
274
|
|
|
* @throws BadMethodCallException |
|
275
|
|
|
*/ |
|
276
|
21 |
|
public function __call($name, $args) |
|
277
|
|
|
{ |
|
278
|
|
|
try { |
|
279
|
21 |
|
return $this->api($name); |
|
280
|
1 |
|
} catch (InvalidArgumentException $e) { |
|
281
|
1 |
|
throw new BadMethodCallException(sprintf('Undefined method called: "%s"', $name)); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|