1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenEngine\Mika\Core\Components\Http\Message\Uri\Traits; |
4
|
|
|
|
5
|
|
|
trait QueryTrait |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $path; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $query; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $fragment; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Retrieve the path component of the URI. |
24
|
|
|
* |
25
|
|
|
* The path can either be empty or absolute (starting with a slash) or |
26
|
|
|
* rootless (not starting with a slash). Implementations MUST support all |
27
|
|
|
* three syntaxes. |
28
|
|
|
* |
29
|
|
|
* Normally, the empty path "" and absolute path "/" are considered equal as |
30
|
|
|
* defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically |
31
|
|
|
* do this normalization because in contexts with a trimmed base path, e.g. |
32
|
|
|
* the front controller, this difference becomes significant. It's the task |
33
|
|
|
* of the user to handle both "" and "/". |
34
|
|
|
* |
35
|
|
|
* The value returned MUST be percent-encoded, but MUST NOT double-encode |
36
|
|
|
* any characters. To determine what characters to encode, please refer to |
37
|
|
|
* RFC 3986, Sections 2 and 3.3. |
38
|
|
|
* |
39
|
|
|
* As an example, if the value should include a slash ("/") not intended as |
40
|
|
|
* delimiter between path segments, that value MUST be passed in encoded |
41
|
|
|
* form (e.g., "%2F") to the instance. |
42
|
|
|
* |
43
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-2 |
44
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.3 |
45
|
|
|
* @return string The URI path. |
46
|
|
|
*/ |
47
|
|
|
public function getPath(): string |
48
|
|
|
{ |
49
|
|
|
return $this->path; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Retrieve the query string of the URI. |
54
|
|
|
* |
55
|
|
|
* If no query string is present, this method MUST return an empty string. |
56
|
|
|
* |
57
|
|
|
* The leading "?" character is not part of the query and MUST NOT be |
58
|
|
|
* added. |
59
|
|
|
* |
60
|
|
|
* The value returned MUST be percent-encoded, but MUST NOT double-encode |
61
|
|
|
* any characters. To determine what characters to encode, please refer to |
62
|
|
|
* RFC 3986, Sections 2 and 3.4. |
63
|
|
|
* |
64
|
|
|
* As an example, if a value in a key/value pair of the query string should |
65
|
|
|
* include an ampersand ("&") not intended as a delimiter between values, |
66
|
|
|
* that value MUST be passed in encoded form (e.g., "%26") to the instance. |
67
|
|
|
* |
68
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-2 |
69
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.4 |
70
|
|
|
* @return string The URI query string. |
71
|
|
|
*/ |
72
|
|
|
public function getQuery(): string |
73
|
|
|
{ |
74
|
|
|
return $this->query; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Retrieve the fragment component of the URI. |
79
|
|
|
* |
80
|
|
|
* If no fragment is present, this method MUST return an empty string. |
81
|
|
|
* |
82
|
|
|
* The leading "#" character is not part of the fragment and MUST NOT be |
83
|
|
|
* added. |
84
|
|
|
* |
85
|
|
|
* The value returned MUST be percent-encoded, but MUST NOT double-encode |
86
|
|
|
* any characters. To determine what characters to encode, please refer to |
87
|
|
|
* RFC 3986, Sections 2 and 3.5. |
88
|
|
|
* |
89
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-2 |
90
|
|
|
* @see https://tools.ietf.org/html/rfc3986#section-3.5 |
91
|
|
|
* @return string The URI fragment. |
92
|
|
|
*/ |
93
|
|
|
public function getFragment(): string |
94
|
|
|
{ |
95
|
|
|
return $this->fragment; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $path |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
private function setPath(?string $path): self |
104
|
|
|
{ |
105
|
|
|
$this->path = $path ?? ''; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $query |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
private function setQuery(?string $query): self |
114
|
|
|
{ |
115
|
|
|
$this->query = $query ?? ''; |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param null|string $fragment |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
private function setFragment(?string $fragment): self |
125
|
|
|
{ |
126
|
|
|
$this->fragment = $fragment ?? ''; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|