Url::getScheme()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
4
  namespace ReRoute;
5
6
7
  /**
8
   *
9
   * @package ReRoute
10
   */
11
  class Url {
12
13
14
    /**
15
     * @var
16
     */
17
    private $path;
18
19
20
    /**
21
     * @var
22
     */
23
    private $host;
24
25
26
    /**
27
     * @var
28
     */
29
    private $scheme;
30
31
32
    /**
33
     * @var
34
     */
35
    private $port;
36
37
38
    /**
39
     * @var array
40
     */
41
    private $parameters = [];
42
43
44
    /**
45
     * @param string $host
46
     * @param string $scheme
47
     * @param int $port
48
     * @param string $path
49
     * @param array $parameters
50
     */
51 36
    public function __construct($host = 'localhost', $scheme = 'http', $port = 80, $path = '/', $parameters = []) {
52 36
      $this->setHost($host);
53 36
      $this->setScheme($scheme);
54 36
      $this->setPort($port);
55 36
      $this->setPath($path);
56 36
      $this->setParameters($parameters);
57 36
    }
58
59
60
    /**
61
     * Updates the RequestContext information based on a HttpFoundation Request.
62
     *
63
     * @param RequestContext $requestContext
64
     *
65
     * @return $this The current instance, implementing a fluent interface
66
     *
67
     */
68 3
    public static function fromRequestContext(RequestContext $requestContext) {
69
70 3
      $Url = new static();
71 3
      $Url->setScheme($requestContext->getScheme());
72 3
      $Url->setHost($requestContext->getHost());
73 3
      $Url->setPath($requestContext->getPath());
74 3
      $Url->setParameters($requestContext->getParameters());
75
76 3
      return $Url;
77
    }
78
79
80
    /**
81
     * Gets the HTTP port.
82
     *
83
     * @return int The HTTP port
84
     */
85 33
    public function getPort() {
86 33
      return $this->port;
87
    }
88
89
90
    /**
91
     * Sets the HTTP port.
92
     *
93
     * @param int $port The HTTP port
94
     *
95
     * @return $this The current instance, implementing a fluent interface
96
     *
97
     * @api
98
     */
99 36
    public function setPort($port) {
100 36
      $this->port = (int)$port;
101 36
      return $this;
102
    }
103
104
105
    /**
106
     * Returns the parameters.
107
     *
108
     * @return array The parameters
109
     */
110 3
    public function getParameters() {
111 3
      return $this->parameters;
112
    }
113
114
115
    /**
116
     * Sets the parameters.
117
     *
118
     * @param array $parameters The parameters
119
     *
120
     * @return $this The current instance, implementing a fluent interface
121
     */
122 36
    public function setParameters(array $parameters) {
123 36
      $this->parameters = $parameters;
124 36
      return $this;
125
    }
126
127
128
    /**
129
     * Gets a parameter value.
130
     *
131
     * @param string $name A parameter name
132
     *
133
     * @return mixed The parameter value or null if nonexistent
134
     */
135 3
    public function getParameter($name) {
136 3
      return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
137
    }
138
139
140
    /**
141
     * Checks if a parameter value is set for the given parameter.
142
     *
143
     * @param string $name A parameter name
144
     *
145
     * @return bool True if the parameter value is set, false otherwise
146
     */
147 3
    public function hasParameter($name) {
148 3
      return array_key_exists($name, $this->parameters);
149
    }
150
151
152
    /**
153
     * Sets a parameter value.
154
     *
155
     * @param string $name A parameter name
156
     * @param mixed $parameter The parameter value
157
     *
158
     * @return $this The current instance, implementing a fluent interface
159
     */
160 9
    public function setParameter($name, $parameter) {
161 9
      $this->parameters[$name] = $parameter;
162 9
      return $this;
163
    }
164
165
166
    /**
167
     * @return string
168
     */
169 33
    public function getUrl() {
170
      return
171 33
        $this->getScheme() . '://' .
172 33
        $this->getHost() .
173 33
        (80 == ($port = $this->getPort()) ? '' : ':' . $port) .
174 33
        $this->getPath() .
175 33
        (!empty($this->parameters) ? '?' . http_build_query($this->parameters) : '');
176
    }
177
178
179
    /**
180
     * Gets the HTTP scheme.
181
     *
182
     * @return string The HTTP scheme
183
     */
184 33
    public function getScheme() {
185 33
      return $this->scheme;
186
    }
187
188
189
    /**
190
     * Sets the HTTP scheme.
191
     *
192
     * @param string $scheme The HTTP scheme
193
     *
194
     * @return $this The current instance, implementing a fluent interface
195
     *
196
     * @api
197
     */
198 36
    public function setScheme($scheme) {
199 36
      $this->scheme = strtolower($scheme);
200 36
      return $this;
201
    }
202
203
204
    /**
205
     * Gets the HTTP host.
206
     *
207
     * The host is always lowercased because it must be treated case-insensitive.
208
     *
209
     * @return string The HTTP host
210
     */
211 33
    public function getHost() {
212 33
      return $this->host;
213
    }
214
215
216
    /**
217
     * Sets the HTTP host.
218
     *
219
     * @param string $host The HTTP host
220
     *
221
     * @return $this The current instance, implementing a fluent interface
222
     *
223
     * @api
224
     */
225 36
    public function setHost($host) {
226 36
      $this->host = strtolower($host);
227 36
      return $this;
228
    }
229
230
231
    /**
232
     * Gets the path info.
233
     *
234
     * @return string The path info
235
     */
236 33
    public function getPath() {
237 33
      return $this->path;
238
    }
239
240
241
    /**
242
     * Sets the path info.
243
     *
244
     * @param string $path
245
     *
246
     * @return $this The current instance, implementing a fluent interface
247
     *
248
     */
249 36
    public function setPath($path) {
250 36
      $this->path = $path;
251 36
      return $this;
252
    }
253
254
255
  }