Passed
Branch master (114a3b)
by
unknown
04:22
created

URL::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 5
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
 * @todo -> Container
19
 *
20
 * @property string url
21
 * @property string method
22
 * @property string scheme
23
 * @property string host
24
 * @property string port
25
 * @property string path
26
 * @property string query
27
 * @property string fragment
28
 * @property array  params
29
 * @property array  parsedquery
30
 * @property mixed  body
31
 * @property array  headers
32
 */
33
class URL{
34
35
	const URL_PARTS       = ['scheme', 'host', 'port', 'path', 'query', 'fragment'];
36
	const ALLOWED_SCHEMES = ['http', 'https'];
37
	const ALLOWED_METHODS = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'];
38
39
	/**
40
	 * @var string
41
	 */
42
	protected $url;
43
44
	/**
45
	 * @var string
46
	 */
47
	protected $method;
48
49
	/**
50
	 * @var string
51
	 */
52
	protected $scheme;
53
54
	/**
55
	 * @var string
56
	 */
57
	protected $host;
58
59
	/**
60
	 * @var int
61
	 */
62
	protected $port;
63
64
	/**
65
	 * @var string
66
	 */
67
	protected $path;
68
69
	/**
70
	 * @var string
71
	 */
72
	protected $query;
73
74
	/**
75
	 * @var string
76
	 */
77
	protected $fragment;
78
79
	/**
80
	 * @var array
81
	 */
82
	protected $parsedquery = [];
83
84
	/**
85
	 * @var array
86
	 */
87
	protected $params = [];
88
89
	/**
90
	 * @var mixed
91
	 */
92
	protected $body;
93
94
	/**
95
	 * @var array
96
	 */
97
	protected $headers = [];
98
99
	/**
100
	 * URL constructor.
101
	 *
102
	 * @param string $url
103
	 * @param array  $params
104
	 * @param string $method
105
	 * @param mixed  $body
106
	 * @param array  $headers
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
		$this->parse();
116
	}
117
118
	/**
119
	 * @throws \chillerlan\TinyCurl\URLException
120
	 * @return void
121
	 */
122
	protected function parse(){
123
		$url = parse_url($this->url);
124
125
		foreach(self::URL_PARTS as $part){
126
			$this->{$part} = $url[$part] ?? null;
127
		}
128
129
		if($this->scheme !== null && !in_array(strtolower($this->scheme), self::ALLOWED_SCHEMES, true)){
130
			throw new URLException('invalid scheme: '.$this->scheme);
131
		}
132
133
		if(!$this->host){
134
			throw new URLException('no host given');
135
		}
136
137
		if(!in_array($this->method, self::ALLOWED_METHODS, true)){
138
			throw new URLException('invalid method: '.$this->method);
139
		}
140
141
		if($this->query !== null){
142
			parse_str($this->query, $this->parsedquery);
143
		}
144
145
	}
146
147
	/**
148
	 * @param string $name
149
	 *
150
	 * @return mixed
151
	 */
152
	public function __get(string $name){
153
154
		if(property_exists($this, $name)){
155
			return $this->{$name};
156
		}
157
158
		return false;
159
	}
160
161
	/**
162
	 * @return string URL with merged params
163
	 */
164
	public function __toString():string{
165
		return $this->mergeParams();
166
	}
167
168
	/**
169
	 * @return string
170
	 */
171
	public function originalParams():string{
172
		return $this->getURL($this->parsedquery);
173
	}
174
175
	/**
176
	 * @return string
177
	 */
178
	public function overrideParams():string{
179
		return $this->getURL($this->params);
180
	}
181
182
	/**
183
	 * @return string
184
	 */
185
	public function mergeParams():string{
186
		return $this->getURL(array_merge($this->parsedquery, $this->params));
187
	}
188
189
	/**
190
	 * @param array $params
191
	 *
192
	 * @return string
193
	 */
194
	protected function getURL(array $params):string{
195
		$url = '';
196
197
		if($this->scheme){
198
			$url .= $this->scheme.':';
199
		}
200
201
		if($this->host){
202
			$url .= '//'.$this->host;
203
204
			if($this->port){
205
				$url .= ':'.$this->port;
206
			}
207
208
		}
209
210
		if($this->path){
211
			$url .= $this->path;
212
		}
213
214
		if(!empty($params)){
215
			$url .= '?'.http_build_query($params);
216
		}
217
218
		if($this->fragment){
219
			$url .= '#'.$this->fragment;
220
		}
221
222
		return $url;
223
	}
224
225
}
226