<?php
/**
* 文字エンコーディング変換クラス
*
* PHP version 5
*
* @package    core
* @author     stk2k <stk2k@sazysoft.com>
* @copyright  2008 stk2k, sazysoft
*/

class EncodingConverter
{
	//==========================================
	//	Converter Types
	//==========================================
	const NO_CONVERT		= 0;
	const PHP_TO_DB  		= 1;
	const PHP_TO_HTML  		= 2;
	const PHP_TO_LOG  		= 3;
	const HTML_TO_PHP  		= 4;
	const DB_TO_PHP  		= 5;
	const DB_TO_HTML  		= 6;
	const DB_TO_LOG	  		= 7;
	const HTML_TO_DB  		= 8;
	const PHP_TO_MAIL		= 9;

	var $_type;
	var $_from;
	var $_to;

	/*
	 *	コンストラクタ
	 */
	private function __construct( $type )
	{
		$this->_type = $type;
		$this->setEncodingSettings();
	}

	/*
	 *	文字列からコンバータを作成
	 */
	public static function fromString( $str )
	{
		switch( $str ){
		case 'NO_CONVERT':		return new EncodingConverter( EncodingConverter::NO_CONVERT );	break;
		case 'PHP_TO_DB':		return new EncodingConverter( EncodingConverter::PHP_TO_DB );		break;
		case 'PHP_TO_HTML':		return new EncodingConverter( EncodingConverter::PHP_TO_HTML );	break;
		case 'PHP_TO_LOG':		return new EncodingConverter( EncodingConverter::PHP_TO_LOG );	break;
		case 'HTML_TO_PHP':		return new EncodingConverter( EncodingConverter::HTML_TO_PHP );	break;
		case 'DB_TO_PHP':		return new EncodingConverter( EncodingConverter::DB_TO_PHP );		break;
		case 'DB_TO_HTML':		return new EncodingConverter( EncodingConverter::DB_TO_HTML );	break;
		case 'DB_TO_LOG':		return new EncodingConverter( EncodingConverter::DB_TO_LOG );		break;
		case 'HTML_TO_DB':		return new EncodingConverter( EncodingConverter::HTML_TO_DB );	break;
		case 'PHP_TO_MAIL':		return new EncodingConverter( EncodingConverter::PHP_TO_MAIL );	break;
		}

		throw new EncodingConverterException( "Illegal fromString() parameter: $str" );
	}

	/*
	 *	変換
	 */
	public function convertEncoding( $str )
	{
		if ( $this->_type == EncodingConverter::NO_CONVERT ){
			return $str;
		}
//print "EncodingConvert:" . $this->_from . "-->" . $this->_to . " message:" . $str . "<BR>";
//print "EncodingConvert:" . $this->_type . " message:" . $str . "<BR>";

		return mb_convert_encoding( $str, $this->_to, $this->_from );
	}

	/*
	 *	配列を変換
	 */
	public function convertEncodingArray( $ary )
	{
		if ( $ary == NULL ){
			throw new NullPointerException( $ary );
		}
		if ( !is_array($ary) ){
			throw new ParameterException( "ary", gettype($ary), "must be an array" );
		}

		$new_array = array();

		foreach( $ary as $value ){
			$new_value = mb_convert_encoding( $value, $this->_to, $this->_from );
//print "convertEncodingArray:" . $value . " => " . $new_value . "<BR>";
			$new_array[] = $new_value;
		}

		return $new_array;
	}

	/*
	 *	配列、文字列を再帰的に変換
	 */
	public function convertEncodingRecursive( $data )
	{
		if ( $data == NULL ){
			throw new NullPointerException( $ary );
		}
		if ( is_array($data) ){
			foreach( $data as $key => $value ){
				if ( $value ){
					$value = $this->convertEncodingRecursive( $value );
					$data[$key] = $value;
				}
			}
		}
		else if ( is_string($data) ){
			$data = $this->convertEncoding( $data );
		}

		return $data;
	}

	/*
	 *	変換タイプを取得
	 */
	public function getType()
	{
		return $this->_type;
	}

	/*
	 *	変換元文字コードを取得
	 */
	public function getFromEncoding()
	{
		return $this->_from;
	}

	/*
	 *	変換先文字コードを取得
	 */
	public function getToEncoding()
	{
		return $this->_to;
	}

	/*
	 *	文字列変換
	 */
	public function __toString()
	{
		return "[EncodingConverter]" . $this->_from . " => " . $this->_to;
	}

	/*
	 *	変換元、変換先文字コードを判定
	 */
	private function setEncodingSettings()
	{
		// 変換元
		switch( $this->_type ){
		case EncodingConverter::PHP_TO_DB:
		case EncodingConverter::PHP_TO_HTML:
		case EncodingConverter::PHP_TO_LOG:
		case EncodingConverter::PHP_TO_MAIL:
			$this->_from = Profile::getString('PHP_CODE');
			break;

		case EncodingConverter::HTML_TO_PHP:
		case EncodingConverter::HTML_TO_DB:
			$this->_from = Profile::getString('HTML_CODE');
			break;

		case EncodingConverter::DB_TO_PHP:
		case EncodingConverter::DB_TO_HTML:
		case EncodingConverter::DB_TO_LOG:
			$this->_from = Profile::getString('DB_CODE');
			break;
		}

		// 変換先
		switch( $this->_type ){
		case EncodingConverter::PHP_TO_LOG:
		case EncodingConverter::DB_TO_LOG:
			$this->_to = Profile::getString('LOG_CODE');
			break;

		case EncodingConverter::HTML_TO_PHP:
		case EncodingConverter::DB_TO_PHP:
			$this->_to = Profile::getString('PHP_CODE');
			break;

		case EncodingConverter::DB_TO_HTML:
		case EncodingConverter::PHP_TO_HTML:
			$this->_to = Profile::getString('HTML_CODE');
			break;

		case EncodingConverter::PHP_TO_DB:
		case EncodingConverter::HTML_TO_DB:
			$this->_to = Profile::getString('DB_CODE');
			break;

		case EncodingConverter::PHP_TO_MAIL:
			$this->_to = "ISO-2022-JP";
			break;

		}
	}
}
