forked from appwrite/appwrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURL.php
More file actions
121 lines (100 loc) · 2.95 KB
/
Copy pathURL.php
File metadata and controls
121 lines (100 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Appwrite\URL;
class URL
{
/**
* Parse URL
*
* Take a URL string and split it to array parts
*
* @param string $url
*
* @return array
*/
public static function parse(string $url): array
{
$default = [
'scheme' => '',
'pass' => '',
'user' => '',
'host' => '',
'port' => null,
'path' => '',
'query' => '',
'fragment' => '',
];
$parsed = \parse_url($url);
if (is_array($parsed)) {
return \array_merge($default, $parsed);
}
// see if $url is just a scheme
if (preg_match('/^([a-z][a-z0-9+.-]*):/i', $url, $matches)) {
$scheme = $matches[1];
return \array_merge($default, [
'scheme' => $scheme
]);
}
throw new \InvalidArgumentException('Invalid URL: ' . $url);
}
/**
* Un-Parse URL
*
* Take URL parts and combine them to a valid string
*
* @param array $url
* @param array $ommit
*
* @return string
*/
public static function unparse(array $url, array $ommit = []): string
{
if (isset($url['path']) && \mb_substr($url['path'], 0, 1) !== '/') {
$url['path'] = '/' . $url['path'];
}
$parts = [];
$parts['scheme'] = isset($url['scheme']) ? $url['scheme'] . '://' : '';
$parts['host'] = isset($url['host']) ? $url['host'] : '';
$parts['port'] = isset($url['port']) ? ':' . $url['port'] : '';
$parts['user'] = isset($url['user']) ? $url['user'] : '';
$parts['pass'] = !empty($url['pass']) ? ':' . $url['pass'] : '';
$parts['pass'] = ($parts['user'] || !empty($parts['pass'])) ? $parts['pass'] . '@' : '';
$parts['path'] = isset($url['path']) ? $url['path'] : '';
$parts['query'] = isset($url['query']) && !empty($url['query']) ? '?' . $url['query'] : '';
$parts['fragment'] = isset($url['fragment']) ? '#' . $url['fragment'] : '';
if ($ommit) {
foreach ($ommit as $key) {
if (isset($parts[ $key ])) {
$parts[ $key ] = '';
}
}
}
return $parts['scheme'] . $parts['user'] . $parts['pass'] . $parts['host'] . $parts['port'] . $parts['path'] . $parts['query'] . $parts['fragment'];
}
/**
* Parse Query String
*
* Convert query string to array
*
* @param string $query
*
* @return array
*/
public static function parseQuery(string $query): array
{
\parse_str($query, $result);
return $result;
}
/**
* Un-Parse Query String
*
* Convert query string array to string
*
* @param array $query
*
* @return string
*/
public static function unparseQuery(array $query): string
{
return \http_build_query($query);
}
}