|
1
|
|
|
<?php |
|
2
|
|
|
namespace Kambo\Http\Message\Factories\String; |
|
3
|
|
|
|
|
4
|
|
|
// \Http\Message |
|
5
|
|
|
use Kambo\Http\Message\Uri; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Create instances of Uri object from the string |
|
9
|
|
|
* |
|
10
|
|
|
* @package Kambo\Http\Message\Factories\String |
|
11
|
|
|
* @author Bohuslav Simek <[email protected]> |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
class UriFactory |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Create new Uri from provided string. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $uri uri that will be parsed into URI object |
|
20
|
|
|
* |
|
21
|
|
|
* @return Uri Instance of Uri based on provided string |
|
22
|
|
|
*/ |
|
23
|
12 |
|
public function create($uri) |
|
24
|
|
|
{ |
|
25
|
12 |
|
list($scheme, $host, $port, $path, $query, $fragment, $user, $pass) = $this->parseUrl($uri); |
|
26
|
|
|
|
|
27
|
12 |
|
return new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $pass); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Parse uri to array with individual parts |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $uri url that will be parsed into array with individual parts |
|
34
|
|
|
* |
|
35
|
|
|
* @return Array Individual parts of uri in array |
|
36
|
|
|
*/ |
|
37
|
12 |
|
protected function parseUrl($uri) |
|
38
|
|
|
{ |
|
39
|
12 |
|
$parsedUrl = parse_url($uri); |
|
40
|
|
|
|
|
41
|
|
|
return [ |
|
42
|
12 |
|
isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] : '', |
|
43
|
12 |
|
isset($parsedUrl['host']) ? $parsedUrl['host'] : '', |
|
44
|
12 |
|
isset($parsedUrl['port']) ? $parsedUrl['port'] : null, |
|
45
|
12 |
|
isset($parsedUrl['path']) ? $parsedUrl['path'] : '/', |
|
46
|
12 |
|
isset($parsedUrl['query']) ? $parsedUrl['query'] : '', |
|
47
|
12 |
|
isset($parsedUrl['fragment']) ? $parsedUrl['fragment'] : '', |
|
48
|
12 |
|
isset($parsedUrl['user']) ? $parsedUrl['user'] : '', |
|
49
|
12 |
|
isset($parsedUrl['pass']) ? $parsedUrl['pass'] : '' |
|
50
|
12 |
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|