blob: 6d3bc81f89ef1aa5644689af7beead8920cca39b (
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
|
<?php
/**
* TSqlMapTypeHandlerRegistry, and abstract TSqlMapTypeHandler classes file.
*
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
* @link http://www.pradosoft.com/
* @copyright Copyright © 2005-2014 PradoSoft
* @license http://www.pradosoft.com/license/
* @package Prado\Data\SqlMap\DataMapper
*/
namespace Prado\Data\SqlMap\DataMapper;
use Prado\Prado;
/**
* 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 Prado\Data\SqlMap\DataMapper
* @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;
}
}
|