1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.8 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Http\Traits\Request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait Url |
19
|
|
|
* @package Quantum\Http\Request |
20
|
|
|
*/ |
21
|
|
|
trait Url |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Scheme |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private static $__protocol = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Host name |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private static $__host = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Server port |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private static $__port = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Request URI |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private static $__uri = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Gets the protocol |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public static function getProtocol(): ?string |
53
|
|
|
{ |
54
|
|
|
return self::$__protocol; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sets the protocol |
59
|
|
|
* @param string $protocol |
60
|
|
|
*/ |
61
|
|
|
public static function setProtocol(string $protocol) |
62
|
|
|
{ |
63
|
|
|
self::$__protocol = $protocol; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets the host name |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public static function getHost(): ?string |
71
|
|
|
{ |
72
|
|
|
return self::$__host; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sets the host name |
77
|
|
|
* @param string $host |
78
|
|
|
*/ |
79
|
|
|
public static function setHost(string $host) |
80
|
|
|
{ |
81
|
|
|
self::$__host = $host; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets the port |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public static function getPort(): ?string |
89
|
|
|
{ |
90
|
|
|
return self::$__port; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets the port |
95
|
|
|
* @param string $port |
96
|
|
|
*/ |
97
|
|
|
public static function setPort(string $port) |
98
|
|
|
{ |
99
|
|
|
self::$__port = $port; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets the URI |
104
|
|
|
* @return string|null |
105
|
|
|
*/ |
106
|
|
|
public static function getUri(): ?string |
107
|
|
|
{ |
108
|
|
|
return self::$__uri; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Sets the URI |
113
|
|
|
* @param string $uri |
114
|
|
|
*/ |
115
|
|
|
public static function setUri(string $uri) |
116
|
|
|
{ |
117
|
|
|
self::$__uri = ltrim($uri, '/'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns the URI segment at the specified index. |
123
|
|
|
* @param int $index |
124
|
|
|
* @return string|null |
125
|
|
|
*/ |
126
|
|
|
public static function getSegment(int $index): ?string |
127
|
|
|
{ |
128
|
|
|
$segments = self::getAllSegments(); |
129
|
|
|
|
130
|
|
|
if (isset($segments[$index])) { |
131
|
|
|
return $segments[$index]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Gets all URI segments as an array. |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public static function getAllSegments(): array |
142
|
|
|
{ |
143
|
|
|
$segments = explode('/', trim(parse_url(self::$__uri)['path'], '/')); |
144
|
|
|
array_unshift($segments, 'zero_segment'); |
145
|
|
|
return $segments; |
146
|
|
|
} |
147
|
|
|
} |