Skip to content

Commit add2b07

Browse files
committed
Adds mb_parse_url, deprecates Parser::mbParseUrl
1 parent 4861638 commit add2b07

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
"autoload": {
3030
"psr-0": {
3131
"Pdp\\": "library/"
32-
}
32+
},
33+
"files": [
34+
"library/mb-parse-url.php"
35+
]
3336
}
3437
}

library/Pdp/Parser.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ public function parseUrl($url)
6161
$url = 'http://' . preg_replace('#^//#', '', $url, 1);
6262
}
6363

64-
$parts = $this->mbParseUrl($url);
64+
$parts = mb_parse_url($url);
65+
66+
if ($parts === false) {
67+
throw new \InvalidArgumentException(sprintf('Invalid url %s', $url));
68+
}
6569

6670
$elem = (array) $parts + $elem;
6771

@@ -230,10 +234,13 @@ public function getSubdomain($host)
230234
}
231235

232236
/**
233-
* UTF-8 aware parse_url() replacement.
237+
* DEPRECATED: UTF-8 aware parse_url() replacement.
234238
*
235239
* Taken from php.net manual comments {@link http://php.net/manual/en/function.parse-url.php#114817}
236240
*
241+
* @deprecated Please use mb_parse_url instead. Will be removed in version 2.0.
242+
* @codeCoverageIgnore
243+
*
237244
* @param string $url The URL to parse
238245
* @param integer $component Specify one of PHP_URL_SCHEME, PHP_URL_HOST,
239246
* PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or
@@ -255,7 +262,7 @@ function ($matches) {
255262
$parts = parse_url($enc_url, $component);
256263

257264
if ($parts === false) {
258-
throw new \InvalidArgumentException(sprintf('Invalid url %s', $url));
265+
return $parts;
259266
}
260267

261268
if (is_array($parts)) {

library/mb-parse-url.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace {
4+
if (!function_exists('mb_parse_url')) {
5+
/**
6+
* UTF-8 aware parse_url() replacement.
7+
*
8+
* Taken from php.net manual comments {@link http://php.net/manual/en/function.parse-url.php#114817}
9+
*
10+
* @param string $url The URL to parse
11+
* @param integer $component Specify one of PHP_URL_SCHEME, PHP_URL_HOST,
12+
* PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or
13+
* PHP_URL_FRAGMENT to retrieve just a specific URL component as a string
14+
* (except when PHP_URL_PORT is given, in which case the return value will
15+
* be an integer).
16+
* @return mixed See parse_url documentation {@link http://us1.php.net/parse_url}
17+
*/
18+
function mb_parse_url($url, $component = -1)
19+
{
20+
$enc_url = preg_replace_callback(
21+
'%[^:/@?&=#]+%usD',
22+
function ($matches) {
23+
return urlencode($matches[0]);
24+
},
25+
$url
26+
);
27+
28+
$parts = parse_url($enc_url, $component);
29+
30+
if ($parts === false) {
31+
return $parts;
32+
}
33+
34+
if (is_array($parts)) {
35+
foreach ($parts as $name => $value) {
36+
$parts[$name] = urldecode($value);
37+
}
38+
} else {
39+
$parts = urldecode($parts);
40+
}
41+
42+
return $parts;
43+
}
44+
}
45+
}

tests/library/Pdp/ParserTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function tearDown()
2323

2424
/**
2525
* @covers Pdp\Parser::parseUrl()
26-
* @covers Pdp\Parser::mbParseUrl()
26+
* @covers ::mb_parse_url
2727
*/
2828
public function testParseBadUrlThrowsInvalidArgumentException()
2929
{
@@ -98,12 +98,13 @@ public function testGetSubdomain($url, $publicSuffix, $registerableDomain, $subd
9898

9999
/**
100100
* @dataProvider parseDataProvider
101+
* @covers ::mb_parse_url
101102
*/
102-
public function testMbParseUrlCanReturnCorrectHost($url, $publicSuffix, $registerableDomain, $subdomain, $hostPart)
103+
public function testmb_parse_urlCanReturnCorrectHost($url, $publicSuffix, $registerableDomain, $subdomain, $hostPart)
103104
{
104105
$this->assertEquals(
105106
$hostPart,
106-
$this->parser->mbParseUrl('http://' . $hostPart, PHP_URL_HOST)
107+
mb_parse_url('http://' . $hostPart, PHP_URL_HOST)
107108
);
108109
}
109110

0 commit comments

Comments
 (0)