Completed
Push — master ( 21fcc7...da5913 )
by smiley
02:45
created

URL::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
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
109
	 */
110
	public function __toString(){
111
		return $this->mergeParams();
112
	}
113
114
	/**
115
	 * @return string
116
	 */
117
	public function overrideParams(){
118
		$url  = $this->getURL();
119
		$url .= '?'.http_build_query($this->params);
120
121
		return $url;
122
	}
123
124
	/**
125
	 * @return string
126
	 */
127
	public function mergeParams(){
128
		$url  = $this->getURL();
129
		$url .= '?'.http_build_query(array_merge($this->parsedquery, $this->params));
130
131
		return $url;
132
	}
133
134
	/**
135
	 * @return void
136
	 */
137
	protected function parseUrl(){
138
		$url = parse_url($this->url);
139
		
140
		$this->host      = !isset($url['host'])      ? null : $url['host'];
141
		$this->port      = !isset($url['port'])      ? null : $url['port'];
142
		$this->path      = !isset($url['path'])      ? null : $url['path'];
143
		$this->scheme    = !isset($url['scheme'])    ? null : $url['scheme'];
144
		$this->query     = !isset($url['query'])     ? null : $url['query'];
145
		$this->fragment  = !isset($url['fragment'])  ? null : $url['fragment'];
146
147
		if($this->query){
148
			parse_str($this->query, $this->parsedquery);
149
		}
150
151
	}
152
153
	/**
154
	 * @return string
155
	 */
156
	protected function getURL(){
157
		$url = '';
158
159
		if($this->scheme){
160
			$url .= $this->scheme.':';
161
		}
162
163
		if($this->host){
164
			$url .= '//'.$this->host;
165
166
			if($this->port){
167
				$url .= ':'.$this->port;
168
			}
169
170
		}
171
172
		if($this->path){
173
			$url .= $this->path;
174
		}
175
176
177
		return $url;
178
	}
179
180
}
181