1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of GitterApi package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
namespace Gitter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Route |
12
|
|
|
* @package Gitter |
13
|
|
|
* |
14
|
|
|
* @method static Route get(string $route) |
15
|
|
|
* @method static Route put(string $route) |
16
|
|
|
* @method static Route post(string $route) |
17
|
|
|
* @method static Route patch(string $route) |
18
|
|
|
* @method static Route delete(string $route) |
19
|
|
|
* @method static Route options(string $route) |
20
|
|
|
* @method static Route head(string $route) |
21
|
|
|
* @method static Route connect(string $route) |
22
|
|
|
* @method static Route trace(string $route) |
23
|
|
|
*/ |
24
|
|
|
class Route |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $route; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $parameters = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $method; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string|null |
43
|
|
|
*/ |
44
|
|
|
private $url; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $body = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Route constructor. |
53
|
|
|
* @param string $route |
54
|
|
|
* @param string $method |
55
|
|
|
*/ |
56
|
|
|
public function __construct(string $route, string $method = 'GET') |
57
|
|
|
{ |
58
|
|
|
$this->route($route); |
59
|
|
|
$this->method($method); |
60
|
|
|
$this->toApi(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $url |
65
|
|
|
* @return $this|Route |
66
|
|
|
*/ |
67
|
|
|
public function to(string $url): Route |
68
|
|
|
{ |
69
|
|
|
$this->url = $url; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return Route |
76
|
|
|
*/ |
77
|
|
|
public function toApi(): Route |
78
|
|
|
{ |
79
|
|
|
return $this->to('https://api.gitter.im/v1/'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return Route |
84
|
|
|
*/ |
85
|
|
|
public function toStream(): Route |
86
|
|
|
{ |
87
|
|
|
return $this->to('https://stream.gitter.im/v1/'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Route |
92
|
|
|
*/ |
93
|
|
|
public function toSocket(): Route |
94
|
|
|
{ |
95
|
|
|
return $this->to('wss://ws.gitter.im/'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return Route |
100
|
|
|
*/ |
101
|
|
|
public function toFaye(): Route |
102
|
|
|
{ |
103
|
|
|
return $this->to('https://gitter.im/api/v1/'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Route |
108
|
|
|
*/ |
109
|
|
|
public function toWebhook(): Route |
110
|
|
|
{ |
111
|
|
|
return $this->to('https://webhooks.gitter.im/e/'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string|null $route |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function route(string $route = null): string |
119
|
|
|
{ |
120
|
|
|
if ($route !== null) { |
121
|
|
|
$this->route = $route; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->route; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string|null $method |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function method(string $method = null): string |
132
|
|
|
{ |
133
|
|
|
if ($method !== null) { |
134
|
|
|
$this->method = strtoupper($method); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->method; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $parameter |
142
|
|
|
* @param string $value |
143
|
|
|
* @return $this|Route |
144
|
|
|
*/ |
145
|
|
|
public function with(string $parameter, string $value): Route |
146
|
|
|
{ |
147
|
|
|
$this->parameters[$parameter] = $value; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param array $parameters |
154
|
|
|
* @return $this|Route |
155
|
|
|
*/ |
156
|
|
|
public function withMany(array $parameters): Route |
157
|
|
|
{ |
158
|
|
|
foreach ($parameters as $parameter => $value) { |
159
|
|
|
$this->with($parameter, $value); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $field |
167
|
|
|
* @param $value |
168
|
|
|
* @return Route|$this |
169
|
|
|
*/ |
170
|
|
|
public function withBody(string $field, $value): Route |
171
|
|
|
{ |
172
|
|
|
$this->body[$field] = $value; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string|null |
179
|
|
|
*/ |
180
|
|
|
public function getBody() |
181
|
|
|
{ |
182
|
|
|
if (count($this->body)) { |
183
|
|
|
return json_encode($this->body); |
184
|
|
|
} |
185
|
|
|
return null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param array $parameters |
190
|
|
|
* @return string |
191
|
|
|
* @throws \InvalidArgumentException |
192
|
|
|
*/ |
193
|
|
|
public function build(array $parameters = []): string |
194
|
|
|
{ |
195
|
|
|
if ($this->url === null) { |
196
|
|
|
throw new \InvalidArgumentException('Can not build route string. URL does not set'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$route = $this->route; |
200
|
|
|
$query = $parameters = array_merge($this->parameters, $parameters); |
201
|
|
|
|
202
|
|
|
foreach ($parameters as $parameter => $value) { |
203
|
|
|
$updatedRoute = str_replace(sprintf('{%s}', $parameter), $value, $route); |
204
|
|
|
|
205
|
|
|
if ($updatedRoute !== $route) { |
206
|
|
|
unset($query[$parameter]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$route = $updatedRoute; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $this->url . $route . '?' . http_build_query($query); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string $name |
217
|
|
|
* @param array $arguments |
218
|
|
|
* @return static |
219
|
|
|
*/ |
220
|
|
|
public static function __callStatic(string $name, array $arguments = []) |
221
|
|
|
{ |
222
|
|
|
return new static($arguments[0] ?? '', $name); |
223
|
|
|
} |
224
|
|
|
} |