Completed
Branch master (e874e7)
by smiley
05:21
created

URL::parseUrl()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 15
rs 7
cc 8
eloc 10
nc 128
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A URL::__toString() 0 3 1
1
<?php
2
/**
3
 * Class URL
4
 *
5
 * (wtb new name - URL is misleading...)
6
 *
7
 * @filesource   URL.php
8
 * @created      18.02.2016
9
 * @package      chillerlan\TinyCurl
10
 * @author       Smiley <[email protected]>
11
 * @copyright    2016 Smiley
12
 * @license      MIT
13
 */
14
15
namespace chillerlan\TinyCurl;
16
17
/**
18
 * @property string url
19
 * @property string method
20
 * @property string scheme
21
 * @property string host
22
 * @property string port
23
 * @property string path
24
 * @property string query
25
 * @property string fragment
26
 * @property array  params
27
 * @property array  parsedquery
28
 * @property mixed  body
29
 * @property array  headers
30
 */
31
class URL{
32
33
	const URL_PARTS       = ['scheme', 'host', 'port', 'path', 'query', 'fragment'];
34
	const ALLOWED_SCHEMES = ['http', 'https'];
35
	const ALLOWED_METHODS = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'];
36
37
	/**
38
	 * @var string
39
	 */
40
	protected $url;
41
42
	/**
43
	 * @var string
44
	 */
45
	protected $method;
46
47
	/**
48
	 * @var string
49
	 */
50
	protected $scheme;
51
52
	/**
53
	 * @var string
54
	 */
55
	protected $host;
56
57
	/**
58
	 * @var int
59
	 */
60
	protected $port;
61
62
	/**
63
	 * @var string
64
	 */
65
	protected $path;
66
67
	/**
68
	 * @var string
69
	 */
70
	protected $query;
71
72
	/**
73
	 * @var string
74
	 */
75
	protected $fragment;
76
77
	/**
78
	 * @var array
79
	 */
80
	protected $parsedquery = [];
81
82
	/**
83
	 * @var array
84
	 */
85
	protected $params = [];
86
87
	/**
88
	 * @var mixed
89
	 */
90
	protected $body;
91
92
	/**
93
	 * @var array
94
	 */
95
	protected $headers = [];
96
97
	/**
98
	 * URL constructor.
99
	 *
100
	 * @param string $url
101
	 * @param array  $params
102
	 * @param string $method
103
	 * @param mixed  $body
104
	 * @param array  $headers
105
	 *
106
	 * @throws \chillerlan\TinyCurl\URLException
107
	 */
108
	public function __construct(string $url, array $params = [], string $method = 'GET', $body = null, array $headers = []){
109
		$this->url     = $url;
110
		$this->params  = $params;
111
		$this->body    = $body;
112
		$this->headers = $headers;
113
		$this->method  = strtoupper($method);
114
115
		$url = parse_url($this->url);
116
117
		foreach(self::URL_PARTS as $part){
118
			$this->{$part} = !isset($url[$part]) ? null : $url[$part];
119
		}
120
121
		if($this->scheme && !in_array(strtolower($this->scheme), self::ALLOWED_SCHEMES)){
122
			throw new URLException('invalid scheme: '.$this->scheme);
123
		}
124
125
		if(!$this->host){
126
			throw new URLException('no host given');
127
		}
128
129
		if(!in_array($this->method, self::ALLOWED_METHODS, true)){
130
			throw new URLException('invalid method: '.$this->method);
131
		}
132
133
		if($this->query){
134
			parse_str($this->query, $this->parsedquery);
135
		}
136
137
	}
138
139
	/**
140
	 * @param string $name
141
	 *
142
	 * @return mixed
143
	 */
144
	public function __get(string $name){
145
146
		if(property_exists($this, $name)){
147
			return $this->{$name};
148
		}
149
150
		return false;
151
	}
152
153
	/**
154
	 * @return string URL with merged params
155
	 */
156
	public function __toString():string{
157
		return $this->mergeParams();
158
	}
159
160
	/**
161
	 * @return string
162
	 */
163
	public function originalParams():string{
164
		return $this->getURL($this->parsedquery);
165
	}
166
167
	/**
168
	 * @return string
169
	 */
170
	public function overrideParams():string{
171
		return $this->getURL($this->params);
172
	}
173
174
	/**
175
	 * @return string
176
	 */
177
	public function mergeParams():string{
178
		return $this->getURL(array_merge($this->parsedquery, $this->params));
179
	}
180
181
	/**
182
	 * @param array $params
183
	 *
184
	 * @return string
185
	 */
186
	protected function getURL(array $params):string{
187
		$url = '';
188
189
		if($this->scheme){
190
			$url .= $this->scheme.':';
191
		}
192
193
		if($this->host){
194
			$url .= '//'.$this->host;
195
196
			if($this->port){
197
				$url .= ':'.$this->port;
198
			}
199
200
		}
201
202
		if($this->path){
203
			$url .= $this->path;
204
		}
205
206
		if(!empty($params)){
207
			$url .= '?'.http_build_query($params);
208
		}
209
210
		if($this->fragment){
211
			$url .= '#'.$this->fragment;
212
		}
213
214
		return $url;
215
	}
216
217
}
218