3 * TSqlMapTypeHandlerRegistry, and abstract TSqlMapTypeHandler classes file.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Data.SqlMap
13 * TTypeHandlerFactory provides type handler classes to convert database field type
14 * to PHP types and vice versa.
16 * @author Wei Zhuo <weizho[at]gmail[dot]com>
17 * @package System.Data.SqlMap
20 class TSqlMapTypeHandlerRegistry
22 private $_typeHandlers=array();
25 * @param string database field type
26 * @return TSqlMapTypeHandler type handler for give database field type.
28 public function getDbTypeHandler($dbType='NULL')
30 foreach($this->_typeHandlers as $handler)
31 if($handler->getDbType()===$dbType)
36 * @param string type handler class name
37 * @return TSqlMapTypeHandler type handler
39 public function getTypeHandler($class)
41 if(isset($this->_typeHandlers[$class]))
42 return $this->_typeHandlers[$class];
46 * @param TSqlMapTypeHandler registers a new type handler
48 public function registerTypeHandler(TSqlMapTypeHandler $handler)
50 $this->_typeHandlers[$handler->getType()] = $handler;
54 * Creates a new instance of a particular class (for PHP primative types,
55 * their corresponding default value for given type is used).
56 * @param string PHP type name
57 * @return mixed default type value, if no type is specified null is returned.
58 * @throws TSqlMapException if class name is not found.
60 public function createInstanceOf($type='')
64 switch(strtolower($type))
66 case 'string': return '';
67 case 'array': return array();
68 case 'float': case 'double': case 'decimal': return 0.0;
69 case 'integer': case 'int': return 0;
70 case 'bool': case 'boolean': return false;
73 if(class_exists('Prado', false))
74 return Prado::createComponent($type);
75 else if(class_exists($type, false)) //NO auto loading
78 throw new TSqlMapException('sqlmap_unable_to_find_class', $type);
83 * Converts the value to given type using PHP's settype() function.
84 * @param string PHP primative type.
85 * @param mixed value to be casted
86 * @return mixed type casted value.
88 public function convertToType($type, $value)
90 switch(strtolower($type))
92 case 'integer': case 'int':
93 $type = 'integer'; break;
94 case 'float': case 'double': case 'decimal':
95 $type = 'float'; break;
96 case 'boolean': case 'bool':
97 $type = 'boolean'; break;
99 $type = 'string'; break;
103 settype($value, $type);
109 * A simple interface for implementing custom type handlers.
111 * Using this interface, you can implement a type handler that
112 * will perform customized processing before parameters are set
113 * on and after values are retrieved from the database.
114 * Using a custom type handler you can extend
115 * the framework to handle types that are not supported, or
116 * handle supported types in a different way. For example,
117 * you might use a custom type handler to implement proprietary
118 * BLOB support (e.g. Oracle), or you might use it to handle
119 * booleans using "Y" and "N" instead of the more typical 0/1.
121 * @author Wei Zhuo <weizho[at]gmail[dot]com>
122 * @package System.Data.SqlMap
125 abstract class TSqlMapTypeHandler extends TComponent
127 private $_dbType='NULL';
130 * @param string database field type.
132 public function setDbType($value)
134 $this->_dbType=$value;
138 * @return string database field type.
140 public function getDbType()
142 return $this->_dbType;
145 public function getType()
147 if($this->_type===null)
148 return get_class($this);
153 public function setType($value)
159 * Performs processing on a value before it is used to set
160 * the parameter of a IDbCommand.
161 * @param object The interface for setting the value.
162 * @param object The value to be set.
164 public abstract function getParameter($object);
168 * Performs processing on a value before after it has been retrieved
170 * @param object The interface for getting the value.
171 * @return mixed The processed value.
173 public abstract function getResult($string);
177 * Casts the string representation of a value into a type recognized by
178 * this type handler. This method is used to translate nullValue values
179 * into types that can be appropriately compared. If your custom type handler
180 * cannot support nullValues, or if there is no reasonable string representation
181 * for this type (e.g. File type), you can simply return the String representation
182 * as it was passed in. It is not recommended to return null, unless null was passed
184 * @param array result row.
187 public abstract function createNewInstance($row=null);