From 1d94593d09b68786f7035e7b06ec6d8324894d77 Mon Sep 17 00:00:00 2001 From: xue <> Date: Fri, 7 Apr 2006 18:44:07 +0000 Subject: Format string in classes extending TListControl can now evaluate an expression --- framework/Util/TDataValueFormatter.php | 67 ++++++++++++++++++++++++ framework/Web/UI/WebControls/TDataGridColumn.php | 30 ++--------- framework/Web/UI/WebControls/TListControl.php | 17 +++--- 3 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 framework/Util/TDataValueFormatter.php (limited to 'framework') diff --git a/framework/Util/TDataValueFormatter.php b/framework/Util/TDataValueFormatter.php new file mode 100644 index 00000000..797a88d1 --- /dev/null +++ b/framework/Util/TDataValueFormatter.php @@ -0,0 +1,67 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Revision: $ $Date: $ + * @package System.Util + */ + +/** + * TDataValueFormatter class + * + * TDataValueFormatter is a utility class that formats a data value + * according to a format string. + * + * @author Qiang Xue + * @version $Revision: $ $Date: $ + * @package System.Util + * @since 3.0 + */ +class TDataValueFormatter +{ + /** + * Formats the text value according to a format string. + * If the format string is empty, the original value is converted into + * a string and returned. + * If the format string starts with '#', the string is treated as a PHP expression + * within which the token '{0}' is translated with the data value to be formated. + * Otherwise, the format string and the data value are passed + * as the first and second parameters in {@link sprintf}. + * @param string format string + * @param mixed the data associated with the cell + * @param TComponent the context to evaluate the expression + * @return string the formatted result + */ + public static function format($formatString,$value,$context=null) + { + if($formatString==='') + return TPropertyValue::ensureString($value); + else if($formatString[0]==='#') + { + $expression=strtr(substr($formatString,1),array('{0}'=>'$value')); + if($context instanceof TComponent) + return $context->evaluateExpression($expression); + else + { + try + { + if(eval("\$result=$expression;")===false) + throw new Exception(''); + return $result; + } + catch(Exception $e) + { + throw new TInvalidOperationException('datavalueformatter_expression_invalid',$expression,$e->getMessage()); + } + } + } + else + return sprintf($formatString,$value); + } +} + +?> \ No newline at end of file diff --git a/framework/Web/UI/WebControls/TDataGridColumn.php b/framework/Web/UI/WebControls/TDataGridColumn.php index a22fa240..ea825360 100644 --- a/framework/Web/UI/WebControls/TDataGridColumn.php +++ b/framework/Web/UI/WebControls/TDataGridColumn.php @@ -11,9 +11,10 @@ */ /** - * Includes TDataFieldAccessor class + * Includes TDataFieldAccessor and TDataValueFormatter classes */ Prado::using('System.Util.TDataFieldAccessor'); +Prado::using('System.Util.TDataValueFormatter'); /** * TDataGridColumn class @@ -332,36 +333,15 @@ abstract class TDataGridColumn extends TApplicationComponent /** * Formats the text value according to a format string. - * If the format string is empty, the original value is converted into - * a string and returned. - * If the format string starts with '#', the string is treated as a PHP expression - * within which the token '$$' is translated with the data value to be formated. - * Otherwise, the format string and the data value are passed - * as the first and second parameters in {@link sprintf}. + * It uses {@link TDataValueFormatter} to do the actual formatting. * @param string format string * @param mixed the data associated with the cell * @return string the formatted result + * @see TDataValueFormatter::format() */ protected function formatDataValue($formatString,$value) { - if($formatString==='') - return TPropertyValue::ensureString($value); - else if($formatString[0]==='#') - { - $expression=strtr(substr($formatString,1),array('$$'=>'$value')); - try - { - if(eval("\$result=$expression;")===false) - throw new Exception(''); - return $result; - } - catch(Exception $e) - { - throw new TInvalidOperationException('datagridcolumn_expression_invalid',get_class($this),$expression,$e->getMessage()); - } - } - else - return sprintf($formatString,$value); + return TDataValueFormatter::format($formatString,$value,$this); } } diff --git a/framework/Web/UI/WebControls/TListControl.php b/framework/Web/UI/WebControls/TListControl.php index 37c232e6..5c1e9083 100644 --- a/framework/Web/UI/WebControls/TListControl.php +++ b/framework/Web/UI/WebControls/TListControl.php @@ -11,17 +11,13 @@ */ /** - * Includes TDataBoundControl class + * Includes the supporting classes */ Prado::using('System.Web.UI.WebControls.TDataBoundControl'); -/** - * Includes TAttributeCollection class - */ Prado::using('System.Collections.TAttributeCollection'); -/** - * Includes TDataFieldAccessor class - */ Prado::using('System.Util.TDataFieldAccessor'); +Prado::using('System.Util.TDataValueFormatter'); + /** * TListControl class @@ -75,7 +71,7 @@ Prado::using('System.Util.TDataFieldAccessor'); * the second item's text be 'Cary', value be 28, and so on. * The {@link setDataTextFormatString DataTextFormatString} property may be further * used to format how the item should be displayed. The formatting function is - * the sprintf() PHP function. + * {@link TDataValueFormatter::format()}. * * @author Qiang Xue * @version $Revision: $ $Date: $ @@ -192,7 +188,7 @@ abstract class TListControl extends TDataBoundControl $text=$object; $item->setValue("$key"); } - $item->setText($textFormat===''?$text:sprintf($textFormat,$text)); + $item->setText(TDataValueFormatter::format($textFormat,$text,$this)); $items->add($item); } // SelectedValue or SelectedIndex may be set before databinding @@ -329,9 +325,10 @@ abstract class TListControl extends TDataBoundControl /** * Sets data text format string. - * The format string is used in sprintf() to format the Text property value + * The format string is used in {@link TDataValueFormatter::format()} to format the Text property value * of each item in the list control. * @param string the formatting string used to control how data bound to the list control is displayed. + * @see TDataValueFormatter::format() */ public function setDataTextFormatString($value) { -- cgit v1.2.3