Skip to content

Commit 796baa5

Browse files
author
Martin Brecht-Precht
committed
Refactored the Url class.
1 parent 74bc7b1 commit 796baa5

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

src/Url.php

+27-39
Original file line numberDiff line numberDiff line change
@@ -78,38 +78,14 @@ public function parseUrl($url)
7878
$argumentType = (is_object($url)) ? get_class($url) : gettype($url);
7979
throw new \InvalidArgumentException('Expected URL as string; got ' . $argumentType);
8080
}
81-
$scheme = parse_url($url, PHP_URL_SCHEME);
82-
if (!is_null($scheme)) {
83-
$this->setScheme($scheme);
84-
}
85-
$hostname = parse_url($url, PHP_URL_HOST);
86-
if (!is_null($hostname)) {
87-
$this->setHostname($hostname);
88-
}
89-
$port = parse_url($url, PHP_URL_PORT);
90-
if (!is_null($port)) {
91-
$this->setPort($port);
92-
}
93-
$username = parse_url($url, PHP_URL_USER);
94-
if (!is_null($username)) {
95-
$this->setUsername($username);
96-
}
97-
$password = parse_url($url, PHP_URL_PASS);
98-
if (!is_null($password)) {
99-
$this->setPassword($password);
100-
}
101-
$path = parse_url($url, PHP_URL_PATH);
102-
if (!is_null($path)) {
103-
$this->setPath($path);
104-
}
105-
$queryString = parse_url($url, PHP_URL_QUERY);
106-
if (!is_null($queryString)) {
107-
$this->parseQueryString($queryString);
108-
}
109-
$fragment = parse_url($url, PHP_URL_FRAGMENT);
110-
if (!is_null($fragment)) {
111-
$this->setFragment($fragment);
112-
}
81+
$this->setScheme(parse_url($url, PHP_URL_SCHEME));
82+
$this->setHostname(parse_url($url, PHP_URL_HOST));
83+
$this->setPort(parse_url($url, PHP_URL_PORT));
84+
$this->setUsername(parse_url($url, PHP_URL_USER));
85+
$this->setPassword(parse_url($url, PHP_URL_PASS));
86+
$this->setPath(parse_url($url, PHP_URL_PATH));
87+
$this->parseQueryString(parse_url($url, PHP_URL_QUERY));
88+
$this->setFragment(parse_url($url, PHP_URL_FRAGMENT));
11389
return $this;
11490
}
11591

@@ -119,6 +95,10 @@ public function parseUrl($url)
11995
*/
12096
public function parseQueryString($queryString)
12197
{
98+
if (is_null($queryString)) {
99+
$this->clearQueryParameters();
100+
return $this;
101+
}
122102
$queryParameters = array();
123103
parse_str($queryString, $queryParameters);
124104
foreach ($queryParameters as $queryParameterKey => $queryParameterValue) {
@@ -189,7 +169,7 @@ public function hasScheme()
189169
*/
190170
public function setScheme($scheme)
191171
{
192-
if (!is_string($scheme)) {
172+
if (!is_null($scheme) && !is_string($scheme)) {
193173
$argumentType = (is_object($scheme)) ? get_class($scheme) : gettype($scheme);
194174
throw new \InvalidArgumentException('Expected scheme as string; got ' . $argumentType);
195175
}
@@ -219,7 +199,7 @@ public function hasHostname()
219199
*/
220200
public function setHostname($hostname)
221201
{
222-
if (!is_string($hostname)) {
202+
if (!is_null($hostname) && !is_string($hostname)) {
223203
$argumentType = (is_object($hostname)) ? get_class($hostname) : gettype($hostname);
224204
throw new \InvalidArgumentException('Expected hostname as string; got ' . $argumentType);
225205
}
@@ -249,7 +229,7 @@ public function hasPort()
249229
*/
250230
public function setPort($port)
251231
{
252-
if (!is_int($port)) {
232+
if (!is_null($port) && !is_int($port)) {
253233
$argumentType = (is_object($port)) ? get_class($port) : gettype($port);
254234
throw new \InvalidArgumentException('Expected port as integer; got ' . $argumentType);
255235
}
@@ -279,7 +259,7 @@ public function hasPath()
279259
*/
280260
public function setPath($path)
281261
{
282-
if (!is_string($path)) {
262+
if (!is_null($path) && !is_string($path)) {
283263
$argumentType = (is_object($path)) ? get_class($path) : gettype($path);
284264
throw new \InvalidArgumentException('Expected path as string; got ' . $argumentType);
285265
}
@@ -317,6 +297,10 @@ public function countQueryParameters()
317297
*/
318298
public function setQueryParameters($queryParameters)
319299
{
300+
if (is_null($queryParameters)) {
301+
$this->clearQueryParameters();
302+
return $this;
303+
}
320304
if (!is_array($queryParameters)) {
321305
$argumentType = (is_object($queryParameters)) ? get_class($queryParameters) : gettype($queryParameters);
322306
throw new \InvalidArgumentException('Expected query parameters as array; got ' . $argumentType);
@@ -368,6 +352,10 @@ public function removeQueryParameter(QueryParameterInterface $queryParameter)
368352
*/
369353
public function removeQueryParameterByKey($key)
370354
{
355+
if (!is_string($key)) {
356+
$argumentType = (is_object($key)) ? get_class($key) : gettype($key);
357+
throw new \InvalidArgumentException('Expected query parameter key as string; got ' . $argumentType);
358+
}
371359
for ($i = 0; $i < count($this->queryParameters); $i++) {
372360
if ($this->queryParameters[$i]->getKey() === $key) {
373361
unset($this->queryParameters[$i]);
@@ -408,7 +396,7 @@ public function hasUsername()
408396
*/
409397
public function setUsername($username)
410398
{
411-
if (!is_string($username)) {
399+
if (!is_null($username) && !is_string($username)) {
412400
$argumentType = (is_object($username)) ? get_class($username) : gettype($username);
413401
throw new \InvalidArgumentException('Expected username as string; got ' . $argumentType);
414402
}
@@ -438,7 +426,7 @@ public function hasPassword()
438426
*/
439427
public function setPassword($password)
440428
{
441-
if (!is_string($password)) {
429+
if (!is_null($password) && !is_string($password)) {
442430
$argumentType = (is_object($password)) ? get_class($password) : gettype($password);
443431
throw new \InvalidArgumentException('Expected password as string; got ' . $argumentType);
444432
}
@@ -468,7 +456,7 @@ public function hasFragment()
468456
*/
469457
public function setFragment($fragment)
470458
{
471-
if (!is_string($fragment)) {
459+
if (!is_null($fragment) && !is_string($fragment)) {
472460
$argumentType = (is_object($fragment)) ? get_class($fragment) : gettype($fragment);
473461
throw new \InvalidArgumentException('Expected fragment as string; got ' . $argumentType);
474462
}

0 commit comments

Comments
 (0)