1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP Generics package. |
5
|
|
|
* |
6
|
|
|
* @package Generics |
7
|
|
|
*/ |
8
|
|
|
namespace Generics\Util; |
9
|
|
|
|
10
|
|
|
use Generics\Socket\InvalidUrlException; |
11
|
|
|
use Generics\Socket\Url; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This class provides a parser to retrieve Url objects out of arbitrary URIs |
15
|
|
|
* |
16
|
|
|
* @author Maik Greubel <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class UrlParser { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Parse a URI into a Url |
22
|
|
|
* |
23
|
|
|
* @param string $url |
24
|
|
|
* @throws InvalidUrlException |
25
|
|
|
* @return \Generics\Socket\Url |
26
|
|
|
*/ |
27
|
17 |
|
public static function parseUrl($url) { |
28
|
17 |
|
$parts = parse_url ( $url ); |
29
|
|
|
|
30
|
17 |
View Code Duplication |
if (false === $parts || false === Arrays::hasElement ( $parts, 'host' )) { |
|
|
|
|
31
|
14 |
|
throw new InvalidUrlException ( 'This URL does not contain a host part' ); |
32
|
|
|
} |
33
|
14 |
View Code Duplication |
if (false === $parts || false === Arrays::hasElement ( $parts, 'scheme' )) { |
|
|
|
|
34
|
1 |
|
throw new InvalidUrlException ( 'This URL does not contain a scheme part' ); |
35
|
|
|
} |
36
|
|
|
|
37
|
13 |
|
$address = $parts ['host']; |
38
|
13 |
|
$scheme = $parts ['scheme']; |
39
|
13 |
|
$port = 0; |
40
|
13 |
|
$path = "/"; |
41
|
|
|
|
42
|
13 |
|
if (isset ( $parts ['port'] )) { |
43
|
2 |
|
$port = intval ( $parts ['port'] ); |
44
|
|
|
} |
45
|
|
|
|
46
|
13 |
|
if ($port == 0) { |
47
|
11 |
|
$port = self::getPortByScheme ( $scheme ); |
48
|
|
|
} |
49
|
|
|
|
50
|
11 |
|
if (isset ( $parts ['path'] )) { |
51
|
10 |
|
$path = $parts ['path']; |
52
|
|
|
} |
53
|
|
|
|
54
|
11 |
|
return new Url ( $address, $port, $path, $scheme ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get port number by scheme name. |
59
|
|
|
* The port will be the default which is defined by |
60
|
|
|
* http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers |
61
|
|
|
* |
62
|
|
|
* @param string $scheme |
63
|
|
|
* The scheme. |
64
|
|
|
* @throws InvalidUrlException |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
11 |
|
public static function getPortByScheme($scheme) { |
68
|
|
|
switch ($scheme) { |
69
|
11 |
|
case 'http' : |
|
|
|
|
70
|
5 |
|
return 80; |
71
|
|
|
|
72
|
6 |
|
case 'https' : |
|
|
|
|
73
|
2 |
|
return 443; |
74
|
|
|
|
75
|
4 |
|
case 'ftp' : |
|
|
|
|
76
|
2 |
|
return 21; |
77
|
|
|
|
78
|
|
|
default : |
|
|
|
|
79
|
2 |
|
throw new InvalidUrlException ( "Scheme {scheme} is not handled!", array ( |
80
|
2 |
|
'scheme' => $scheme |
81
|
|
|
) ); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.