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

feat(package): remove dependency on ext-intl #8

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## dev

New features:
* issue #7 use polyfill for intl functions

## 1.1.1 - 2016-08-03

Fixes:
Expand All @@ -16,4 +21,4 @@ New features:
New release with following features:
* IDN support;
* Database in separate weekly updatable package;
* Full test coverage.
* Full test coverage.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
],
"require": {
"php": ">=5.5",
"ext-intl": "*",
"layershifter/tld-database": "^1.0",
"layershifter/tld-support": "^1.1"
"layershifter/tld-support": "^1.1",
"true/punycode": "^2.1.1"
},
"require-dev": {
"codeclimate/php-test-reporter": "dev-master",
Expand Down
9 changes: 7 additions & 2 deletions src/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class Extract
* @var string Name of class that will store results of parsing.
*/
private $resultClassName;
/**
* @var IDN Object of TLDExtract\IDN class.
*/
private $idn;
/**
* @var Store Object of TLDDatabase\Store class.
*/
Expand All @@ -67,6 +71,7 @@ class Extract
*/
public function __construct($databaseFile = null, $resultClassName = null, $extractionMode = null)
{
$this->idn = new IDN();
$this->suffixStore = new Store($databaseFile);
$this->resultClassName = Result::class;

Expand Down Expand Up @@ -248,7 +253,7 @@ private function extractSuffix($hostname)
$isPunycoded = Str::strpos($hostname, 'xn--') !== false;

if ($isPunycoded) {
$hostname = idn_to_utf8($hostname);
$hostname = $this->idn->toUTF8($hostname);
}

$suffix = $this->parseSuffix($hostname);
Expand All @@ -263,7 +268,7 @@ private function extractSuffix($hostname)

// If domain is punycoded, suffix will be converted to punycode.

return $isPunycoded ? idn_to_ascii($suffix) : $suffix;
return $isPunycoded ? $this->idn->toASCII($hostname) : $suffix;
}

/**
Expand Down
59 changes: 59 additions & 0 deletions src/IDN.php
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);
}
}
64 changes: 64 additions & 0 deletions tests/IDNTest.php
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'));
}
}