-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrl.php
More file actions
92 lines (82 loc) · 2.12 KB
/
Url.php
File metadata and controls
92 lines (82 loc) · 2.12 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
<?php
namespace Depage\Http;
/**
* brief Url
* Class Url
*/
class Url
{
/**
* @brief url
**/
protected $url = "";
// {{{ __construct()
/**
* @brief __construct
*
* @param mixed $
* @return void
**/
public function __construct($url)
{
$this->url = $url;
}
// }}}
// {{{ getRelativePathTo()
/**
* @brief getRelativePathTo
*
* @param mixed $targetPath
* @return void
**/
public function getRelativePathTo($targetPath)
{
// link to self by default
$path = '';
if ($targetPath != '' && $targetPath != $this->url) {
$currentPath = explode('/', $this->url);
$targetPath = explode('/', $targetPath);
$i = 0;
while ((isset($currentPath[$i]) && $targetPath[$i]) && $currentPath[$i] == $targetPath[$i]) {
$i++;
}
if (count($currentPath) - $i >= 1) {
$path = str_repeat('../', count($currentPath) - $i - 1) . implode('/', array_slice($targetPath, $i));
}
}
return $path;
}
// }}}
// {{{ getAbsolutePathTo()
/**
* @brief getAbsolutePathTo
*
* @param mixed $targetPath
* @return void
**/
public function getAbsolutePathTo($targetPath)
{
if (substr($targetPath, 0, 1) == "/") {
return $targetPath;
} else if (parse_url($targetPath, PHP_URL_HOST) != "") {
return $targetPath;
}
// @todo throw exception when out of bounds
$currentPath = explode('/', $this->url);
array_pop($currentPath);
$targetPath = explode('/', $targetPath);
while (count($targetPath) > 0) {
$part = array_shift($targetPath);
if ($part == "." || $part == "") {
// stay on same level
} else if ($part == "..") {
array_pop($currentPath);
} else {
$currentPath[] = $part;
}
}
return implode('/', $currentPath);
}
// }}}
}
/* vim:set ft=php sw=4 sts=4 fdm=marker et : */