1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Uri; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class to help the buildup of an uri. |
7
|
|
|
* Wraps a string and provides methods to check and manipulate uri. |
8
|
|
|
*/ |
9
|
|
|
class Uri |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string $uri The boxed uri string. |
13
|
|
|
*/ |
14
|
|
|
private $uri; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Uri constructor. |
20
|
|
|
* |
21
|
|
|
* @param string $uri Initial uri-string. |
22
|
|
|
*/ |
23
|
33 |
|
public function __construct($uri) |
24
|
|
|
{ |
25
|
33 |
|
$this->uri = $uri; |
26
|
33 |
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Check if uri is empty by php standards with exception "0" string |
32
|
|
|
* (see http://php.net/manual/en/function.empty.php). |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
9 |
|
public function isEmpty() |
37
|
|
|
{ |
38
|
9 |
|
return empty($this->uri) && $this->uri !== "0"; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check if uri starts with string. |
45
|
|
|
* |
46
|
|
|
* Private helper class helper. |
47
|
|
|
* |
48
|
|
|
* @param string $string String to check for in start of uri. |
49
|
|
|
* |
50
|
|
|
* @return bool True if uri strarts with $string. |
51
|
|
|
*/ |
52
|
1 |
|
private function uriBeginsWith($string) |
53
|
|
|
{ |
54
|
1 |
|
$len = strlen($string); |
55
|
1 |
|
return substr($this->uri, 0, $len) == $string; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check if uri starts with one or more strings. |
62
|
|
|
* |
63
|
|
|
* @param string ...$string Strings to check for in start of uri. |
64
|
|
|
* |
65
|
|
|
* @return bool True if uri strarts with $string. |
66
|
|
|
*/ |
67
|
|
|
public function startsWith(...$strings) |
68
|
|
|
{ |
69
|
1 |
|
return array_reduce($strings, function ($collectedCondition, $string) { |
70
|
1 |
|
return $this->uriBeginsWith($string) || $collectedCondition; |
71
|
1 |
|
}, false); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Prepend this uri with another uri with a slash inbetween. |
78
|
|
|
* |
79
|
|
|
* Example: |
80
|
|
|
* $relativeUri = new Uri("about"); // $relativeUrl->uri() == "about" |
81
|
|
|
* $baseUrl = new Uri("http://dbwebb.se"); // $baseUrl->uri() == "http://dbwebb.se" |
82
|
|
|
* $urlString = $relativeUri->prepend($baseUrl)->uri(); // $urlString == "http://dbwebb.se/about" |
83
|
|
|
* |
84
|
|
|
* @param Uri $uri Uri to prepend this uri |
85
|
|
|
* |
86
|
|
|
* @return Uri self Reference to this Uri for chaining. |
87
|
|
|
*/ |
88
|
7 |
|
public function prepend(Uri $uri) |
89
|
|
|
{ |
90
|
7 |
|
$this->uri = $uri->uri() . "/" . ltrim($this->uri(), "/"); |
91
|
7 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Appends supplied $uri to this uri with a slash inbetween. |
98
|
|
|
* |
99
|
|
|
* Example: |
100
|
|
|
* $relativeUri = new Uri("about"); // $relativeUrl->uri() == "about" |
101
|
|
|
* $baseUrl = new Uri("http://dbwebb.se"); // $baseUrl->uri() == "http://dbwebb.se" |
102
|
|
|
* $urlString = $baseUri->append($relativeUri)->uri(); // $urlString == "http://dbwebb.se/about" |
103
|
|
|
* |
104
|
|
|
* @param Uri $uri Uri to append this uri |
105
|
|
|
* |
106
|
|
|
* @return Uri self Reference to this Uri for chaining. |
107
|
|
|
*/ |
108
|
7 |
|
public function append(Uri $uri) |
109
|
|
|
{ |
110
|
7 |
|
$this->uri = $this->uri() . "/" . ltrim($uri->uri(), "/"); |
111
|
7 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Remove the basename part of uri if it is same as argument. |
118
|
|
|
* |
119
|
|
|
* Example: |
120
|
|
|
* $theUri = new Uri("http://dbwebb.se/about/this.html"); |
121
|
|
|
* theUri->removeBasename("index.html"); // theUri->uri() == "http://dbwebb.se/about/this.html" |
122
|
|
|
* theUri->removeBasename("this.html"); // theUri->uri() == "http://dbwebb.se/about" |
123
|
|
|
* |
124
|
|
|
* @param string $basename The basename to remove. |
125
|
|
|
* |
126
|
|
|
* @return Uri self Reference to this Uri for chaining. |
127
|
|
|
*/ |
128
|
9 |
|
public function removeBasename($basename) |
129
|
|
|
{ |
130
|
9 |
|
$this->uri = basename($this->uri) == $basename |
131
|
9 |
|
? dirname($this->uri) |
132
|
9 |
|
: $this->uri; |
133
|
9 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the boxed uri as string without any trailing slash. |
140
|
|
|
* |
141
|
|
|
* @return string The uri |
142
|
|
|
*/ |
143
|
22 |
|
public function uri() |
144
|
|
|
{ |
145
|
22 |
|
return rtrim($this->uri, "/"); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|