|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Slackify. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Strime <[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 Strime\Slackify\Api; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractApi implements ApiInterface |
|
15
|
|
|
{ |
|
16
|
|
|
const SLACK_API_URL = "https://slack.com/api/"; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $token; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
public $url; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public function __construct($token) { |
|
26
|
|
|
$this->token = $token; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getToken() { |
|
35
|
|
|
return $this->token; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $token |
|
40
|
|
|
* |
|
41
|
|
|
* @return ApiInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setToken($token) { |
|
44
|
|
|
|
|
45
|
|
|
if (!is_string($token)) { |
|
46
|
|
|
throw new InvalidArgumentException('The token must be a string.'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->token = $token; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $method |
|
56
|
|
|
* @param array $arguments |
|
57
|
|
|
* |
|
58
|
|
|
* @return ApiInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setUrl($method, $arguments = NULL) { |
|
61
|
|
|
|
|
62
|
|
|
if (!is_string($method)) { |
|
63
|
|
|
throw new InvalidArgumentException('The method variable must be a string.'); |
|
64
|
|
|
} |
|
65
|
|
|
if (($arguments != NULL) && !is_array($arguments)) { |
|
66
|
|
|
throw new InvalidArgumentException('The arguments variable must be an array.'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Set a new array |
|
70
|
|
|
$arguments_list = array(); |
|
71
|
|
|
|
|
72
|
|
|
// Check if a token is set through the arguments |
|
73
|
|
|
$token_is_set = FALSE; |
|
74
|
|
|
|
|
75
|
|
|
if ($arguments != NULL) { |
|
76
|
|
|
foreach ($arguments as $key => $value) { |
|
77
|
|
|
$arguments_list[] = $key . "=" . $value; |
|
78
|
|
|
|
|
79
|
|
|
if(strcmp($key, "token") == 0) { |
|
80
|
|
|
$token_is_set = TRUE; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// If the token has not been set, add it to the list |
|
86
|
|
|
if(!$token_is_set && isset($this->token) && is_string($this->token)) { |
|
87
|
|
|
$arguments_list[] = "token=".$this->token; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->url = self::SLACK_API_URL . $method . "?" . implode("&", $arguments_list); |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getUrl() { |
|
99
|
|
|
return $this->url; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|