| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TPropertyAccess class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TPropertyAccess.php 2560 2008-11-07 11:49:53Z carlgmathisen $
|
|
|
10 |
* @package System.Data.SqlMap
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TPropertyAccess class provides dot notation stype property access and setting.
|
|
|
15 |
*
|
|
|
16 |
* Access object's properties (and subproperties) using dot path notation.
|
|
|
17 |
* The following are equivalent.
|
|
|
18 |
* <code>
|
|
|
19 |
* echo $obj->property1;
|
|
|
20 |
* echo $obj->getProperty1();
|
|
|
21 |
* echo $obj['property1']; //$obj may be an array or object
|
|
|
22 |
* echo TPropertyAccess($obj, 'property1');
|
|
|
23 |
* </code>
|
|
|
24 |
*
|
|
|
25 |
* Setting a property value.
|
|
|
26 |
* <code>
|
|
|
27 |
* $obj1->propert1 = 'hello';
|
|
|
28 |
* $obj->setProperty('hello');
|
|
|
29 |
* $obj['property1'] = 'hello'; //$obj may be an array or object
|
|
|
30 |
* TPropertyAccess($obj, 'property1', 'hello');
|
|
|
31 |
* </code>
|
|
|
32 |
*
|
|
|
33 |
* Subproperties are supported using the dot notation. E.g.
|
|
|
34 |
* <code>
|
|
|
35 |
* echo $obj->property1->property2->property3
|
|
|
36 |
* echo TPropertyAccess::get($obj, 'property1.property2.property3');
|
|
|
37 |
* </code>
|
|
|
38 |
*
|
|
|
39 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
40 |
* @version $Id: TPropertyAccess.php 2560 2008-11-07 11:49:53Z carlgmathisen $
|
|
|
41 |
* @package System.Data.SqlMap
|
|
|
42 |
* @since 3.1
|
|
|
43 |
*/
|
|
|
44 |
class TPropertyAccess
|
|
|
45 |
{
|
|
|
46 |
/**
|
|
|
47 |
* Gets the property value.
|
|
|
48 |
* @param mixed object or path.
|
|
|
49 |
* @param string property path.
|
|
|
50 |
* @return mixed property value.
|
|
|
51 |
* @throws TInvalidDataValueException if property path is invalid.
|
|
|
52 |
*/
|
|
|
53 |
public static function get($object,$path)
|
|
|
54 |
{
|
|
|
55 |
if(!is_array($object) && !is_object($object))
|
|
|
56 |
return $object;
|
|
|
57 |
$properties = explode('.', $path);
|
|
|
58 |
foreach($properties as $prop)
|
|
|
59 |
{
|
|
|
60 |
if(is_array($object) || $object instanceof ArrayAccess)
|
|
|
61 |
{
|
|
|
62 |
if(array_key_exists($prop, $object))
|
|
|
63 |
$object = $object[$prop];
|
|
|
64 |
else
|
|
|
65 |
throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
|
|
|
66 |
}
|
|
|
67 |
else if(is_object($object))
|
|
|
68 |
{
|
|
|
69 |
$getter = 'get'.$prop;
|
|
|
70 |
if(method_exists($object, $getter) && is_callable(array($object, $getter)))
|
|
|
71 |
$object = $object->{$getter}();
|
|
|
72 |
else if(in_array($prop, array_keys(get_object_vars($object))))
|
|
|
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 |
else
|
|
|
110 |
return false;
|
|
|
111 |
}
|
|
|
112 |
else
|
|
|
113 |
return false;
|
|
|
114 |
}
|
|
|
115 |
return true;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Sets the property value.
|
|
|
120 |
* @param mixed object or array
|
|
|
121 |
* @param string property path.
|
|
|
122 |
* @param mixed new property value.
|
|
|
123 |
* @throws TInvalidDataValueException if property path is invalid.
|
|
|
124 |
*/
|
|
|
125 |
public static function set(&$originalObject, $path, $value)
|
|
|
126 |
{
|
|
|
127 |
$properties = explode('.', $path);
|
|
|
128 |
$prop = array_pop($properties);
|
|
|
129 |
if(count($properties) > 0)
|
|
|
130 |
$object = self::get($originalObject, implode('.',$properties));
|
|
|
131 |
else
|
|
|
132 |
$object = &$originalObject;
|
|
|
133 |
|
|
|
134 |
if(is_array($object) || $object instanceof ArrayAccess)
|
|
|
135 |
{
|
|
|
136 |
$object[$prop] = $value;
|
|
|
137 |
}
|
|
|
138 |
else if(is_object($object))
|
|
|
139 |
{
|
|
|
140 |
$setter = 'set'.$prop;
|
|
|
141 |
if (method_exists($object, $setter) && is_callable(array($object, $setter)))
|
|
|
142 |
$object->{$setter}($value);
|
|
|
143 |
else
|
|
|
144 |
$object->{$prop} = $value;
|
|
|
145 |
}
|
|
|
146 |
else
|
|
|
147 |
throw new TInvalidPropertyException('sqlmap_invalid_property_type',$path);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
?>
|