00001 <?php
00002
00003
00004
00005
00006
00007
00008 class ConverterFactory {
00009 const HTML = 'html';
00010 const HTML_EX = 'htmlex';
00011 const CALLBACK = 'callback';
00012 const MIMEHEADER = 'mimeheader';
00013 const NONE = 'none';
00014
00015
00016
00017
00018
00019
00020 private static $registered_converters = array();
00021
00022
00023
00024
00025
00026
00027
00028 public static function create($type) {
00029 $ret = false;
00030 switch ($type) {
00031 case self::HTML:
00032 require_once dirname(__FILE__) . '/converters/html.converter.php';
00033 $ret = new ConverterHtml();
00034 break;
00035 case self::HTML_EX:
00036 require_once dirname(__FILE__) . '/converters/htmlex.converter.php';
00037 $ret = new ConverterHtmlEx();
00038 break;
00039 case self::MIMEHEADER:
00040 require_once dirname(__FILE__) . '/converters/mimeheader.converter.php';
00041 $ret = new ConverterMimeHeader();
00042 break;
00043 case self::CALLBACK:
00044 require_once dirname(__FILE__) . '/converters/callback.converter.php';
00045 $ret = new ConverterCallback();
00046 break;
00047 case self::NONE:
00048 require_once dirname(__FILE__) . '/converters/none.converter.php';
00049 $ret = new ConverterNone();
00050 break;
00051 default:
00052 $ret = Arr::get_item(self::$registered_converters, $type, false);
00053 break;
00054 }
00055 return $ret;
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065 public static function encode($value, $type, $params = false) {
00066 $converter = self::create($type);
00067 if ($converter) {
00068 return $converter->encode($value, $params);
00069 }
00070 return false;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080 public static function decode($value, $type, $params = false) {
00081 $converter = self::create($type);
00082 if ($converter) {
00083 return $converter->decode($value, $params);
00084 }
00085 return false;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095 public static function register_converter($type, IConverter $converter) {
00096 self::$registered_converters[$type] = $converter;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 public static function create_chain($arr_converters) {
00121 require_once dirname(__FILE__) . '/converters/chain.converter.php';
00122 $ret = new ConverterChain();
00123
00124 foreach($arr_converters as $name_or_index => $params_or_name) {
00125 $converter = false;
00126 $params = false;
00127 if ($params_or_name instanceof IConverter) {
00128 $converter = $params_or_name;
00129 }
00130 else {
00131 $has_index = is_numeric($name_or_index);
00132 $name = ($has_index) ? $params_or_name : $name_or_index;
00133 $params = ($has_index) ? false : $params_or_name;
00134
00135 $converter = self::create($name);
00136 if (!$converter instanceof IConverter) {
00137 throw new Exception("Unknown Covnerter $name");
00138 }
00139 }
00140 $ret->append($converter, $params);
00141 }
00142
00143 return $ret;
00144 }
00145 }