|
1
|
|
|
<?php |
|
2
|
|
|
namespace Subreality\Dilmun\Anshar\Http\UriParts; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class Scheme |
|
6
|
|
|
* @package Subreality\Dilmun\Anshar\Http\UriParts |
|
7
|
|
|
*/ |
|
8
|
|
|
class Scheme |
|
9
|
|
|
{ |
|
10
|
|
|
private static $valid_pattern = '/^[a-z][\w\+\-\.]*$/i'; |
|
11
|
|
|
|
|
12
|
|
|
private $scheme = ""; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Scheme constructor. Accepts a string representing a URI scheme. Construction will throw an exception if the |
|
16
|
|
|
* scheme is either not a string or does not conform to the RFC3986 URI scheme specification. |
|
17
|
|
|
* |
|
18
|
|
|
* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) |
|
19
|
|
|
* |
|
20
|
|
|
* @see https://tools.ietf.org/html/rfc3986#appendix-A |
|
21
|
|
|
* |
|
22
|
|
|
* @throws \InvalidArgumentException |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $scheme A string representing a URI scheme |
|
25
|
|
|
*/ |
|
26
|
12 |
|
public function __construct($scheme) |
|
27
|
|
|
{ |
|
28
|
12 |
|
if (!is_string($scheme)) { |
|
29
|
6 |
|
throw new \InvalidArgumentException("Scheme must be a string"); |
|
30
|
6 |
|
} elseif (!empty($scheme) && !$this->isValid($scheme)) { |
|
31
|
3 |
|
throw new \InvalidArgumentException("Scheme must conform to RFC3986 specification"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
3 |
|
$this->scheme = $scheme; |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
public function __toString() |
|
38
|
|
|
{ |
|
39
|
3 |
|
return $this->scheme; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Determines whether a given scheme adheres to the RFC3986 specification: |
|
44
|
|
|
* |
|
45
|
|
|
* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) |
|
46
|
|
|
* |
|
47
|
|
|
* @see https://tools.ietf.org/html/rfc3986#appendix-A |
|
48
|
|
|
* @see Scheme::$valid_pattern |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $scheme The scheme to check for validity |
|
51
|
|
|
* @return bool Returns true if the provided scheme matches the RFC3986 specification |
|
52
|
|
|
* Returns false otherwise |
|
53
|
|
|
*/ |
|
54
|
10 |
|
public static function isValid($scheme) |
|
55
|
|
|
{ |
|
56
|
10 |
|
return (bool) preg_match(self::$valid_pattern, $scheme); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
public function toUriString() |
|
60
|
|
|
{ |
|
61
|
2 |
|
$uri_scheme = $this->scheme; |
|
62
|
|
|
|
|
63
|
2 |
|
if (!empty($this->scheme)) { |
|
64
|
1 |
|
$uri_scheme .= ":"; |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return $uri_scheme; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|