Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Update idn to ascii algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Feb 6, 2017
1 parent 6cbb098 commit 129e088
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

All Notable changes to `league-uri-parser` will be documented in this file

## 1.0.3 - 2017-02-06

### Added

- None

### Fixed

- idn_to_ascii uses `INTL_IDNA_VARIANT_UTS46` as `INTL_IDNA_VARIANT_2003` will be deprecated

### Deprecated

- None

### Removed

- None

## 1.0.2 - 2017-01-19

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^1.9",
"phpunit/phpunit" : "^5.0",
"phpunit/phpunit" : "^6.0",
"phpbench/phpbench": "^0.12.2"
},
"autoload": {
Expand Down
20 changes: 19 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,29 @@ protected function isRegisteredName(string $host): bool
$host = substr($host, 0, -1);
}

$labels = array_map('idn_to_ascii', explode('.', $host));
$labels = array_map([$this, 'toAscii'], explode('.', $host));

return 127 > count($labels) && $labels === array_filter($labels, [$this, 'isHostLabel']);
}

/**
* Convert domain name to IDNA ASCII form.
*
* @param string $label
*
* @return string
*/
protected function toAscii(string $label)
{
$res = idn_to_ascii($label, 0, INTL_IDNA_VARIANT_UTS46);
if (false !== $res) {
return $res;
}

return '';
}


/**
* Returns whether the host label is valid
*
Expand Down
32 changes: 11 additions & 21 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,17 @@
class ParserTest extends TestCase
{
/**
* @var Parser
*/
protected $parser;

protected function setUp()
{
$this->parser = new Parser();
}

/**
* @dataProvider testValidURI
* @dataProvider validUriProvider
* @param $uri
* @param $expected
*/
public function testParseSucced($uri, $expected)
{
$this->assertSame($expected, $this->parser->__invoke($uri));
$components = (new Parser())($uri);
$this->assertSame($expected, $components);
}

public function testValidURI()
public function validUriProvider()
{
return [
'complete URI' => [
Expand Down Expand Up @@ -86,7 +77,6 @@ public function testValidURI()
'fragment' => null,
],
],

'URI without userinfo' => [
'scheme://HoSt:81/path?query#fragment',
[
Expand Down Expand Up @@ -637,16 +627,16 @@ public function testValidURI()
}

/**
* @dataProvider testInvalidURI
* @dataProvider invalidUriProvider
* @param string $uri
*/
public function testParseFailed($uri)
{
$this->setExpectedException(Exception::class);
$this->parser->__invoke($uri);
$this->expectException(Exception::class);
(new Parser())($uri);
}

public function testInvalidURI()
public function invalidUriProvider()
{
return [
'invalid scheme (1)' => ['0scheme://host/path?query#fragment'],
Expand All @@ -670,14 +660,14 @@ public function testInvalidURI()
}

/**
* @dataProvider testValidHost
* @dataProvider validHostProvider
*/
public function testHost($host, $expected)
{
$this->assertSame($expected, $this->parser->isHost($host));
$this->assertSame($expected, (new Parser())->isHost($host));
}

public function testValidHost()
public function validHostProvider()
{
return [
'RFC3986 registered name' => ['bebe.be', true],
Expand Down

0 comments on commit 129e088

Please sign in to comment.