1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
<?php
/**
* TDataFieldAccessor class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.pradosoft.com/
* @copyright Copyright © 2005 PradoSoft
* @license http://www.pradosoft.com/license/
* @version $Revision: $ $Date: $
* @package System.Data
*/
/**
* TDataFieldAccessor class
*
* TDataFieldAccessor is a utility class that provides access to a field of some data.
* The accessor attempts to obtain the field value in the following order:
* - 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.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System.Data
* @since 3.0
*/
class TDataFieldAccessor
{
/**
* 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 getDataFieldValue($data,$field)
{
if(Prado::getApplication()->getMode()===TApplication::STATE_PERFORMANCE)
{
if(is_array($data) || ($data instanceof ArrayAccess))
return $data[$field];
else if(is_object($data))
{
if(strpos($field,'.')===false) // simple field
return call_user_func(array($data,'get'.$field));
else // field in the format of xxx.yyy.zzz
{
$object=$data;
foreach(explode('.',$field) as $f)
$object=call_user_func(array($object,'get'.$f));
return $object;
}
}
else
throw new TInvalidDataValueException('datafieldaccessor_data_invalid',$field);
}
else
{
if(is_array($data) || ($data instanceof ArrayAccess))
{
if(isset($data[$field]))
return $data[$field];
else
throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid',$field);
}
else if(is_object($data))
{
if(strpos($field,'.')===false) // simple field
{
$getter='get'.$field;
if(is_callable(array($data,$getter)))
return call_user_func(array($data,$getter));
else if(in_array($field, array_keys(get_object_vars($data))))
return $data->{$field};
}
else // field in the format of xxx.yyy.zzz
{
$object=$data;
foreach(explode('.',$field) as $f)
{
$getter='get'.$f;
if(is_callable(array($object,$getter)))
$object=call_user_func(array($object,$getter));
else
throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid',$field);
}
return $object;
}
}
else
throw new TInvalidDataValueException('datafieldaccessor_data_invalid',$field);
}
}
}
?>
|