Commit a40748a2 authored by Matthias Sommerfeld's avatar Matthias Sommerfeld

Improve separation of concerns

The huge nameprep database, which partially exists in two different versions due
to changes between IDNA 2003 and IDNA 2008 has been extracted from the core class.
The same goes for the actual Punyocde algorithms.

Thus there's now the IdnaConvert class, which still is the public interface,
the Punycode class for the core algorithm and the NamePrepData classes for
IDNA 2003 and 2008 respectively. Since IDNA 2003 is only still supported for
historic reasons, NamePrepData is the leading class and NamePrepData2003 just
extends it.
Both implement the NamePrepDataInterface for easy dependency injection.

Also there's now the PunycodeInterface and the UnicodeTranscoderInterface for the
same reason.
parent 6736029d
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
<?php
namespace Mso\IdnaConvert;
interface NamePrepDataInterface
{
}
This diff is collapsed.
<?php
/*
* @author Matthias Sommerfeld <mso@phlylabs.de>
* @copyright 2004-2016 phlyLabs Berlin, http://phlylabs.de
*/
namespace Mso\IdnaConvert;
interface PunycodeInterface
{
public function __construct(NamePrepDataInterface $NamePrepData, UnicodeTranscoderInterface $UCTC);
public function getPunycodePrefix();
public function decode($encoded);
public function encode($decoded);
}
......@@ -18,7 +18,7 @@
namespace Mso\IdnaConvert;
class UnicodeTranscoder
class UnicodeTranscoder implements UnicodeTranscoderInterface
{
private static $mechs = ['ucs4', 'ucs4array', 'utf8', 'utf7', 'utf7imap'];
// unsupported yet: 'ucs4le', 'ucs4be', 'utf16', 'utf16le', 'utf16be'
......@@ -55,10 +55,12 @@ class UnicodeTranscoder
throw new \InvalidArgumentException(sprintf('Invalid output format %s', $to));
}
if ($from != 'ucs4array') {
eval('$data = self::' . $from . '_ucs4array($data);');
$methodName = $from.'_ucs4array';
$data = self::$methodName($data);
}
if ($to != 'ucs4array') {
eval('$data = self::ucs4array_' . $to . '($data);');
$methodName = 'ucs4array_'.$to;
$data = self::$methodName($data);
}
return $data;
......
<?php
/**
* UCTC - The Unicode Transcoder
*
* Converts between various flavours of Unicode representations like UCS-4 or UTF-8
* Supported schemes:
* - UCS-4 Little Endian / Big Endian / Array (partially)
* - UTF-16 Little Endian / Big Endian (not yet)
* - UTF-8
* - UTF-7
* - UTF-7 IMAP (modified UTF-7)
*
* @package IdnaConvert
* @author Matthias Sommerfeld <mso@phlyLabs.de>
* @copyright 2003-2016 phlyLabs Berlin, http://phlylabs.de
* @version 0.1.0 2016-01-08
*/
namespace Mso\IdnaConvert;
interface UnicodeTranscoderInterface
{
public static function convert($data, $from, $to, $safe_mode = false, $safe_char = 0xFFFC);
public static function utf8_ucs4array($input);
public static function ucs4array_utf8($input);
public static function utf7imap_ucs4array($input);
public static function utf7_ucs4array($input, $sc = '+');
public static function ucs4array_utf7imap($input);
public static function ucs4array_utf7($input, $sc = '+');
public static function ucs4array_ucs4($input);
public static function ucs4_ucs4array($input);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment