summaryrefslogtreecommitdiff
path: root/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php
blob: 18b8ef99dd09df0dc1e7900f9ae131ca007c527b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
 * TSqlMapTypeHandlerRegistry, and abstract TSqlMapTypeHandler classes file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package System.Data.SqlMap
 */

/**
 * TTypeHandlerFactory provides type handler classes to convert database field type
 * to PHP types and vice versa.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @package System.Data.SqlMap
 * @since 3.1
 */
class TSqlMapTypeHandlerRegistry
{
	private $_typeHandlers=array();

	/**
	 * @param string database field type
	 * @return TSqlMapTypeHandler type handler for give database field type.
	 */
	public function getDbTypeHandler($dbType='NULL')
	{
		foreach($this->_typeHandlers as $handler)
			if($handler->getDbType()===$dbType)
				return $handler;
	}

	/**
	 * @param string type handler class name
	 * @return TSqlMapTypeHandler type handler
	 */
	public function getTypeHandler($class)
	{
		if(isset($this->_typeHandlers[$class]))
			return $this->_typeHandlers[$class];
	}

	/**
	 * @param TSqlMapTypeHandler registers a new type handler
	 */
	public function registerTypeHandler(TSqlMapTypeHandler $handler)
	{
		$this->_typeHandlers[$handler->getType()] = $handler;
	}

	/**
	 * Creates a new instance of a particular class (for PHP primative types,
	 * their corresponding default value for given type is used).
	 * @param string PHP type name
	 * @return mixed default type value, if no type is specified null is returned.
	 * @throws TSqlMapException if class name is not found.
	 */
	public function createInstanceOf($type='')
	{
		if(strlen($type) > 0)
		{
			switch(strtolower($type))
			{
				case 'string': return '';
				case 'array': return array();
				case 'float': case 'double': case 'decimal': return 0.0;
				case 'integer': case 'int': return 0;
				case 'bool': case 'boolean': return false;
			}

			if(class_exists('Prado', false))
				return Prado::createComponent($type);
			else if(class_exists($type, false)) //NO auto loading
				return new $type;
			else
				throw new TSqlMapException('sqlmap_unable_to_find_class', $type);
		}
	}

	/**
	 * Converts the value to given type using PHP's settype() function.
	 * @param string PHP primative type.
	 * @param mixed value to be casted
	 * @return mixed type casted value.
	 */
	public function convertToType($type, $value)
	{
		switch(strtolower($type))
		{
			case 'integer': case 'int':
				$type = 'integer'; break;
			case 'float': case 'double': case 'decimal':
				$type = 'float'; break;
			case 'boolean': case 'bool':
				$type = 'boolean'; break;
			case 'string' :
				$type = 'string'; break;
			default:
				return $value;
		}
		settype($value, $type);
		return $value;
	}
}

/**
 * A simple interface for implementing custom type handlers.
 *
 * Using this interface, you can implement a type handler that
 * will perform customized processing before parameters are set
 * on and after values are retrieved from the database.
 * Using a custom type handler you can extend
 * the framework to handle types that are not supported, or
 * handle supported types in a different way.  For example,
 * you might use a custom type handler to implement proprietary
 * BLOB support (e.g. Oracle), or you might use it to handle
 * booleans using "Y" and "N" instead of the more typical 0/1.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @package System.Data.SqlMap
 * @since 3.1
 */
abstract class TSqlMapTypeHandler extends TComponent
{
	private $_dbType='NULL';
	private $_type;
	/**
	 * @param string database field type.
	 */
	public function setDbType($value)
	{
		$this->_dbType=$value;
	}

	/**
	 * @return string database field type.
	 */
	public function getDbType()
	{
		return $this->_dbType;
	}

	public function getType()
	{
		if($this->_type===null)
			return get_class($this);
		else
			return $this->_type;
	}

	public function setType($value)
	{
		$this->_type=$value;
	}

	/**
	 * Performs processing on a value before it is used to set
	 * the parameter of a IDbCommand.
	 * @param object The interface for setting the value.
	 * @param object The value to be set.
	 */
	public abstract function getParameter($object);


	/**
	 * Performs processing on a value before after it has been retrieved
	 * from a database
	 * @param object The interface for getting the value.
	 * @return mixed The processed value.
	 */
	public abstract function getResult($string);


	/**
	 * Casts the string representation of a value into a type recognized by
	 * this type handler.  This method is used to translate nullValue values
	 * into types that can be appropriately compared.  If your custom type handler
	 * cannot support nullValues, or if there is no reasonable string representation
	 * for this type (e.g. File type), you can simply return the String representation
	 * as it was passed in.  It is not recommended to return null, unless null was passed
	 * in.
	 * @param array result row.
	 * @return mixed
	 */
	public abstract function createNewInstance($row=null);
}