diff options
| -rw-r--r-- | .gitattributes | 1 | ||||
| -rw-r--r-- | buildscripts/phpbuilder/files.txt | 1 | ||||
| -rw-r--r-- | framework/Data/TDataFieldAccessor.php | 118 | ||||
| -rw-r--r-- | framework/Exceptions/messages.txt | 3 | ||||
| -rw-r--r-- | framework/Web/UI/WebControls/TDataGridColumn.php | 13 | ||||
| -rw-r--r-- | framework/core.php | 4 | 
6 files changed, 128 insertions, 12 deletions
| diff --git a/.gitattributes b/.gitattributes index 392e37c7..5b0fc88f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -267,6 +267,7 @@ framework/Collections/TMap.php -text  framework/Collections/TPagedDataSource.php -text  framework/Data/TAPCCache.php -text  framework/Data/TCache.php -text +framework/Data/TDataFieldAccessor.php -text  framework/Data/TDateTimeSimpleFormatter.php -text  framework/Data/TMemCache.php -text  framework/Data/TSqliteCache.php -text diff --git a/buildscripts/phpbuilder/files.txt b/buildscripts/phpbuilder/files.txt index 143f7404..f2b3676f 100644 --- a/buildscripts/phpbuilder/files.txt +++ b/buildscripts/phpbuilder/files.txt @@ -10,6 +10,7 @@ Collections/TAttributeCollection.php  Data/TXmlDocument.php
  Web/THttpUtility.php
  Data/TCache.php
 +Data/TDataFieldAccessor.php
  Log/TLogger.php
  core.php
 diff --git a/framework/Data/TDataFieldAccessor.php b/framework/Data/TDataFieldAccessor.php new file mode 100644 index 00000000..2c2be00f --- /dev/null +++ b/framework/Data/TDataFieldAccessor.php @@ -0,0 +1,118 @@ +<?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))
 +				return $data[$field];
 +			else if(($data instanceof TMap) || ($data instanceof TList))
 +				return $data->itemAt($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))
 +			{
 +				if(isset($data[$field]))
 +					return $data[$field];
 +				else
 +					throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid',$field);
 +			}
 +			else if(($data instanceof TMap) || ($data instanceof TList))
 +			{
 +				if($data->contains($field))
 +					return $data->itemAt($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 // 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);
 +		}
 +	}
 +}
 +
 +?>
\ No newline at end of file diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt index ebe645e8..d0c1a7be 100644 --- a/framework/Exceptions/messages.txt +++ b/framework/Exceptions/messages.txt @@ -203,7 +203,8 @@ datagrid_virtualitemcount_invalid		= TDataGrid.VirtualItemCount must be no less  datagridpagerstyle_pagebuttoncount_invalid = TDataGridPagerStyle.PageButtonCount must be greater than 0.
 -datagridcolumn_data_invalid				= %s is unable to evaluate the value of data field %s. The data must be an array, TList, TMap, or component that contains the field.
 +datafieldaccessor_data_invalid			= TDataFieldAccessor is trying to evaluate a field value of an invalid data. Make sure the data is an array, TMap, TList, or object that contains the specified field '%s'.
 +datafieldaccessor_datafield_invalid		= TDataFieldAccessor is trying to evaluate data value of an unknown field '%s'.
  repeateritemcollection_repeateritem_required = TRepeaterItemCollection can only accept TRepeaterItem objects.
 diff --git a/framework/Web/UI/WebControls/TDataGridColumn.php b/framework/Web/UI/WebControls/TDataGridColumn.php index 925fa90b..6e18a18f 100644 --- a/framework/Web/UI/WebControls/TDataGridColumn.php +++ b/framework/Web/UI/WebControls/TDataGridColumn.php @@ -256,19 +256,10 @@ abstract class TDataGridColumn extends TComponent  	 */
  	protected function getDataFieldValue($data,$field)
  	{
 -		if(is_array($data))
 -			return $data[$field];
 -		else if(($data instanceof TMap) || ($data instanceof TList))
 -			return $data->itemAt($field);
 -		else if(($data instanceof TComponent) && $data->canGetProperty($field))
 -		{
 -			$getter='get'.$field;
 -			return $data->$getter();
 -		}
 -		else
 -			throw new TInvalidDataValueException('datagridcolumn_data_invalid',get_class($this),$field);
 +		return TDataFieldAccessor::getDataFieldValue($data,$field);
  	}
 +
  	/**
  	 * Initializes the specified cell to its initial values.
  	 * The default implementation sets the content of header and footer cells.
 diff --git a/framework/core.php b/framework/core.php index 3f5493d5..e43b1168 100644 --- a/framework/core.php +++ b/framework/core.php @@ -46,6 +46,10 @@ require_once(PRADO_DIR.'/Web/THttpUtility.php');   */
  require_once(PRADO_DIR.'/Data/TCache.php');
  /**
 + * Includes TDataFieldAccessor definition
 + */
 +require_once(PRADO_DIR.'/Data/TDataFieldAccessor.php');
 +/**
   * Includes TLogger definition
   */
  require_once(PRADO_DIR.'/Log/TLogger.php');
 | 
