diff options
author | wei <> | 2006-04-14 06:22:09 +0000 |
---|---|---|
committer | wei <> | 2006-04-14 06:22:09 +0000 |
commit | 3d3f8d3832921f99daf8ce1953304763c2e76c62 (patch) | |
tree | e1b0a9bc3a13fccd253770fb452ac96cc6315121 /framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php | |
parent | 373d8acc503b94ea09823f49e2ab5e395eccc584 (diff) |
Importing SQLMap + sample + docs.
Diffstat (limited to 'framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php')
-rw-r--r-- | framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php b/framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php new file mode 100644 index 00000000..8680601e --- /dev/null +++ b/framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php @@ -0,0 +1,91 @@ +<?php
+
+class TPropertyAccess
+{
+ private $_obj;
+ private $_performance=false;
+
+ public function __construct($obj,$performance=false)
+ {
+ $this->_obj = $obj;
+ $this->_performance=$performance;
+ }
+
+ public function __get($name)
+ {
+ return self::get($this->_obj,$name,$this->_performance);
+ }
+
+ public function __set($name,$value)
+ {
+ self::set($this->_obj,$name,$value,$this->_performance);
+ }
+
+ /**
+ * Evaluates the data value at the specified field.
+ * - If the data is an array, then the field is treated as an array index
+ * and the corresponding element value is returned;
+ * - If the data is a TMap or TList object, then the field is treated as a key
+ * into the map or list, and the corresponding value is returned.
+ * - If the data is an object, the field is treated as a property or subproperty
+ * defined with getter methods. For example, if the object has a method called
+ * getMyValue(), then field 'MyValue' will retrive the result of this method call.
+ * If getMyValue() returns an object which contains a method getMySubValue(),
+ * then field 'MyValue.MySubValue' will return that method call result.
+ * @param mixed data containing the field value, can be an array, TMap, TList or object.
+ * @param mixed field value
+ * @return mixed value at the specified field
+ * @throw TInvalidDataValueException if field or data is invalid
+ */
+ public static function get($object,$path)
+ {
+ if(!is_array($object) && !is_object($object))
+ return $object;
+ $properties = explode('.', $path);
+ foreach($properties as $prop)
+ {
+ if(is_array($object) || $object instanceof ArrayAccess)
+ {
+ if(isset($object[$prop]))
+ $object = $object[$prop];
+ else
+ throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
+ }
+ else if(is_object($object))
+ {
+ $getter = 'get'.$prop;
+ if(is_callable(array($object,$getter)))
+ $object = $object->{$getter}();
+ else if(in_array($prop, array_keys(get_object_vars($object))))
+ $object = $object->{$prop};
+ else
+ throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
+ }
+ else
+ throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
+ }
+ return $object;
+ }
+
+
+ public static function set($object, $path, $value)
+ {
+ $properties = explode('.', $path);
+ $prop = array_pop($properties);
+ if(count($properties) > 0)
+ $object = self::get($object, implode('.',$properties));
+ if(is_object($object))
+ {
+ $setter = 'set'.$prop;
+ if(is_callable(array($object, $setter)))
+ $object->{$setter}($value);
+ else
+ $object->{$prop} = $value;
+ }
+ else
+ throw new TInvalidPropertyException('sqlmap_invalid_property_type',$path);
+ }
+
+}
+
+?>
\ No newline at end of file |