URL.php
Code (php)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
class URL
{
private $url;
static public function format()
{
$args = func_get_args();
$url = vsprintf(array_shift($args), array_map('urlencode', $args));
return new self($url);
}
static public function current()
{
return new self($_SERVER['REQUEST_URI']);
}
public function __construct($url = null)
{
if($url)
$this->url = parse_url($url);
else
$this->url = array();
if(isset($this->url['query']))
$this->url['query'] = new URL_Query($this->url['query']);
else
$this->url['query'] = new URL_Query();
}
public function __get($part)
{
return $this->url[$part];
}
public function __isset($part)
{
return isset($this->url[$part]) && !is_null($this->url[$part]);
}
public function __set($part, $value)
{
switch($part) {
case 'scheme':
if(!self::isValidScheme($value))
throw new InvalidArgumentException('Scheme can only contain alphanummeric characters and digits');
break;
case 'host':
if(!self::isValidHost($value))
throw new InvalidArgumentException('Invalid Host');
break;
case 'port':
if(!self::isValidPort($value))
throw new InvalidArgumentException('Invalid port number');
break;
case 'path':
break;
case 'query':
if(is_string($value))
$value = $this->url['query']->merge(new URL_Query($value));
else if(!$value instanceof URL_Query)
throw new InvalidArgumentException('Query has to be a query-string or an instance of URL_Query');
break;
case 'fragment':
break;
default:
throw new InvalidArgumentException('Unknown part of url:' . $part);
}
$this->url[$part] = $value;
}
public function __toString()
{
return $this->generate();
}
public function generate()
{
$url = '';
if(!empty($this->url['scheme']))
$url .= $this->url['scheme'] . ':';
if(!empty($this->url['host']))
$url .= '//' . $this->url['host'];
if(!empty($this->url['port']))
$url .= ':' . $this->url['port'];
if(!empty($this->url['host']) || !empty($this->url['port']) ||
(!empty($this->url['path']) && substr($this->url['path'], 0, 1) == '/'))
$url .= '/';
if(!empty($this->url['path']))
$url .= ltrim($this->url['path'], '/');
if(count($this->url['query']))
$url .= '?' . $this->url['query']->generate();
if(!empty($this->url['fragment']))
$url .= '#' . $this->url['fragment'];
return $url;
}
public function merge(self $other)
{
$combined = new self();
$combined->url = array_merge($this->url, $other->url);
$combined->url['path'] = self::concatPath(
isset($this->url['path']) ? $this->url['path'] : '',
isset($other->url['path']) ? $other->url['path'] : '');
$combined->url['query'] = $this->url['query']->merge($other->url['query']);
return $combined;
}
static private function concatPath($left, $right)
{
if(strlen($left) == 0)
return $right;
if(strlen($right) == 0)
return $left;
$left = explode('/', $left);
$right = explode('/', $right);
array_pop($left); // bestand weghalen
while(current($right) == '..') {
array_pop($left);
array_shift($right);
}
$combined = array_merge($left, $right);
$check_empty = create_function('$string', 'return $string != ".";');
$combined = array_filter($combined, $check_empty);
return implode('/', $combined);
}
static public function isValidScheme($scheme)
{
return preg_match('/^[a-zA-Z]+[a-zA-Z0-9]*$/', $scheme);
}
static public function isValidHost($host)
{
return preg_match('/^([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+$/', $host);
}
static public function isValidPort($port)
{
return ctype_digit((string) $port);
}
}
class URL_Query extends ArrayObject
{
public function __construct($query = null)
{
if($query)
parse_str($query, $data);
else
$data = array();
if(!is_array($data))
throw new InvalidArgumentException('Query cannot be parsed with parse_str');
parent::__construct($data);
}
public function __toString()
{
return $this->generate();
}
public function merge(URL_Query $other)
{
$merged = new self;
$merged->exchangeArray(array_merge($this->getArrayCopy(), $other->getArrayCopy()));
return $merged;
}
public function generate()
{
return http_build_query($this);
}
}
class URL
{
private $url;
static public function format()
{
$args = func_get_args();
$url = vsprintf(array_shift($args), array_map('urlencode', $args));
return new self($url);
}
static public function current()
{
return new self($_SERVER['REQUEST_URI']);
}
public function __construct($url = null)
{
if($url)
$this->url = parse_url($url);
else
$this->url = array();
if(isset($this->url['query']))
$this->url['query'] = new URL_Query($this->url['query']);
else
$this->url['query'] = new URL_Query();
}
public function __get($part)
{
return $this->url[$part];
}
public function __isset($part)
{
return isset($this->url[$part]) && !is_null($this->url[$part]);
}
public function __set($part, $value)
{
switch($part) {
case 'scheme':
if(!self::isValidScheme($value))
throw new InvalidArgumentException('Scheme can only contain alphanummeric characters and digits');
break;
case 'host':
if(!self::isValidHost($value))
throw new InvalidArgumentException('Invalid Host');
break;
case 'port':
if(!self::isValidPort($value))
throw new InvalidArgumentException('Invalid port number');
break;
case 'path':
break;
case 'query':
if(is_string($value))
$value = $this->url['query']->merge(new URL_Query($value));
else if(!$value instanceof URL_Query)
throw new InvalidArgumentException('Query has to be a query-string or an instance of URL_Query');
break;
case 'fragment':
break;
default:
throw new InvalidArgumentException('Unknown part of url:' . $part);
}
$this->url[$part] = $value;
}
public function __toString()
{
return $this->generate();
}
public function generate()
{
$url = '';
if(!empty($this->url['scheme']))
$url .= $this->url['scheme'] . ':';
if(!empty($this->url['host']))
$url .= '//' . $this->url['host'];
if(!empty($this->url['port']))
$url .= ':' . $this->url['port'];
if(!empty($this->url['host']) || !empty($this->url['port']) ||
(!empty($this->url['path']) && substr($this->url['path'], 0, 1) == '/'))
$url .= '/';
if(!empty($this->url['path']))
$url .= ltrim($this->url['path'], '/');
if(count($this->url['query']))
$url .= '?' . $this->url['query']->generate();
if(!empty($this->url['fragment']))
$url .= '#' . $this->url['fragment'];
return $url;
}
public function merge(self $other)
{
$combined = new self();
$combined->url = array_merge($this->url, $other->url);
$combined->url['path'] = self::concatPath(
isset($this->url['path']) ? $this->url['path'] : '',
isset($other->url['path']) ? $other->url['path'] : '');
$combined->url['query'] = $this->url['query']->merge($other->url['query']);
return $combined;
}
static private function concatPath($left, $right)
{
if(strlen($left) == 0)
return $right;
if(strlen($right) == 0)
return $left;
$left = explode('/', $left);
$right = explode('/', $right);
array_pop($left); // bestand weghalen
while(current($right) == '..') {
array_pop($left);
array_shift($right);
}
$combined = array_merge($left, $right);
$check_empty = create_function('$string', 'return $string != ".";');
$combined = array_filter($combined, $check_empty);
return implode('/', $combined);
}
static public function isValidScheme($scheme)
{
return preg_match('/^[a-zA-Z]+[a-zA-Z0-9]*$/', $scheme);
}
static public function isValidHost($host)
{
return preg_match('/^([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+$/', $host);
}
static public function isValidPort($port)
{
return ctype_digit((string) $port);
}
}
class URL_Query extends ArrayObject
{
public function __construct($query = null)
{
if($query)
parse_str($query, $data);
else
$data = array();
if(!is_array($data))
throw new InvalidArgumentException('Query cannot be parsed with parse_str');
parent::__construct($data);
}
public function __toString()
{
return $this->generate();
}
public function merge(URL_Query $other)
{
$merged = new self;
$merged->exchangeArray(array_merge($this->getArrayCopy(), $other->getArrayCopy()));
return $merged;
}
public function generate()
{
return http_build_query($this);
}
}