summaryrefslogtreecommitdiff
path: root/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php
blob: 55fb1f207a0cfee2f2e96d61691a6a8a55bda290 (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
<?php

class TTypeHandlerFactory
{
	private $_typeHandlerMap;
	
	const NullDbType = '__NULL__';

	public function __construct()
	{
		$this->_typeHandlerMap = new TMap;
	}

	public function getTypeHandler($type, $dbType=null)
	{
		$dbTypeHandlerMap = $this->_typeHandlerMap[$type];
		$handler = null;
		if(!is_null($dbTypeHandlerMap))
		{
			if(is_null($dbType))
				$handler = $dbTypeHandlerMap[self::NullDbType];
			else
			{
				$handler = $dbTypeHandlerMap[$dbType];
				if(is_null($handler))
					$handler = $dbTypeHandlerMap[self::NullDbType];
			}
		}
		return $handler;
	}

	public function register($type, $handler, $dbType=null)
	{
		$map = $this->_typeHandlerMap[$type];
		if(is_null($map))
		{
			$map = new TMap;
			$this->_typeHandlerMap->add($type, $map);
		}
		if(is_null($dbType))
			$map->add(self::NullDbType, $handler);
		else
			$map->add($dbType, $handler);
	}

	public static 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 TDataMapperException('sqlmap_unable_to_find_class', $type);
		}
		return null;
	}

	public static 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.
 */
interface ITypeHandlerCallback
{
	/**
	 * 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 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 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 function createNewInstance($row=null);
}

?>