Skip to content

Commit 052395f

Browse files
committed
Releasing 1.0.3
2 parents 0799392 + daa4ca8 commit 052395f

File tree

2 files changed

+320
-0
lines changed

2 files changed

+320
-0
lines changed

src/TgUtils/URL.php

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
<?php
2+
3+
namespace TgUtils;
4+
5+
/**
6+
* Analyzes URLs and its components.
7+
* @author ralph
8+
*
9+
*/
10+
class URL {
11+
12+
protected $components;
13+
14+
/**
15+
* Constructor.
16+
* @param mixed string $url as string or other URL object.
17+
*/
18+
public function __construct($url) {
19+
if (is_string($url)) {
20+
// Trim to avoid problem with parsing
21+
$url = trim($url);
22+
// Ad the scheme if it is missing
23+
if (strpos($url, '://') === FALSE) {
24+
$url = 'http://'.$url;
25+
}
26+
$this->components = parse_url($url);
27+
28+
// And fixing some potential issues
29+
$this->fixIssues();
30+
} else if (is_object($url) && is_a($url, 'TgUtils\\URL')) {
31+
$this->components = $url->components;
32+
}
33+
}
34+
35+
protected function fixIssues() {
36+
$parts = array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment');
37+
foreach ($parts AS $part) {
38+
if (!isset($this->components[$part])) {
39+
$this->components[$part] = NULL;
40+
}
41+
}
42+
43+
if (!is_numeric($this->components['port'])) {
44+
$this->components['port'] = 0;
45+
}
46+
if ($this->components['path'] == NULL) {
47+
$this->components['path'] = '/';
48+
}
49+
if (substr($this->components['path'], 0, 1) != '/') {
50+
$this->components['path'] = '/' . $this->components['path'];
51+
}
52+
}
53+
54+
/**
55+
* Returns the URL scheme.
56+
* @return string the scheme
57+
*/
58+
public function getScheme() {
59+
return $this->components['scheme'];
60+
}
61+
62+
/**
63+
* Modify this URL by setting a new scheme.
64+
* @param string $scheme - the new scheme
65+
*/
66+
public function setScheme($scheme) {
67+
$this->components['scheme'] = $scheme;
68+
}
69+
70+
/**
71+
* Returns the URL's host.
72+
* @return string the host
73+
*/
74+
public function getHost() {
75+
return $this->components['host'];
76+
}
77+
78+
/**
79+
* Modify this URL by setting a new host.
80+
* @param string $host - the new host
81+
*/
82+
public function setHost($host) {
83+
$this->components['host'] = $host;
84+
}
85+
86+
/**
87+
* Returns the URL's port.
88+
* @return string the port
89+
*/
90+
public function getPort() {
91+
return $this->components['port'];
92+
}
93+
94+
/**
95+
* Modify this URL by setting a new port.
96+
* @param int $port - the new port
97+
*/
98+
public function setPort($port) {
99+
$this->components['port'] = $port;
100+
$this->fixIssues();
101+
}
102+
103+
/**
104+
* Returns the URL's user.
105+
* @return string the user
106+
*/
107+
public function getUser() {
108+
return $this->components['user'];
109+
}
110+
111+
/**
112+
* Modify this URL by setting a new user.
113+
* @param string $user - the new user
114+
*/
115+
public function setUser($user) {
116+
$this->components['user'] = $user;
117+
}
118+
119+
/**
120+
* Returns the URL's password.
121+
* @return string the password
122+
*/
123+
public function getPassword() {
124+
return $this->components['pass'];
125+
}
126+
127+
/**
128+
* Modify this URL by setting a new password.
129+
* @param string $password - the new password
130+
*/
131+
public function setPassword($password) {
132+
$this->components['pass'] = $password;
133+
}
134+
135+
/**
136+
* Returns the URL's path.
137+
* @return string the path
138+
*/
139+
public function getPath() {
140+
return $this->components['path'];
141+
}
142+
143+
/**
144+
* Modify this URL by setting a new path.
145+
* @param string $path - the new path
146+
*/
147+
public function setPath($path) {
148+
$this->components['path'] = $path;
149+
$this->fixIssues();
150+
}
151+
152+
/**
153+
* Returns the URL's query string.
154+
* @return string the query string
155+
*/
156+
public function getQuery() {
157+
return $this->components['query'];
158+
}
159+
160+
/**
161+
* Modify this URL by setting a new query string.
162+
* @param string $query - the new query string
163+
*/
164+
public function setQuery($query) {
165+
$this->components['query'] = $query;
166+
}
167+
168+
/**
169+
* Returns the URL's fragment.
170+
* @return string the fragment
171+
*/
172+
public function getFragment() {
173+
return $this->components['fragment'];
174+
}
175+
176+
/**
177+
* Modify this URL by setting a new fragment.
178+
* @param string $fragment - the new fragment
179+
*/
180+
public function setFragment($fragment) {
181+
$this->components['fragment'] = $fragment;
182+
}
183+
184+
public function __toString() {
185+
// Scheme
186+
$rc = $this->components['scheme'].'://';
187+
188+
// User
189+
if ($this->components['user'] != NULL) {
190+
$rc .= $this->components['user'];
191+
192+
// Password
193+
if ($this->components['pass'] != NULL) {
194+
$rc .= ':'.$this->components['pass'];
195+
}
196+
197+
$rc .= '@';
198+
}
199+
200+
// Host
201+
$rc .= $this->components['host'];
202+
203+
// Port
204+
if ($this->components['port'] != 0) {
205+
$rc .= ':'.$this->components['port'];
206+
}
207+
208+
// Path
209+
$rc .= $this->components['path'];
210+
211+
// Query
212+
if ($this->components['query'] != NULL) {
213+
$rc .= '?'.$this->components['query'];
214+
}
215+
216+
// Fragment
217+
if ($this->components['fragment'] != NULL) {
218+
$rc .= '#'.$this->components['fragment'];
219+
}
220+
221+
return $rc;
222+
}
223+
224+
/**
225+
* Returns whether to URLs are equal.
226+
* <p>Equality is defined as the equality of stringified URLs.</p>
227+
* @return boolean TRUE or FALSE
228+
*/
229+
public function equals($another) {
230+
if (is_string($another)) {
231+
$other = new URL($another);
232+
return $this->__toString() == $other->__toString();
233+
} else if (is_object($another) && is_a($another, 'TgUtils\\URL')) {
234+
return $this->__toString() == $another->__toString();
235+
}
236+
return FALSE;
237+
}
238+
}
239+

tests/TgUtils/URLTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TgUtils;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* Tests the various URL flavours and their correct parsing
9+
* @author ralph
10+
*
11+
*/
12+
class URLTest extends TestCase {
13+
14+
public function testWithHost(): void {
15+
$urlString = 'www.example.com';
16+
$url = new URL($urlString);
17+
$this->testComponents($url, 'http', NULL, NULL, 'www.example.com', 0, '/', NULL, NULL, 'http://www.example.com/');
18+
}
19+
20+
public function testWithSchemeHostPath(): void {
21+
$urlString = 'https://www.example.com/';
22+
$url = new URL($urlString);
23+
$this->testComponents($url, 'https', NULL, NULL, 'www.example.com', 0, '/', NULL, NULL, 'https://www.example.com/');
24+
}
25+
26+
public function testWithSchemeHostPath2(): void {
27+
$urlString = 'https://www.example.com/dev/index.php';
28+
$url = new URL($urlString);
29+
$this->testComponents($url, 'https', NULL, NULL, 'www.example.com', 0, '/dev/index.php', NULL, NULL, $urlString);
30+
}
31+
32+
public function testWithSchemeHostPathQuery(): void {
33+
$urlString = 'https://www.example.com/dev?name=value';
34+
$url = new URL($urlString);
35+
$this->testComponents($url, 'https', NULL, NULL, 'www.example.com', 0, '/dev', 'name=value', NULL, $urlString);
36+
}
37+
38+
public function testWithSchemeHostPathFragment(): void {
39+
$urlString = 'https://www.example.com/dev#anchor';
40+
$url = new URL($urlString);
41+
$this->testComponents($url, 'https', NULL, NULL, 'www.example.com', 0, '/dev', NULL, 'anchor', $urlString);
42+
}
43+
44+
public function testWithSchemeHostPathQueryFragment(): void {
45+
$urlString = 'https://www.example.com/dev?name=value#anchor';
46+
$url = new URL($urlString);
47+
$this->testComponents($url, 'https', NULL, NULL, 'www.example.com', 0, '/dev', 'name=value', 'anchor', $urlString);
48+
}
49+
50+
public function testWithSchemeUserHostPath(): void {
51+
$urlString = 'https://[email protected]/';
52+
$url = new URL($urlString);
53+
$this->testComponents($url, 'https', 'user', NULL, 'www.example.com', 0, '/', NULL, NULL, $urlString);
54+
}
55+
56+
public function testWithSchemeUserPasswordHostPath(): void {
57+
$urlString = 'https://user:[email protected]/';
58+
$url = new URL($urlString);
59+
$this->testComponents($url, 'https', 'user', 'password', 'www.example.com', 0, '/', NULL, NULL, $urlString);
60+
}
61+
62+
public function testWithSchemeUserHostPortPath(): void {
63+
$urlString = 'https://user:[email protected]:8443/';
64+
$url = new URL($urlString);
65+
$this->testComponents($url, 'https', 'user', 'password', 'www.example.com', 8443, '/', NULL, NULL, $urlString);
66+
}
67+
68+
protected function testComponents(URL $url, $scheme, $user, $pass, $host, $port, $path, $query, $fragment, $toString) {
69+
$this->assertEquals($scheme, $url->getScheme());
70+
$this->assertEquals($user, $url->getUser());
71+
$this->assertEquals($pass, $url->getPassword());
72+
$this->assertEquals($host, $url->getHost());
73+
$this->assertEquals($port, $url->getPort());
74+
$this->assertEquals($path, $url->getPath());
75+
$this->assertEquals($query, $url->getQuery());
76+
$this->assertEquals($fragment, $url->getFragment());
77+
$this->assertEquals($toString, $url->__toString());
78+
}
79+
80+
}
81+

0 commit comments

Comments
 (0)