-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.php
More file actions
60 lines (51 loc) · 1.24 KB
/
Server.php
File metadata and controls
60 lines (51 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace Gotify;
use Gotify\Exception\GotifyException;
/**
* Class for setting and validating a server URI
*/
final class Server
{
/** @var string $uri Server URI */
private string $uri = '';
/**
*
* @param string $uri Server URI
*/
public function __construct(string $uri)
{
$this->uri = $this->validate($uri);
}
/**
* Get server
*
* @return string Returns server URI
*/
public function get(): string
{
return $this->uri;
}
/**
* Validate server URI
*
* Checks if server URI starts with `https://` or `http://`.
*
* Checks if server URI ends with a forward slash and adds it if missing.
*
* @param string $uri Server URI
* @return string $uri Returns validated server URI
*
* @throws GotifyException if Server URL doesn't start with `https://` or `http://`.
*/
private function validate(string $uri): string
{
if (preg_match('/^https?:\/\//', $uri) === 0) {
throw new GotifyException('Server URI must start with https:// or http://');
}
if (substr($uri, -1) !== '/') {
$uri .= '/';
}
return $uri;
}
}