]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Data / SqlMap / DataMapper / TSqlMapTypeHandlerRegistry.php
1 <?php
2 /**
3  * TSqlMapTypeHandlerRegistry, and abstract TSqlMapTypeHandler classes file.
4  *
5  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Data.SqlMap
10  */
11
12 /**
13  * TTypeHandlerFactory provides type handler classes to convert database field type
14  * to PHP types and vice versa.
15  *
16  * @author Wei Zhuo <weizho[at]gmail[dot]com>
17  * @package System.Data.SqlMap
18  * @since 3.1
19  */
20 class TSqlMapTypeHandlerRegistry
21 {
22         private $_typeHandlers=array();
23
24         /**
25          * @param string database field type
26          * @return TSqlMapTypeHandler type handler for give database field type.
27          */
28         public function getDbTypeHandler($dbType='NULL')
29         {
30                 foreach($this->_typeHandlers as $handler)
31                         if($handler->getDbType()===$dbType)
32                                 return $handler;
33         }
34
35         /**
36          * @param string type handler class name
37          * @return TSqlMapTypeHandler type handler
38          */
39         public function getTypeHandler($class)
40         {
41                 if(isset($this->_typeHandlers[$class]))
42                         return $this->_typeHandlers[$class];
43         }
44
45         /**
46          * @param TSqlMapTypeHandler registers a new type handler
47          */
48         public function registerTypeHandler(TSqlMapTypeHandler $handler)
49         {
50                 $this->_typeHandlers[$handler->getType()] = $handler;
51         }
52
53         /**
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.
59          */
60         public function createInstanceOf($type='')
61         {
62                 if(strlen($type) > 0)
63                 {
64                         switch(strtolower($type))
65                         {
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;
71                         }
72
73                         if(class_exists('Prado', false))
74                                 return Prado::createComponent($type);
75                         else if(class_exists($type, false)) //NO auto loading
76                                 return new $type;
77                         else
78                                 throw new TSqlMapException('sqlmap_unable_to_find_class', $type);
79                 }
80         }
81
82         /**
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.
87          */
88         public function convertToType($type, $value)
89         {
90                 switch(strtolower($type))
91                 {
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;
98                         case 'string' :
99                                 $type = 'string'; break;
100                         default:
101                                 return $value;
102                 }
103                 settype($value, $type);
104                 return $value;
105         }
106 }
107
108 /**
109  * A simple interface for implementing custom type handlers.
110  *
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.
120  *
121  * @author Wei Zhuo <weizho[at]gmail[dot]com>
122  * @package System.Data.SqlMap
123  * @since 3.1
124  */
125 abstract class TSqlMapTypeHandler extends TComponent
126 {
127         private $_dbType='NULL';
128         private $_type;
129         /**
130          * @param string database field type.
131          */
132         public function setDbType($value)
133         {
134                 $this->_dbType=$value;
135         }
136
137         /**
138          * @return string database field type.
139          */
140         public function getDbType()
141         {
142                 return $this->_dbType;
143         }
144
145         public function getType()
146         {
147                 if($this->_type===null)
148                         return get_class($this);
149                 else
150                         return $this->_type;
151         }
152
153         public function setType($value)
154         {
155                 $this->_type=$value;
156         }
157
158         /**
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.
163          */
164         public abstract function getParameter($object);
165
166
167         /**
168          * Performs processing on a value before after it has been retrieved
169          * from a database
170          * @param object The interface for getting the value.
171          * @return mixed The processed value.
172          */
173         public abstract function getResult($string);
174
175
176         /**
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
183          * in.
184          * @param array result row.
185          * @return mixed
186          */
187         public abstract function createNewInstance($row=null);
188 }
189