3 * TPropertyAccess class file.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Data.SqlMap
13 * TPropertyAccess class provides dot notation stype property access and setting.
15 * Access object's properties (and subproperties) using dot path notation.
16 * The following are equivalent.
18 * echo $obj->property1;
19 * echo $obj->getProperty1();
20 * echo $obj['property1']; //$obj may be an array or object
21 * echo TPropertyAccess($obj, 'property1');
24 * Setting a property value.
26 * $obj1->propert1 = 'hello';
27 * $obj->setProperty('hello');
28 * $obj['property1'] = 'hello'; //$obj may be an array or object
29 * TPropertyAccess($obj, 'property1', 'hello');
32 * Subproperties are supported using the dot notation. E.g.
34 * echo $obj->property1->property2->property3
35 * echo TPropertyAccess::get($obj, 'property1.property2.property3');
38 * @author Wei Zhuo <weizho[at]gmail[dot]com>
39 * @package System.Data.SqlMap
45 * Gets the property value.
46 * @param mixed object or path.
47 * @param string property path.
48 * @return mixed property value.
49 * @throws TInvalidDataValueException if property path is invalid.
51 public static function get($object,$path)
53 if(!is_array($object) && !is_object($object))
55 $properties = explode('.', $path);
56 foreach($properties as $prop)
58 if(is_array($object) || $object instanceof ArrayAccess)
60 if(array_key_exists($prop, $object))
61 $object = $object[$prop];
63 throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
65 else if(is_object($object))
67 $getter = 'get'.$prop;
68 if(method_exists($object, $getter) && is_callable(array($object, $getter)))
69 $object = $object->{$getter}();
70 else if(in_array($prop, array_keys(get_object_vars($object))))
71 $object = $object->{$prop};
72 elseif(method_exists($object, '__get') && is_callable(array($object, '__get')))
73 $object = $object->{$prop};
75 throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
78 throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
84 * @param mixed object or array
85 * @param string property path.
86 * @return boolean true if property path is valid
88 public static function has($object, $path)
90 if(!is_array($object) && !is_object($object))
92 $properties = explode('.', $path);
93 foreach($properties as $prop)
95 if(is_array($object) || $object instanceof ArrayAccess)
97 if(array_key_exists($prop, $object))
98 $object = $object[$prop];
102 else if(is_object($object))
104 $getter = 'get'.$prop;
105 if(method_exists($object, $getter) && is_callable(array($object, $getter)))
106 $object = $object->{$getter}();
107 else if(in_array($prop, array_keys(get_object_vars($object))))
108 $object = $object->{$prop};
109 elseif(method_exists($object, '__get') && is_callable(array($object, '__get')))
110 $object = $object->{$prop};
121 * Sets the property value.
122 * @param mixed object or array
123 * @param string property path.
124 * @param mixed new property value.
125 * @throws TInvalidDataValueException if property path is invalid.
127 public static function set(&$originalObject, $path, $value)
129 $properties = explode('.', $path);
130 $prop = array_pop($properties);
131 if(count($properties) > 0)
132 $object = self::get($originalObject, implode('.',$properties));
134 $object = &$originalObject;
136 if(is_array($object) || $object instanceof ArrayAccess)
138 $object[$prop] = $value;
140 else if(is_object($object))
142 $setter = 'set'.$prop;
143 if (method_exists($object, $setter) && is_callable(array($object, $setter)))
144 $object->{$setter}($value);
146 $object->{$prop} = $value;
149 throw new TInvalidPropertyException('sqlmap_invalid_property_type',$path);