Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TSqlMapTypeHandlerRegistry, and abstract TSqlMapTypeHandler classes file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TSqlMapTypeHandlerRegistry.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Data.SqlMap
11
 */
12
 
13
/**
14
 * TTypeHandlerFactory provides type handler classes to convert database field type
15
 * to PHP types and vice versa.
16
 *
17
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
18
 * @version $Id: TSqlMapTypeHandlerRegistry.php 2541 2008-10-21 15:05:13Z qiang.xue $
19
 * @package System.Data.SqlMap
20
 * @since 3.1
21
 */
22
class TSqlMapTypeHandlerRegistry
23
{
24
	private $_typeHandlers=array();
25
 
26
	/**
27
	 * @param string database field type
28
	 * @return TSqlMapTypeHandler type handler for give database field type.
29
	 */
30
	public function getDbTypeHandler($dbType='NULL')
31
	{
32
		foreach($this->_typeHandlers as $handler)
33
			if($handler->getDbType()===$dbType)
34
				return $handler;
35
	}
36
 
37
	/**
38
	 * @param string type handler class name
39
	 * @return TSqlMapTypeHandler type handler
40
	 */
41
	public function getTypeHandler($class)
42
	{
43
		if(isset($this->_typeHandlers[$class]))
44
			return $this->_typeHandlers[$class];
45
	}
46
 
47
	/**
48
	 * @param TSqlMapTypeHandler registers a new type handler
49
	 */
50
	public function registerTypeHandler(TSqlMapTypeHandler $handler)
51
	{
52
		$this->_typeHandlers[$handler->getType()] = $handler;
53
	}
54
 
55
	/**
56
	 * Creates a new instance of a particular class (for PHP primative types,
57
	 * their corresponding default value for given type is used).
58
	 * @param string PHP type name
59
	 * @return mixed default type value, if no type is specified null is returned.
60
	 * @throws TSqlMapException if class name is not found.
61
	 */
62
	public function createInstanceOf($type='')
63
	{
64
		if(strlen($type) > 0)
65
		{
66
			switch(strtolower($type))
67
			{
68
				case 'string': return '';
69
				case 'array': return array();
70
				case 'float': case 'double': case 'decimal': return 0.0;
71
				case 'integer': case 'int': return 0;
72
				case 'bool': case 'boolean': return false;
73
			}
74
 
75
			if(class_exists('Prado', false))
76
				return Prado::createComponent($type);
77
			else if(class_exists($type, false)) //NO auto loading
78
				return new $type;
79
			else
80
				throw new TSqlMapException('sqlmap_unable_to_find_class', $type);
81
		}
82
	}
83
 
84
	/**
85
	 * Converts the value to given type using PHP's settype() function.
86
	 * @param string PHP primative type.
87
	 * @param mixed value to be casted
88
	 * @return mixed type casted value.
89
	 */
90
	public function convertToType($type, $value)
91
	{
92
		switch(strtolower($type))
93
		{
94
			case 'integer': case 'int':
95
				$type = 'integer'; break;
96
			case 'float': case 'double': case 'decimal':
97
				$type = 'float'; break;
98
			case 'boolean': case 'bool':
99
				$type = 'boolean'; break;
100
			case 'string' :
101
				$type = 'string'; break;
102
			default:
103
				return $value;
104
		}
105
		settype($value, $type);
106
		return $value;
107
	}
108
}
109
 
110
/**
111
 * A simple interface for implementing custom type handlers.
112
 *
113
 * Using this interface, you can implement a type handler that
114
 * will perform customized processing before parameters are set
115
 * on and after values are retrieved from the database.
116
 * Using a custom type handler you can extend
117
 * the framework to handle types that are not supported, or
118
 * handle supported types in a different way.  For example,
119
 * you might use a custom type handler to implement proprietary
120
 * BLOB support (e.g. Oracle), or you might use it to handle
121
 * booleans using "Y" and "N" instead of the more typical 0/1.
122
 *
123
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
124
 * @version $Id: TSqlMapTypeHandlerRegistry.php 2541 2008-10-21 15:05:13Z qiang.xue $
125
 * @package System.Data.SqlMap
126
 * @since 3.1
127
 */
128
abstract class TSqlMapTypeHandler extends TComponent
129
{
130
	private $_dbType='NULL';
131
	private $_type;
132
	/**
133
	 * @param string database field type.
134
	 */
135
	public function setDbType($value)
136
	{
137
		$this->_dbType=$value;
138
	}
139
 
140
	/**
141
	 * @return string database field type.
142
	 */
143
	public function getDbType()
144
	{
145
		return $this->_dbType;
146
	}
147
 
148
	public function getType()
149
	{
150
		if($this->_type===null)
151
			return get_class($this);
152
		else
153
			return $this->_type;
154
	}
155
 
156
	public function setType($value)
157
	{
158
		$this->_type=$value;
159
	}
160
 
161
	/**
162
	 * Performs processing on a value before it is used to set
163
	 * the parameter of a IDbCommand.
164
	 * @param object The interface for setting the value.
165
	 * @param object The value to be set.
166
	 */
167
	public abstract function getParameter($object);
168
 
169
 
170
	/**
171
	 * Performs processing on a value before after it has been retrieved
172
	 * from a database
173
	 * @param object The interface for getting the value.
174
	 * @return mixed The processed value.
175
	 */
176
	public abstract function getResult($string);
177
 
178
 
179
	/**
180
	 * Casts the string representation of a value into a type recognized by
181
	 * this type handler.  This method is used to translate nullValue values
182
	 * into types that can be appropriately compared.  If your custom type handler
183
	 * cannot support nullValues, or if there is no reasonable string representation
184
	 * for this type (e.g. File type), you can simply return the String representation
185
	 * as it was passed in.  It is not recommended to return null, unless null was passed
186
	 * in.
187
	 * @param array result row.
188
	 * @return mixed
189
	 */
190
	public abstract function createNewInstance($row=null);
191
}
192