| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TDataFieldAccessor class file
|
|
|
4 |
*
|
|
|
5 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TDataFieldAccessor.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Util
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TDataFieldAccessor class
|
|
|
15 |
*
|
|
|
16 |
* TDataFieldAccessor is a utility class that provides access to a field of some data.
|
|
|
17 |
* The accessor attempts to obtain the field value in the following order:
|
|
|
18 |
* - If the data is an array, then the field is treated as an array index
|
|
|
19 |
* and the corresponding element value is returned;
|
|
|
20 |
* - If the data is a TMap or TList object, then the field is treated as a key
|
|
|
21 |
* into the map or list, and the corresponding value is returned.
|
|
|
22 |
* - If the data is an object, the field is treated as a property or sub-property
|
|
|
23 |
* defined with getter methods. For example, if the object has a method called
|
|
|
24 |
* getMyValue(), then field 'MyValue' will retrieve the result of this method call.
|
|
|
25 |
* If getMyValue() returns an object which contains a method getMySubValue(),
|
|
|
26 |
* then field 'MyValue.MySubValue' will return that method call result.
|
|
|
27 |
*
|
|
|
28 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
29 |
* @version $Id: TDataFieldAccessor.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
30 |
* @package System.Util
|
|
|
31 |
* @since 3.0
|
|
|
32 |
*/
|
|
|
33 |
class TDataFieldAccessor
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* Evaluates the data value at the specified field.
|
|
|
37 |
* - If the data is an array, then the field is treated as an array index
|
|
|
38 |
* and the corresponding element value is returned;
|
|
|
39 |
* - If the data is a TMap or TList object, then the field is treated as a key
|
|
|
40 |
* into the map or list, and the corresponding value is returned.
|
|
|
41 |
* - If the data is an object, the field is treated as a property or sub-property
|
|
|
42 |
* defined with getter methods. For example, if the object has a method called
|
|
|
43 |
* getMyValue(), then field 'MyValue' will retrieve the result of this method call.
|
|
|
44 |
* If getMyValue() returns an object which contains a method getMySubValue(),
|
|
|
45 |
* then field 'MyValue.MySubValue' will return that method call result.
|
|
|
46 |
* @param mixed data containing the field value, can be an array, TMap, TList or object.
|
|
|
47 |
* @param mixed field value
|
|
|
48 |
* @return mixed value at the specified field
|
|
|
49 |
* @throws TInvalidDataValueException if field or data is invalid
|
|
|
50 |
*/
|
|
|
51 |
public static function getDataFieldValue($data,$field)
|
|
|
52 |
{
|
|
|
53 |
try
|
|
|
54 |
{
|
|
|
55 |
if(is_array($data) || ($data instanceof ArrayAccess))
|
|
|
56 |
return $data[$field];
|
|
|
57 |
else if(is_object($data))
|
|
|
58 |
{
|
|
|
59 |
if(strpos($field,'.')===false) // simple field
|
|
|
60 |
{
|
|
|
61 |
if(property_exists($data,$field))
|
|
|
62 |
return $data->{$field};
|
|
|
63 |
else
|
|
|
64 |
return call_user_func(array($data,'get'.$field));
|
|
|
65 |
}
|
|
|
66 |
else // field in the format of xxx.yyy.zzz
|
|
|
67 |
{
|
|
|
68 |
$object=$data;
|
|
|
69 |
foreach(explode('.',$field) as $f)
|
|
|
70 |
$object=$object->$f;
|
|
|
71 |
return $object;
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
catch(Exception $e)
|
|
|
76 |
{
|
|
|
77 |
throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid',$field,$e->getMessage());
|
|
|
78 |
}
|
|
|
79 |
throw new TInvalidDataValueException('datafieldaccessor_data_invalid',$field);
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|