summaryrefslogtreecommitdiff
path: root/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php
diff options
context:
space:
mode:
authorwei <>2006-04-14 06:22:09 +0000
committerwei <>2006-04-14 06:22:09 +0000
commit3d3f8d3832921f99daf8ce1953304763c2e76c62 (patch)
treee1b0a9bc3a13fccd253770fb452ac96cc6315121 /framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php
parent373d8acc503b94ea09823f49e2ab5e395eccc584 (diff)
Importing SQLMap + sample + docs.
Diffstat (limited to 'framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php')
-rw-r--r--framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php134
1 files changed, 134 insertions, 0 deletions
diff --git a/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php b/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php
new file mode 100644
index 00000000..fcadea28
--- /dev/null
+++ b/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php
@@ -0,0 +1,134 @@
+<?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($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 string nullValue.
+ * @return mixed
+ */
+ public function createNewInstance();
+}
+
+?> \ No newline at end of file