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 extends AbstractUriPart |
9
|
|
|
{ |
10
|
|
|
protected static $compositions = array(); |
11
|
|
|
|
12
|
|
|
/** @noinspection PhpMissingParentConstructorInspection */ |
13
|
|
|
/** |
14
|
|
|
* Scheme constructor. Accepts a string representing a URI scheme. Construction will throw an exception if the |
15
|
|
|
* scheme is either not a string or does not conform to the RFC3986 URI scheme specification. |
16
|
|
|
* |
17
|
|
|
* Construction also results in the scheme string being converted to lowercase per RFC3986. |
18
|
|
|
* |
19
|
|
|
* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) |
20
|
|
|
* |
21
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.1 |
22
|
|
|
* @see https://tools.ietf.org/html/rfc3986#appendix-A |
23
|
|
|
* |
24
|
|
|
* @throws \InvalidArgumentException |
25
|
|
|
* |
26
|
|
|
* @param string $scheme A string representing a URI scheme |
27
|
|
|
*/ |
28
|
78 |
|
public function __construct($scheme) |
29
|
|
|
{ |
30
|
78 |
|
$this->compileValidPattern(); |
31
|
|
|
|
32
|
78 |
|
if (!is_string($scheme)) { |
33
|
12 |
|
throw new \InvalidArgumentException("Scheme must be a string"); |
34
|
66 |
|
} elseif (!$this->isValid($scheme)) { |
35
|
3 |
|
throw new \InvalidArgumentException("Scheme must conform to RFC3986 specification"); |
36
|
|
|
} |
37
|
|
|
|
38
|
63 |
|
$this->data = strtolower($scheme); |
39
|
63 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns a string representation of the scheme formatted so that it can be compiled into a complete URI string |
43
|
|
|
* per the Uri object's string specification. |
44
|
|
|
* |
45
|
|
|
* If the scheme is empty, toUriString returns an empty string; if the scheme is not empty, toUriString returns the |
46
|
|
|
* scheme suffixed with a colon. |
47
|
|
|
* |
48
|
|
|
* @see Uri::__toString |
49
|
|
|
* |
50
|
|
|
* @return string A string representation of the scheme formatted for a complete URI string |
51
|
|
|
*/ |
52
|
18 |
|
public function toUriString() |
53
|
|
|
{ |
54
|
18 |
|
$uri_scheme = parent::toUriString(); //returns data string |
55
|
|
|
|
56
|
18 |
|
if (!empty($this->data)) { |
57
|
16 |
|
$uri_scheme .= ":"; |
58
|
16 |
|
} |
59
|
|
|
|
60
|
18 |
|
return $uri_scheme; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Compiles a regexp pattern based on predefined patterns that define allowed characters for a scheme. |
65
|
|
|
*/ |
66
|
86 |
|
protected static function compileValidPattern() |
67
|
|
|
{ |
68
|
86 |
|
self::$part_pattern = '[a-z][\w\+\-\.]*'; |
69
|
86 |
|
self::$valid_pattern = '/^$|^[a-z][\w\+\-\.]*$/i'; //the beginning '^$|' part allows for empty schemes |
70
|
86 |
|
} |
71
|
|
|
} |
72
|
|
|
|