1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax. |
7
|
|
|
* |
8
|
|
|
* @see https://tools.ietf.org/html/rfc3986 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Vanderlee\Comprehend\Library; |
12
|
|
|
|
13
|
|
|
use Vanderlee\Comprehend\Builder\AbstractRuleset; |
14
|
|
|
use Vanderlee\Comprehend\Parser\Parser; |
15
|
|
|
use Vanderlee\Comprehend\Parser\Terminal\Nothing; |
16
|
|
|
|
17
|
|
|
require_once 'functions.php'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @property-read Parser IPv6address |
21
|
|
|
* @property-read Parser IPv4address |
22
|
|
|
* @property-read Parser sub_delims |
23
|
|
|
*/ |
24
|
|
|
class Rfc3986 extends AbstractRuleset |
25
|
|
|
{ |
26
|
|
|
protected static $name = 'Rfc3986'; |
27
|
|
|
|
28
|
4 |
|
public function __construct($overwrites = []) |
29
|
|
|
{ |
30
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
31
|
4 |
|
$abnf = new Rfc2234(); |
32
|
|
|
|
33
|
|
|
// Defined out-of-order for performance reasons |
34
|
|
|
$rules = [ |
35
|
|
|
// 2.1. Percent-Encoding |
36
|
4 |
|
'pct_encoded' => s('%', $abnf->HEXDIG, $abnf->HEXDIG), |
37
|
|
|
|
38
|
|
|
// 2.2. Reserved Characters |
39
|
4 |
|
'sub_delims' => set('!$&\'()*+,;='), |
40
|
4 |
|
'gen_delims' => set(':/?#[]@'), |
41
|
4 |
|
'reserved' => c($this->gen_delims, $this->sub_delims), |
|
|
|
|
42
|
|
|
|
43
|
|
|
// 2.3. Unreserved Characters |
44
|
4 |
|
'unreserved' => c($abnf->ALPHA, $abnf->DIGIT, set('-._~')), |
45
|
|
|
|
46
|
|
|
// 3.3. Path |
47
|
4 |
|
'pchar' => c($this->unreserved, $this->pct_encoded, $this->sub_delims, ':', '@'), |
|
|
|
|
48
|
4 |
|
'segment' => star($this->pchar), |
|
|
|
|
49
|
4 |
|
'segment_nz' => plus($this->pchar), |
50
|
4 |
|
'segment_nz_nc' => plus(c($this->unreserved, $this->pct_encoded, $this->sub_delims, '@')), |
51
|
4 |
|
'path_abempty' => star(['/', $this->segment]), |
|
|
|
|
52
|
4 |
|
'path_absolute' => s('/', opt([$this->segment_nz, star(['/', $this->segment])])), |
|
|
|
|
53
|
4 |
|
'path_noscheme' => s($this->segment_nz_nc, star(['/', $this->segment])), |
|
|
|
|
54
|
4 |
|
'path_rootless' => s($this->segment_nz, star(['/', $this->segment])), |
55
|
4 |
|
'path_empty' => new Nothing(), |
56
|
4 |
|
'path' => c($this->path_abempty, |
|
|
|
|
57
|
4 |
|
$this->path_absolute, |
|
|
|
|
58
|
4 |
|
$this->path_noscheme, |
|
|
|
|
59
|
4 |
|
$this->path_rootless, |
|
|
|
|
60
|
4 |
|
$this->path_empty |
|
|
|
|
61
|
|
|
), |
62
|
|
|
|
63
|
|
|
// 3.4. Query |
64
|
4 |
|
'query' => star(c($this->pchar, '/', '?')), |
65
|
|
|
|
66
|
|
|
// 3.5. Fragment |
67
|
4 |
|
'fragment ' => star(c($this->pchar, '/', '?')), |
68
|
|
|
|
69
|
|
|
// 3. Syntax Components |
70
|
4 |
|
'hier_part' => s('//', $this->authority, [ |
|
|
|
|
71
|
4 |
|
$this->path_abempty, |
72
|
4 |
|
$this->path_absolute, |
73
|
4 |
|
$this->path_rootless, |
74
|
4 |
|
$this->path_empty, |
75
|
|
|
]), |
76
|
4 |
|
'URI' => s($this->scheme, ':', $this->hier_part, opt(['?', $this->query]), |
|
|
|
|
77
|
4 |
|
opt(['#', $this->fragment])), |
|
|
|
|
78
|
|
|
|
79
|
|
|
// 3.1. Scheme |
80
|
4 |
|
'scheme' => s($abnf->ALPHA, star(c($abnf->ALPHA, $abnf->DIGIT, '+', '-', '.'))), |
81
|
|
|
|
82
|
|
|
// 3.2. Authority |
83
|
4 |
|
'authority' => s(opt([$this->userinfo, '@']), $this->host, opt([':', $this->port])), |
|
|
|
|
84
|
|
|
|
85
|
|
|
// 3.2.1. User Information |
86
|
4 |
|
'userinfo' => star(c($this->unreserved, $this->pct_encoded, $this->sub_delims, ':')), |
87
|
|
|
|
88
|
|
|
// 3.2.3. Port |
89
|
4 |
|
'port' => star($abnf->DIGIT), |
90
|
|
|
|
91
|
|
|
// 3.2.2. Host |
92
|
4 |
|
'h16' => repeat(1, 4, $abnf->HEXDIG), |
93
|
4 |
|
'ls32' => c([$this->h16, ':', $this->h16], $this->IPv4address), |
|
|
|
|
94
|
4 |
|
'IPv6address' => c( |
95
|
4 |
|
[repeat(6, 6, [$this->h16, ':']), $this->ls32], |
|
|
|
|
96
|
4 |
|
['::', repeat(5, 5, [$this->h16, ':']), $this->ls32], |
97
|
4 |
|
[opt($this->h16), '::', repeat(4, 4, [$this->h16, ':']), $this->ls32], |
98
|
|
|
[ |
99
|
4 |
|
opt([repeat(0, 1, [$this->h16, ':']), $this->h16]), |
100
|
4 |
|
'::', |
101
|
4 |
|
repeat(3, 3, [$this->h16, ':']), |
102
|
4 |
|
$this->ls32, |
103
|
|
|
], |
104
|
|
|
[ |
105
|
4 |
|
opt([repeat(0, 2, [$this->h16, ':']), $this->h16]), |
106
|
4 |
|
'::', |
107
|
4 |
|
repeat(2, 2, [$this->h16, ':']), |
108
|
4 |
|
$this->ls32, |
109
|
|
|
], |
110
|
4 |
|
[opt([repeat(0, 3, [$this->h16, ':']), $this->h16]), '::', $this->h16, ':', $this->ls32], |
111
|
4 |
|
[opt([repeat(0, 4, [$this->h16, ':']), $this->h16]), '::', $this->ls32], |
112
|
4 |
|
[opt([repeat(0, 5, [$this->h16, ':']), $this->h16]), '::', $this->h16], |
113
|
4 |
|
[opt([repeat(0, 6, [$this->h16, ':']), $this->h16]), '::'] |
114
|
|
|
), |
115
|
4 |
|
'dec_octet' => c( |
116
|
4 |
|
['25', range('0', '5')], // 250-255 |
117
|
4 |
|
['2', range('0', '4'), $abnf->DIGIT], // 200-249 |
118
|
4 |
|
['1', repeat(2, 2, $abnf->DIGIT)], // 100-199 |
119
|
4 |
|
[range('1', '9'), $abnf->DIGIT], // 10-99 |
120
|
4 |
|
$abnf->DIGIT // 0-9 |
121
|
|
|
), |
122
|
4 |
|
'IPv4address' => s($this->dec_octet, '.', $this->dec_octet, '.', $this->dec_octet, '.', $this->dec_octet), |
|
|
|
|
123
|
4 |
|
'IPvFuture' => s('v', plus($abnf->HEXDIG), '.', plus(c($this->unreserved, $this->sub_delims, ':'))), |
124
|
4 |
|
'IP_literal' => s('[', [$this->IPv6address, $this->IPvFuture], ']'), |
|
|
|
|
125
|
4 |
|
'reg_name' => star(c($this->unreserved, $this->pct_encoded, $this->sub_delims)), |
126
|
4 |
|
'host' => c($this->IP_literal, $this->IPv4address, $this->reg_name), |
|
|
|
|
127
|
|
|
|
128
|
|
|
// 4.2. Relative Reference |
129
|
4 |
|
'relative_part' => s('//', $this->authority, [ |
130
|
4 |
|
$this->path_abempty, |
131
|
4 |
|
$this->path_absolute, |
132
|
4 |
|
$this->path_noscheme, |
133
|
4 |
|
$this->path_empty, |
134
|
|
|
]), |
135
|
4 |
|
'relative_ref' => s($this->relative_part, opt(['?', $this->query]), opt(['#', $this->fragment])), |
|
|
|
|
136
|
|
|
|
137
|
|
|
// 4.1. URI Reference |
138
|
4 |
|
'URI_reference' => c($this->URI, $this->relative_ref), |
|
|
|
|
139
|
|
|
|
140
|
|
|
// 4.3. Absolute URI |
141
|
4 |
|
'absolute_URI' => s($this->scheme, ':', $this->hier_part, opt(['?', $this->query])), |
142
|
|
|
|
143
|
|
|
// self::ROOT => $this->rulelist, |
144
|
|
|
]; |
145
|
|
|
|
146
|
4 |
|
parent::__construct(array_merge($rules, $overwrites)); |
147
|
4 |
|
} |
148
|
|
|
} |
149
|
|
|
|