Skip to content

Commit 3217f81

Browse files
lyrixxfabpot
authored andcommitted
[DomCrawler] Rename UriExpander.php -> UriResolver
1 parent 9eb7cb1 commit 3217f81

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/Symfony/Component/DomCrawler/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CHANGELOG
55
-----
66

77
* Added an internal cache layer on top of the CssSelectorConverter
8-
* Added `UriExpander` to expand an URL according to another URL
8+
* Added `UriResolver` to resolve an URI according to a base URI
99

1010
5.0.0
1111
-----

src/Symfony/Component/DomCrawler/Tests/UriExpanderTest.php renamed to src/Symfony/Component/DomCrawler/Tests/UriResolverTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
namespace Symfony\Component\DomCrawler\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\DomCrawler\UriExpander;
15+
use Symfony\Component\DomCrawler\UriResolver;
1616

17-
class UriExpanderTest extends TestCase
17+
class UriResolverTest extends TestCase
1818
{
1919
/**
20-
* @dataProvider provideExpandUriTests
20+
* @dataProvider provideResolverTests
2121
*/
22-
public function testExpandUri(string $uri, string $currentUri, string $expected)
22+
public function testResolver(string $uri, string $baseUri, string $expected)
2323
{
24-
$this->assertEquals($expected, UriExpander::expand($uri, $currentUri));
24+
$this->assertEquals($expected, UriResolver::resolve($uri, $baseUri));
2525
}
2626

27-
public function provideExpandUriTests()
27+
public function provideResolverTests()
2828
{
2929
return [
3030
['/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],

src/Symfony/Component/DomCrawler/UriExpander.php renamed to src/Symfony/Component/DomCrawler/UriResolver.php

+17-16
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@
1212
namespace Symfony\Component\DomCrawler;
1313

1414
/**
15-
* Expand an URI according a current URI.
15+
* The UriResolver class takes an URI (relative, absolute, fragment, etc.)
16+
* and turns it into an absolute URI against another given base URI.
1617
*
1718
* @author Fabien Potencier <fabien@symfony.com>
1819
* @author Grégoire Pineau <lyrixx@lyrixx.info>
1920
*/
20-
class UriExpander
21+
class UriResolver
2122
{
2223
/**
23-
* Expand an URI according to a current Uri.
24+
* Resolves a URI according to a base URI.
2425
*
25-
* For example if $uri=/foo/bar and $currentUri=https://symfony.com it will
26+
* For example if $uri=/foo/bar and $baseUri=https://symfony.com it will
2627
* return https://symfony.com/foo/bar
2728
*
28-
* If the $uri is not absolute you must pass an absolute $currentUri
29+
* If the $uri is not absolute you must pass an absolute $baseUri
2930
*/
30-
public static function expand(string $uri, ?string $currentUri): string
31+
public static function resolve(string $uri, ?string $baseUri): string
3132
{
3233
$uri = trim($uri);
3334

@@ -36,43 +37,43 @@ public static function expand(string $uri, ?string $currentUri): string
3637
return $uri;
3738
}
3839

39-
if (null === $currentUri) {
40+
if (null === $baseUri) {
4041
throw new \InvalidArgumentException('The URI is relative, so you must define its base URI passing an absolute URL.');
4142
}
4243

4344
// empty URI
4445
if (!$uri) {
45-
return $currentUri;
46+
return $baseUri;
4647
}
4748

4849
// an anchor
4950
if ('#' === $uri[0]) {
50-
return self::cleanupAnchor($currentUri).$uri;
51+
return self::cleanupAnchor($baseUri).$uri;
5152
}
5253

53-
$baseUri = self::cleanupUri($currentUri);
54+
$baseUriCleaned = self::cleanupUri($baseUri);
5455

5556
if ('?' === $uri[0]) {
56-
return $baseUri.$uri;
57+
return $baseUriCleaned.$uri;
5758
}
5859

5960
// absolute URL with relative schema
6061
if (0 === strpos($uri, '//')) {
61-
return preg_replace('#^([^/]*)//.*$#', '$1', $baseUri).$uri;
62+
return preg_replace('#^([^/]*)//.*$#', '$1', $baseUriCleaned).$uri;
6263
}
6364

64-
$baseUri = preg_replace('#^(.*?//[^/]*)(?:\/.*)?$#', '$1', $baseUri);
65+
$baseUriCleaned = preg_replace('#^(.*?//[^/]*)(?:\/.*)?$#', '$1', $baseUriCleaned);
6566

6667
// absolute path
6768
if ('/' === $uri[0]) {
68-
return $baseUri.$uri;
69+
return $baseUriCleaned.$uri;
6970
}
7071

7172
// relative path
72-
$path = parse_url(substr($currentUri, \strlen($baseUri)), PHP_URL_PATH);
73+
$path = parse_url(substr($baseUri, \strlen($baseUriCleaned)), PHP_URL_PATH);
7374
$path = self::canonicalizePath(substr($path, 0, strrpos($path, '/')).'/'.$uri);
7475

75-
return $baseUri.('' === $path || '/' !== $path[0] ? '/' : '').$path;
76+
return $baseUriCleaned.('' === $path || '/' !== $path[0] ? '/' : '').$path;
7677
}
7778

7879
/**

0 commit comments

Comments
 (0)