]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Data/SqlMap/DataMapper/TPropertyAccess.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Data / SqlMap / DataMapper / TPropertyAccess.php
1 <?php
2 /**
3  * TPropertyAccess class 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  * TPropertyAccess class provides dot notation stype property access and setting.
14  *
15  * Access object's properties (and subproperties) using dot path notation.
16  * The following are equivalent.
17  * <code>
18  * echo $obj->property1;
19  * echo $obj->getProperty1();
20  * echo $obj['property1']; //$obj may be an array or object
21  * echo TPropertyAccess($obj, 'property1');
22  * </code>
23  *
24  * Setting a property value.
25  * <code>
26  * $obj1->propert1 = 'hello';
27  * $obj->setProperty('hello');
28  * $obj['property1'] = 'hello'; //$obj may be an array or object
29  * TPropertyAccess($obj, 'property1', 'hello');
30  * </code>
31  *
32  * Subproperties are supported using the dot notation. E.g.
33  * <code>
34  * echo $obj->property1->property2->property3
35  * echo TPropertyAccess::get($obj, 'property1.property2.property3');
36  * </code>
37  *
38  * @author Wei Zhuo <weizho[at]gmail[dot]com>
39  * @package System.Data.SqlMap
40  * @since 3.1
41  */
42 class TPropertyAccess
43 {
44         /**
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.
50          */
51         public static function get($object,$path)
52         {
53                 if(!is_array($object) && !is_object($object))
54                         return $object;
55                 $properties = explode('.', $path);
56                 foreach($properties as $prop)
57                 {
58                         if(is_array($object) || $object instanceof ArrayAccess)
59                         {
60                                 if(array_key_exists($prop, $object))
61                                         $object = $object[$prop];
62                                 else
63                                         throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
64                         }
65                         else if(is_object($object))
66                         {
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};
74                                 else
75                                         throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
76                         }
77                         else
78                                 throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
79                 }
80                 return $object;
81         }
82
83         /**
84          * @param mixed object or array
85          * @param string property path.
86          * @return boolean true if property path is valid
87          */
88         public static function has($object, $path)
89         {
90                 if(!is_array($object) && !is_object($object))
91                         return false;
92                 $properties = explode('.', $path);
93                 foreach($properties as $prop)
94                 {
95                         if(is_array($object) || $object instanceof ArrayAccess)
96                         {
97                                 if(array_key_exists($prop, $object))
98                                         $object = $object[$prop];
99                                 else
100                                         return false;
101                         }
102                         else if(is_object($object))
103                         {
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};
111                                 else
112                                         return false;
113                         }
114                         else
115                                 return false;
116                 }
117                 return true;
118         }
119
120         /**
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.
126          */
127         public static function set(&$originalObject, $path, $value)
128         {
129                 $properties = explode('.', $path);
130                 $prop = array_pop($properties);
131                 if(count($properties) > 0)
132                         $object = self::get($originalObject, implode('.',$properties));
133                 else
134                         $object = &$originalObject;
135
136                 if(is_array($object) || $object instanceof ArrayAccess)
137                 {
138                         $object[$prop] = $value;
139                 }
140                 else if(is_object($object))
141                 {
142                         $setter = 'set'.$prop;
143                         if (method_exists($object, $setter) && is_callable(array($object, $setter)))
144                                 $object->{$setter}($value);
145                         else
146                                 $object->{$prop} = $value;
147                 }
148                 else
149                         throw new TInvalidPropertyException('sqlmap_invalid_property_type',$path);
150         }
151
152 }
153