Restrict User Choice of Encodings
Posted: Mon Aug 28, 2006 10:52 am
I'm posting this code because I think its perfect. Want to proove me wrong? I'm sure you do. Can you find fault?
What it is designed to do is aid the user of OsisForms to specify a character encoding to use for their applications from a list of approved encodings and then allow me to write code based on different encodings by testing against the object. PHP's doesn't really have any good facilities for restricting input to a series of approved values so there is quite a lot of code but is very straight forward.
How it is used
Unit Test Case
Class
What it is designed to do is aid the user of OsisForms to specify a character encoding to use for their applications from a list of approved encodings and then allow me to write code based on different encodings by testing against the object. PHP's doesn't really have any good facilities for restricting input to a series of approved values so there is quite a lot of code but is very straight forward.
How it is used
Code: Select all
class OF
{
<< snip >>
/**
* Initialize OF without creating an instance
*
* @param OF_Encoding $encoding
*/
static public function init(OF_Encoding $encoding)
{
self::$_encoding = $encoding
if ($encoding->isUtf8()) {
require_once 'utf8/utf8.php';
}
self::$registry = new OF_Registry();
self::$_idRegistry = new OF_Registry(null);
}
<< snip >>
}
OF::init(OF_Encoding::getInstance('UTF-8'));Code: Select all
<?php
class TestOfEncoding extends OF_UnitTest
{
private $_instance;
public function testConstruction()
{
// smoketest this because when it fails (which it is supposed to) its
// fatal
//$a = new OF_Encoding();
//$this->assertError(true);
}
public function testGetInstance()
{
try {
$this->_instance = OF_Encoding::getInstance('no exist');
} catch (OF_Exception $e) {}
$this->assertNotNull($e);
$this->_instance = OF_Encoding::getInstance('UTF-8');
}
public function testIsUtf8()
{
$this->assertTrue($this->_instance->isUtf8());
// deliberately inexact case:
$this->_instance = OF_Encoding::getInstance('Iso-8859-1');
$this->assertFalse($this->_instance->isUtf8());
}
public function testToStringAndGet()
{
$this->assertEqual($this->_instance->get(), 'ISO-8859-1');
$this->assertEqual($this->_instance->__tostring(), 'OF_Encoding(ISO-8859-1)');
}
}Code: Select all
<?php
/**
* @package OF
*/
/**
* OF_Encoding
*
* --- Class Details -----------------
*
* Ensures only a supported encoding is used.
* This will be replaced by declare(encoding = 'x'); in PHP 6
*
* --- Maintenance Details -----------
*
* If you need support for more encodeding add to the static $_supported
* variable these must be all uppercase. If you need to add any more is* tests
* remember you can cache them on construction if you think it will aid
* performance.
*
* ------------------------------------
*
* @package OF
* @author Oliver Saunders
* @copyright Copyright (c) 2006 OS Internet Services (http://www.osinternetservices.com)
* @license See LICENSE.txt New BSD License
*/
class OF_Encoding
{
static protected $_supported = array('UTF-8', 'ISO-8859-1', 'CP1252');
/**
* Key of self::$_support, the encoding to use
*
* @var int
*/
protected $_encoding;
/**
* @var bool;
*/
protected $_isUtf8;
/**
* Privatised to ensure construction is only done internally with getInstance()
*/
private function __construct() {}
/**
* Creates a controller instance of Encoding
*
* @param string $encoding
* @return Encoding
*/
static public function getInstance($encoding)
{
$encoding = strtoupper($encoding); // ensures case insensitivity
$supportedKey = array_search($encoding, self::$_supported);
if ($supportedKey === false) {
throw new OF_Exception('Unsupported encoding specified (' . $encoding . ')');
}
$instance = new self();
$instance->_encoding = $supportedKey;
// a very common test, result is cached here at construction
$instance->_isUtf8 = $encoding == 'UTF-8';
return $instance;
}
/**
* Access to $_isUtf8
*
* @return bool
*/
public function isUtf8()
{
return $this->_isUtf8;
}
/**
* Return encoding
*
* @return string
*/
public function get()
{
return self::$_supported[$this->_encoding];
}
/**
* @return string
*/
public function __tostring()
{
return __CLASS__ . '(' . $this->get() . ')';
}
}