|
1
|
|
|
<?php |
|
2
|
|
|
namespace One; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* FactoryUri Class |
|
6
|
|
|
* |
|
7
|
|
|
* @method create |
|
8
|
|
|
* @method createArticle |
|
9
|
|
|
* @method validateArray |
|
10
|
|
|
* @method validateUrl |
|
11
|
|
|
* @method validateInteger |
|
12
|
|
|
* @method validateString |
|
13
|
|
|
* @method checkData |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
class FactoryUri |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* function Create Uri |
|
21
|
|
|
* |
|
22
|
|
|
* @param String $string |
|
23
|
|
|
* @return object Uri |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function create($string = null) |
|
26
|
|
|
{ |
|
27
|
|
|
if (!empty($string)) { |
|
28
|
|
|
return self::createFromString($string); |
|
29
|
|
|
} |
|
30
|
|
|
return self::createFromServer(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function createFromString($uri) |
|
34
|
|
|
{ |
|
35
|
|
|
$data = parse_url(self::validateUrl($uri)); |
|
36
|
|
|
|
|
37
|
|
|
$scheme = self::validateString((string) self::checkData($data, 'scheme', '')); |
|
38
|
|
|
$user = self::validateString((string) self::checkData($data, 'user', '')); |
|
39
|
|
|
$pass = self::validateString((string) self::checkData($data, 'pass', '')); |
|
40
|
|
|
$host = self::validateString((string) self::checkData($data, 'host', '')); |
|
41
|
|
|
$port = self::validateInteger((int) self::checkData($data, 'port', null)); |
|
42
|
|
|
$path = self::validateString((string) self::checkData($data, 'path', '')); |
|
43
|
|
|
$query = self::validateString((string) self::checkData($data, 'query', '')); |
|
44
|
|
|
$fragment = self::validateString((string) self::checkData($data, 'fragment', '')); |
|
45
|
|
|
return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* functionality to check whether a variable is set or not. |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $parts |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
private static function checkData($data, $key, $default = '') |
|
55
|
|
|
{ |
|
56
|
|
|
return isset($data[$key]) ? $data[$key] : $default; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* function for Create Uri From Server |
|
61
|
|
|
* |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function createFromServer() |
|
64
|
|
|
{ |
|
65
|
|
|
$scheme = self::validateString((string) self::checkData($_SERVER, 'HTTPS', 'http://')); |
|
66
|
|
|
$host = self::validateString((string) self::checkData($_SERVER, 'HTTP_HOST', isset($_SERVER['SERVER_NAME']))); |
|
67
|
|
|
$port = self::validateInteger((int) self::checkData($_SERVER, 'SERVER_PORT', null)); |
|
68
|
|
|
$user = self::validateString((string) self::checkData($_SERVER, 'PHP_AUTH_USER', '')); |
|
69
|
|
|
$pass = self::validateString((string) self::checkData($_SERVER, 'PHP_AUTH_PW', '')); |
|
70
|
|
|
$path = (string) self::checkData($_SERVER, 'REQUEST_URI', ''); |
|
71
|
|
|
$path = self::validateString(parse_url('http://www.foobar.com/' . $path, PHP_URL_PATH)); |
|
72
|
|
|
$query = self::validateString((string) self::checkData($_SERVER, 'QUERY_STRING', '')); |
|
73
|
|
|
$fragment = ''; |
|
74
|
|
|
if (empty($user) && empty($pass) && !empty($_SERVER['HTTP_AUTHORIZATION'])) { |
|
75
|
|
|
list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); |
|
76
|
|
|
} |
|
77
|
|
|
return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Create Uri Object |
|
82
|
|
|
* |
|
83
|
|
|
* @param String $string |
|
84
|
|
|
* @param string $scheme |
|
85
|
|
|
* @param string $host |
|
86
|
|
|
* @param int $port |
|
87
|
|
|
* @param string $user |
|
88
|
|
|
* @param string $password |
|
89
|
|
|
* @param string $path |
|
90
|
|
|
* @param string $query |
|
91
|
|
|
* @param string $fragment |
|
92
|
|
|
* @return Uri Object |
|
93
|
|
|
*/ |
|
94
|
|
|
private static function createUri($scheme, $host, $port, $user, $password, $path, $query, $fragment) |
|
95
|
|
|
{ |
|
96
|
|
|
return new Uri( |
|
97
|
|
|
$scheme, |
|
98
|
|
|
$host, |
|
99
|
|
|
$port, |
|
100
|
|
|
$path, |
|
101
|
|
|
$query, |
|
102
|
|
|
$fragment, |
|
103
|
|
|
$user, |
|
104
|
|
|
$password |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Make Sure Url in string with correct url format |
|
110
|
|
|
* |
|
111
|
|
|
* @param String $string |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
private static function validateUrl($url) |
|
115
|
|
|
{ |
|
116
|
|
|
if (filter_var($url, FILTER_VALIDATE_URL) === false) { |
|
117
|
|
|
throw new \Exception("Invalid url : $url"); |
|
118
|
|
|
} |
|
119
|
|
|
return $url; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* functionality validity for int variables |
|
124
|
|
|
* |
|
125
|
|
|
* @param int $var |
|
126
|
|
|
* @return int |
|
127
|
|
|
*/ |
|
128
|
|
|
private static function validateInteger($var) |
|
129
|
|
|
{ |
|
130
|
|
|
if (filter_var($var, FILTER_VALIDATE_INT) === false) { |
|
131
|
|
|
throw new \Exception("The variable must be a integer :" . $var); |
|
132
|
|
|
} |
|
133
|
|
|
return $var; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* functionality validity for string variables |
|
138
|
|
|
* |
|
139
|
|
|
* @param String $var |
|
140
|
|
|
* @return String |
|
141
|
|
|
*/ |
|
142
|
|
|
private static function validateString($var) |
|
143
|
|
|
{ |
|
144
|
|
|
if (is_string($var) == true) { |
|
|
|
|
|
|
145
|
|
|
return $var; |
|
146
|
|
|
} |
|
147
|
|
|
throw new \Exception("The variable must be a string :" . $var); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.