Completed
Push — master ( d9806b...bcb6f0 )
by smiley
02:51
created

URL   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 20
c 7
b 0
f 2
lcom 1
cbo 0
dl 0
loc 153
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A __get() 0 3 1
A __toString() 0 3 1
A originalParams() 0 3 1
A overrideParams() 0 3 1
A mergeParams() 0 3 1
B parseUrl() 0 15 8
B getURL() 0 22 5
1
<?php
2
/**
3
 *
4
 * @filesource   URL.php
5
 * @created      18.02.2016
6
 * @package      chillerlan\TinyCurl
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2016 Smiley
9
 * @license      MIT
10
 */
11
12
namespace chillerlan\TinyCurl;
13
14
/**
15
 * Class URL
16
 *
17
 * @property string url
18
 * @property string method
19
 * @property string scheme
20
 * @property string host
21
 * @property string port
22
 * @property string path
23
 * @property string query
24
 * @property string fragment
25
 * @property array  params
26
 * @property array  parsedquery
27
 */
28
class URL{
29
30
	/**
31
	 * @var string
32
	 */
33
	protected $url;
34
35
	/**
36
	 * @var string
37
	 */
38
	protected $method;
39
40
	/**
41
	 * @var string
42
	 */
43
	protected $scheme;
44
45
	/**
46
	 * @var string
47
	 */
48
	protected $host;
49
50
	/**
51
	 * @var int
52
	 */
53
	protected $port;
54
55
	/**
56
	 * @var string
57
	 */
58
	protected $path;
59
60
	/**
61
	 * @var string
62
	 */
63
	protected $query;
64
65
	/**
66
	 * @var string
67
	 */
68
	protected $fragment;
69
70
	/**
71
	 * @var array
72
	 */
73
	protected $parsedquery = [];
74
75
	/**
76
	 * @var array
77
	 */
78
	protected $params = [];
79
80
	/**
81
	 * URL constructor.
82
	 *
83
	 * @param string $url
84
	 * @param array  $params
85
	 * @param string $method
86
	 */
87
	public function __construct($url, array $params = [], $method = 'GET'){
88
		$this->url    = $url;
89
		$this->params = $params;
90
91
		if(in_array(strtoupper($method), ['GET', 'POST'])){
92
			$this->method = $method;
93
		}
94
95
		$this->parseUrl();
96
	}
97
98
	/**
99
	 * @param $name
100
	 *
101
	 * @return mixed
102
	 */
103
	public function __get($name){
104
		return $this->{$name};
105
	}
106
107
	/**
108
	 * @return string URL with merged params
109
	 */
110
	public function __toString(){
111
		return $this->mergeParams();
112
	}
113
114
	/**
115
	 * @return string
116
	 */
117
	public function originalParams(){
118
		return $this->getURL().'?'.http_build_query($this->parsedquery);
119
	}
120
121
	/**
122
	 * @return string
123
	 */
124
	public function overrideParams(){
125
		return $this->getURL().'?'.http_build_query($this->params);
126
	}
127
128
	/**
129
	 * @return string
130
	 */
131
	public function mergeParams(){
132
		return $this->getURL().'?'.http_build_query(array_merge($this->parsedquery, $this->params));
133
	}
134
135
	/**
136
	 * @return void
137
	 */
138
	protected function parseUrl(){
139
		$url = parse_url($this->url);
140
		
141
		$this->host      = !isset($url['host'])      ? null : $url['host'];
142
		$this->port      = !isset($url['port'])      ? null : $url['port'];
143
		$this->path      = !isset($url['path'])      ? null : $url['path'];
144
		$this->scheme    = !isset($url['scheme'])    ? null : $url['scheme'];
145
		$this->query     = !isset($url['query'])     ? null : $url['query'];
146
		$this->fragment  = !isset($url['fragment'])  ? null : $url['fragment'];
147
148
		if($this->query){
149
			parse_str($this->query, $this->parsedquery);
150
		}
151
152
	}
153
154
	/**
155
	 * @return string
156
	 */
157
	protected function getURL(){
158
		$url = '';
159
160
		if($this->scheme){
161
			$url .= $this->scheme.':';
162
		}
163
164
		if($this->host){
165
			$url .= '//'.$this->host;
166
167
			if($this->port){
168
				$url .= ':'.$this->port;
169
			}
170
171
		}
172
173
		if($this->path){
174
			$url .= $this->path;
175
		}
176
177
		return $url;
178
	}
179
180
}
181