|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Utilities\Router; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* URLs class |
|
8
|
|
|
* |
|
9
|
|
|
* @link https://github.com/utilities-php/router |
|
10
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
|
11
|
|
|
* @license https://github.com/utilities-php/router/blob/master/LICENSE (MIT License) |
|
12
|
|
|
*/ |
|
13
|
|
|
class URLs |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param bool $withoutQuery If true, the query string will be removed from the url |
|
18
|
|
|
* @return string |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function getURL(bool $withoutQuery = true): string |
|
21
|
|
|
{ |
|
22
|
|
|
if (str_contains($_SERVER['REQUEST_URI'], "?") && $withoutQuery) { |
|
23
|
|
|
return explode("?", $_SERVER['REQUEST_URI'])[0]; |
|
24
|
|
|
} |
|
25
|
|
|
return $_SERVER['REQUEST_URI']; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param int $index Get the segment by index |
|
30
|
|
|
* @return string|false |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function segment(int $index): string|false |
|
33
|
|
|
{ |
|
34
|
|
|
$segments = self::getSegments(); |
|
35
|
|
|
|
|
36
|
|
|
if (isset($segments[$index])) { |
|
37
|
|
|
return $segments[$index]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function getSegments(): array |
|
47
|
|
|
{ |
|
48
|
|
|
if (str_contains($_SERVER['REQUEST_URI'], "?")) { |
|
49
|
|
|
if ($_SERVER['QUERY_STRING'] != null) { |
|
50
|
|
|
$url = str_replace("?{$_SERVER['QUERY_STRING']}", "", $_SERVER['REQUEST_URI']); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$url = $url ?? $_SERVER['REQUEST_URI']; |
|
55
|
|
|
|
|
56
|
|
|
if (strlen($url) > 1 && str_contains($url, "/")) { |
|
57
|
|
|
if ($url[0] == "/") { |
|
58
|
|
|
$url = substr($url, 1); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($url[strlen($url) - 1] == "?") { |
|
62
|
|
|
$url = substr($url, 0, strlen($url) - 1); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return explode('/', $url ?? $_SERVER['REQUEST_URI']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return []; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param array $extra (optional) extra query string parameters |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function QueryString(array $extra = []): array |
|
76
|
|
|
{ |
|
77
|
|
|
$raw_data = explode("&", $_SERVER['QUERY_STRING'] ?? ''); |
|
78
|
|
|
$result = []; |
|
79
|
|
|
|
|
80
|
|
|
if (count($raw_data) > 0 && $raw_data[0] != "") { |
|
81
|
|
|
foreach ($raw_data as $Query) { |
|
82
|
|
|
[$key, $val] = explode("=", $Query); |
|
83
|
|
|
$result[$key] = urldecode($val); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
foreach ($extra as $key => $val) { |
|
88
|
|
|
$result[$key] = $val; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $result; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $url (without http:// and domain) (ex: /home/shahrad/testEcho) |
|
96
|
|
|
* @param string $pattern Example: '/{sector}/{subsector}/{method}/' |
|
97
|
|
|
* @return array the matched segments |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function parseURL(string $url, string $pattern): array |
|
100
|
|
|
{ |
|
101
|
|
|
$url = explode('/', $url); |
|
102
|
|
|
$pattern = explode('/', $pattern); |
|
103
|
|
|
$data = []; |
|
104
|
|
|
foreach ($pattern as $key => $value) { |
|
105
|
|
|
if (str_contains($value, '{')) { |
|
106
|
|
|
$data[str_replace('{', '', str_replace('}', '', $value))] = $url[$key]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
return $data; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Find index of segment by use a keyword |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $keyword |
|
116
|
|
|
* @param bool $insensitive (optional) if true, the keyword will be case-insensitive |
|
117
|
|
|
* @return int |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function findIndexOfSegment(string $keyword, bool $insensitive = false): int |
|
120
|
|
|
{ |
|
121
|
|
|
if ($insensitive) { |
|
122
|
|
|
$keyword = strtolower($keyword); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
foreach (URLs::getSegments() as $index => $value) { |
|
126
|
|
|
if ($insensitive) { |
|
127
|
|
|
$value = strtolower($value); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($value == $keyword) { |
|
131
|
|
|
return $index; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return -1; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |