Posted: Tue Aug 29, 2006 6:16 am
I think it boils down to preference.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
can't you move the cleaning and conversions into the encoding class?ole wrote: And then dotted around the entire library will be things like this:Code: Select all
if (self::$encoding->isUtf8()) { $out.= self::_makeAttrib('class', utf8_bad_replace($this->wrapClass, '')); } else { $out.= self::_makeAttrib('class', $this->wrapClass); }
Code: Select all
$out.= self::_makeAttrib('class', self::$encoding->cleanup($this->wrapClass, ''));Code: Select all
OF::init('UTF-8');
class OF
{
<<snip>>
static public function init($enc)
{
static $called = false;
if ($called) {
$errStr = 'You may not reinitialize ' . __CLASS__;
throw new OF_Exception($errStr, OF_Exception::MISC);
}
self::$enc = new OF_Enc($enc);
self::$registry = new OF_Registry();
self::$_idRegistry = new OF_Registry(null);
$called = true;
}
<<snip>>
}Code: Select all
<?php
class TestOfEnc extends OF_UnitTest
{
public function testInstanitation()
{
$iso88591 = new OF_EncStrategySingle('ISO-8859-1');
$cp1252 = new OF_EncStrategySingle('cp1252');
$utf8 = new OF_EncStrategyMultiUtf8();
$defaultEnc = new OF_Enc(null);
}
public function testConstruction()
{
$enc = new OF_Enc('UTF-8');
$this->assertIsA(self::expose($enc, '_strategy'), 'OF_EncStrategyMultiUtf8');
$enc = new OF_Enc('ISO-8859-1');
$this->assertIsA(self::expose($enc, '_strategy'), 'OF_EncStrategySingle');
try {
$enc = new OF_Enc('unsupported encoding');
} catch (OF_Exception $e) {}
$this->assertNotNull(@$e);
}
public function testStrategy()
{
$encoding = new OF_Enc('UTF-8');
$clean = 'just a string';
$cleaned = $encoding->inputClean($clean);
$this->assertEqual($cleaned, $clean);
// 170 is invalid UTF-8
$dirty = chr(30) . chr(170);
$cleaned = $encoding->inputClean($dirty);
$this->assertEqual($cleaned, $dirty[0]);
try {
$warn = $encoding->inputWarn($clean);
$warn = $encoding->inputWarn($dirty);
} catch (OF_Exception $e) {};
$this->assertNotNull(@$e);
$this->assertEqual($warn, $clean);
try {
$this->assertEqual($encoding->someNonExistantThing($clean), $clean);
} catch (OF_Exception $e) {}
$this->assertNotNull(@$e);
}
}Code: Select all
<?php
/**
* LICENSE
*
* This source file is subject to the new BSD license that is bundled with this
* package in the file LICENSE.txt. If you did not receive a copy of the
* license, please send an email to oliver@osinternetservices.com so I can send
* you a copy immediately.
*
* @package OF
*/
/**
* OF_Enc
*
* --- Class Details -----------------
*
* OF_Enc contains an encoding strategy subclass. See methods.
*
* --- Maintenance Details -----------
*
* Add new strategies to the switch
*
* -----------------------------------
*
* @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_Enc
{
/**
* The strategy implementation
*
* @var OF_EncStrategy
*/
private $_strategy;
/**
* Instantiate with a strategy. You may specify a string encoding name which
* will then use the appropriate strategy, null to grab the default_charset
* from php.ini and use the appropriate strategy for that or an instance of
* OF_EncStrategy.
*
* @param null|string|OF_EncStrategy $encoding
* @throws OF_Exception if an unknown strategy is specified
*/
public function __construct($encoding = null)
{
$iniGot = false;
if ($encoding === null) {
$encoding = ini_get('default_charset');
$iniGot = true;
}
if (is_string($encoding)) {
switch ($encoding) {
case 'cp1252':
case 'ISO-8859-1':
$this->_strategy = new OF_EncStrategySingle($encoding);
break;
case 'UTF-8':
$this->_strategy = new OF_EncStrategyMultiUtf8();
break;
default:
$errStr = 'Unsupported encoding (' . $encoding . ')';
throw new OF_Exception($errStr, OF_Exception::EXISTANCE);
}
} else if ($encoding instanceof OF_EncStrategy) {
$this->_strategy = $encoding;
} else {
$errStr = 'Only instances of OF_EncStrategy acceptable';
throw new OF_Exception($errStr, OF_Exception::INITIALIZATION);
}
if (!$iniGot) {
ini_set('default_charset', $this->_strategy->getName());
}
}
/**
* Passes on any calls to the strategy.
*
* If the method exists you are returned the result if the method does not
* exist you are returned an unmodified first argument. This is usually a
* string to process.
*
* @param string $funcName
* @param array $params
* @return mixed usually a string
*/
public function __call($funcName, $params)
{
if (!method_exists($this->_strategy, $funcName)) {
// Notify of mistakes
$errStr = 'Unknown method (' . $funcName . ')';
throw new OF_Exception($errStr, OF_Exception::EXISTANCE);
}
return call_user_func_array(array($this->_strategy, $funcName), $params);
}
}
/**
* OF_EncStrategy
*
* --- Class Details -----------------
*
* OF_EncStrategy is the base encoding strategy class.
*
* --- Maintenance Details -----------
*
* IMPORTANT: If you add a new method to any strategy you must either:
* a. Specify it as abstract here and implement it in all encodings
* OR
* b. Specify a default implementation here
*
* -----------------------------------
*
* @package OF
* @author Oliver Saunders
* @copyright Copyright (c) 2006 OS Internet Services (http://www.osinternetservices.com)
* @license See LICENSE.txt New BSD License
*/
abstract class OF_EncStrategy
{
/**
* Get the name of the encoding. This is the value that will be used in the
* charset part of the HTTP response Content-Type header.
*
* @return string
*/
abstract public function getName();
/**
* Following methods are default implementations for encoding specific
* functionality
*/
public function inputWarn($str)
{
return $str;
}
public function inputClean($str)
{
return $str;
}
}
/**
* OF_EncStrategySingle
*
* --- Class Details -----------------
*
* OF_EncSingle is a strategy of OF_Enc that handles single byte charater
* encodings. These are supported natively by PHP and thus require no extra
* functionality.
*
* --- Maintenance Details -----------
*
* -----------------------------------
*
* @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_EncStrategySingle extends OF_EncStrategy
{
/**
* The name of the encoding to use
*
* @var string
*/
protected $_encoding;
/**
* @param string $encoding
*/
public function __construct($encoding)
{
$this->_encoding = $encoding;
}
/**
* @see OF_EncStrategy::getName()
*/
public function getName()
{
return $this->_encoding;
}
}
/**
* OF_EncStrategyMultiUtf8
*
* --- Class Details -----------------
*
* OF_EncSingle is a strategy of OF_Enc that handles single byte charater
* encodings. These are supported natively by PHP and thus require no extra
* functionality.
*
* --- Maintenance Details -----------
*
* -----------------------------------
*
* @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_EncStrategyMultiUtf8 extends OF_EncStrategy
{
/**
* Load up the utf8 library.
* Add any additional requires here
*/
public function __construct()
{
require_once 'utf8/utf8.php';
require_once 'utf8/utils/bad.php';
}
/**
* Remove bad characters from $str.
*
* Use this on input where bad characters are to be expected i.e. $r_source
*
* @param string $str
* @return string
*/
public function inputClean($str)
{
return utf8_bad_replace($str, '');
}
/**
* Warn if bad characters are present in $str
*
* Use this on input that is supposed to be safe and not actually be used
* as source of input i.e. public properties or parameters on public methods.
*
* @param string $str
* @throws OF_Exception if bad characters are contained in $str
* @return string
*/
public function inputWarn($str)
{
if (utf8_bad_find($str) !== false) {
$errStr = 'Encoding error (' . $this->getName() . ')';
throw new OF_Exception($errStr, OF_Exception::TYPE_SCALAR);
}
return $str;
}
/**
* @see OF_EncStrategy::getName()
*/
public function getName()
{
return 'UTF-8';
}
}