1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the AMFWebServicesClientBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Amine Fattouch <http://github.com/fattouchsquall> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AMF\WebServicesClientBundle\Rest\Component; |
13
|
|
|
|
14
|
|
|
use AMF\WebServicesClientBundle\Rest\Constant\Schemes; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This class represents a ReST Url. |
18
|
|
|
* |
19
|
|
|
* @author Mohamed Amine Fattouch <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Url |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $hostname; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $scheme; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var integer |
35
|
|
|
*/ |
36
|
|
|
protected $port; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $queryDelimiter; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The constructor class. |
45
|
|
|
* |
46
|
|
|
* @param string $hostname The hostname for url of Rest. |
47
|
|
|
* @param string $scheme The scheme for url of Rest. |
48
|
|
|
* @param integer $port The port for url of Rest. |
49
|
|
|
* @param string $queryDelimiter The query delimiter for url of Rest. |
50
|
|
|
*/ |
51
|
|
|
public function __construct($hostname, $scheme, $port, $queryDelimiter) |
52
|
|
|
{ |
53
|
|
|
$this->hostname = $hostname; |
54
|
|
|
$this->scheme = $scheme; |
55
|
|
|
$this->port = $port; |
56
|
|
|
$this->queryDelimiter = $queryDelimiter; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The getter for hostname. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getHostname() |
65
|
|
|
{ |
66
|
|
|
return $this->hostname; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The getter for scheme. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getScheme() |
75
|
|
|
{ |
76
|
|
|
return $this->scheme; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The getter for port. |
81
|
|
|
* |
82
|
|
|
* @return integer |
83
|
|
|
*/ |
84
|
|
|
public function getPort() |
85
|
|
|
{ |
86
|
|
|
if (isset($this->port) === false) { |
87
|
|
|
return Schemes::HTTPS === $this->getScheme() ? 443 : 80; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return (int) $this->port; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The getter for queryDelimiter. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getQueryDelimiter() |
99
|
|
|
{ |
100
|
|
|
return $this->queryDelimiter; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* The getter for uri. |
105
|
|
|
* |
106
|
|
|
* @param string $path The path of the url. |
107
|
|
|
* @param array $query The query to pass to the URL (default empty). |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getUriForPath($path, array $query = []) |
112
|
|
|
{ |
113
|
|
|
$formattedQuery = $this->buildQuery($query); |
114
|
|
|
$fullUri = $this->buildBaseUrl().'/'.ltrim($path, '/').(isset($formattedQuery) && strlen($formattedQuery) > 0 ? $this->queryDelimiter.$formattedQuery : ''); |
115
|
|
|
|
116
|
|
|
return $fullUri; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Builds the full base url. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
protected function buildBaseUrl() |
125
|
|
|
{ |
126
|
|
|
$hostname = $this->getHostname(); |
127
|
|
|
|
128
|
|
|
if (strpos($hostname, '/') === false) { |
129
|
|
|
$hostname .= ':'.$this->getPort(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$baseUrl = $this->getScheme().'://'.$hostname; |
133
|
|
|
|
134
|
|
|
return trim($baseUrl, '/'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Builds the list of query paramaters. |
139
|
|
|
* |
140
|
|
|
* @param array $query The data to pass to the URL (default empty). |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
protected function buildQuery(array $query = []) |
145
|
|
|
{ |
146
|
|
|
$formattedQuery = ''; |
147
|
|
|
if (isset($query) && !empty($query)) { |
148
|
|
|
if ($this->queryDelimiter === '/') { |
149
|
|
|
$formattedQuery = implode($this->queryDelimiter, $query); |
150
|
|
|
} else { |
151
|
|
|
$formattedQuery = http_build_query($query, '', '&'); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $formattedQuery; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|