This repository has been archived by the owner on Nov 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(package): remove dependency on ext-intl (#8)
* added symfony intl polyfill * add polyfill to for the idn calls * add fallback for idn functions when intl extension not installed
- Loading branch information
1 parent
db5c472
commit 22d0e94
Showing
5 changed files
with
138 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace LayerShifter\TLDExtract; | ||
|
||
use TrueBV\Punycode; | ||
|
||
/** | ||
* Class that transforms IDN domains, if `intl` extension present uses it. | ||
*/ | ||
class IDN | ||
{ | ||
|
||
/** | ||
* @var Punycode Object of TrueBV\Punycode class. | ||
*/ | ||
private $transformer; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
if (!function_exists('\idn_to_utf8')) { | ||
$this->transformer = new Punycode(); | ||
} | ||
} | ||
|
||
/** | ||
* Converts domain name from Unicode to IDNA ASCII. | ||
* | ||
* @param string $domain Domain to convert in IDNA ASCII-compatible format. | ||
* | ||
* @return string | ||
*/ | ||
public function toASCII($domain) | ||
{ | ||
if ($this->transformer) { | ||
return $this->transformer->encode($domain); | ||
} | ||
|
||
return idn_to_ascii($domain); | ||
} | ||
|
||
/** | ||
* Converts domain name from IDNA ASCII to Unicode. | ||
* | ||
* @param string $domain Domain to convert in Unicode format. | ||
* | ||
* @return string | ||
*/ | ||
public function toUTF8($domain) | ||
{ | ||
if ($this->transformer) { | ||
return $this->transformer->decode($domain); | ||
} | ||
|
||
return idn_to_utf8($domain); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace LayerShifter\TLDExtract\Tests; | ||
|
||
use LayerShifter\TLDExtract\IDN; | ||
use TrueBV\Punycode; | ||
|
||
/** | ||
* Tests for IDN class. | ||
*/ | ||
class IDNTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* @var IDN Object for tests | ||
*/ | ||
private $idn; | ||
|
||
/** | ||
* Method that setups test's environment. | ||
* | ||
* @return void | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->idn = new IDN(); | ||
} | ||
|
||
/** | ||
* Tests constructor(), ensures that transformer isn't loaded when `intl` extension present. | ||
* | ||
* @void | ||
*/ | ||
public function testConstructor() | ||
{ | ||
if (function_exists('\idn_to_utf8')) { | ||
$this->assertAttributeInternalType('null', 'transformer', $this->idn); | ||
|
||
return; | ||
} | ||
|
||
$this->assertAttributeInstanceOf(Punycode::class, 'transformer', $this->idn); | ||
} | ||
|
||
/** | ||
* Tests toASCII() method. | ||
* | ||
* @return void | ||
*/ | ||
public function testToASCII() | ||
{ | ||
$this->assertEquals('xn--tst-qla.de', $this->idn->toASCII('täst.de')); | ||
} | ||
|
||
/** | ||
* Tests toUTF8() method. | ||
* | ||
* @return void | ||
*/ | ||
public function testToUTF8() | ||
{ | ||
$this->assertEquals('täst.de', $this->idn->toUTF8('xn--tst-qla.de')); | ||
} | ||
} |