Package | system.utils |
---|---|
Inheritance | class CChoiceFormat |
Since | 1.0.2 |
Version | $Id$ |
Source Code | framework/i18n/CChoiceFormat.php |
'expr1#message1|expr2#message2|expr3#message3'where each expression should be a valid PHP expression with 'n' as the only variable. For example, 'n==1' and 'n%10==2 && n>10' are both valid expressions. The variable 'n' will take the given number value, and if an expression evaluates true, the corresponding message will be returned.
Method | Description | Defined By |
---|---|---|
format() | Formats a message according to the specified number value. | CChoiceFormat |
Method | Description | Defined By |
---|---|---|
evaluate() | Evaluates a PHP expression with the given number value. | CChoiceFormat |
protected static boolean evaluate(string $expression, mixed $n)
| ||
$expression | string | the PHP expression |
$n | mixed | the number value |
{return} | boolean | the expression result |
protected static function evaluate($expression,$n)
{
return @eval("return $expression;");
}
Evaluates a PHP expression with the given number value.
public static string format(string $messages, mixed $number)
| ||
$messages | string | the candidate messages in the format of 'expr1#message1|expr2#message2|expr3#message3'. See CChoiceFormat for more details. |
$number | mixed | the number value |
{return} | string | the selected message |
public static function format($messages, $number)
{
$n=preg_match_all('/\s*([^#]*)\s*#([^\|]*)\|/',$messages.'|',$matches);
if($n===0)
return $messages;
for($i=0;$i<$n;++$i)
{
$expression=$matches[1][$i];
$message=$matches[2][$i];
$intval=(int)$expression;
if($expression==="$intval")
{
if($intval==$number)
return $message;
}
else if(self::evaluate(str_replace('n','$n',$expression),$number))
return $message;
}
return $message; // return the last choice
}
Formats a message according to the specified number value.
Signup or Login in order to comment.