CChoiceFormat
包 | system.utils |
---|---|
继承 | class CChoiceFormat |
可用自 | 1.0.2 |
版本 | $Id$ |
CChoiceFormat is a helper that chooses an appropriate message based on the specified number value.
The candidate messages are given as a string in the following format:
For example, given the candidate messages 'n==1#one|n==2#two|n>2#others' and the number value 2, the resulting message will be 'two'.
For expressions like 'n==1', we can also use a shortcut '1'. So the above example candidate messages can be simplified as '1#one|2#two|n>2#others'.
In case the given number doesn't select any message, the last candidate message will be returned.
'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.
For example, given the candidate messages 'n==1#one|n==2#two|n>2#others' and the number value 2, the resulting message will be 'two'.
For expressions like 'n==1', we can also use a shortcut '1'. So the above example candidate messages can be simplified as '1#one|2#two|n>2#others'.
In case the given number doesn't select any message, the last candidate message will be returned.
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
format() | Formats a message according to the specified number value. | CChoiceFormat |
受保护的方法
方法 | 描述 | 被定义在 |
---|---|---|
evaluate() | Evaluates a PHP expression with the given number value. | CChoiceFormat |
方法详情
evaluate()
方法
protected static boolean evaluate(string $expression, mixed $n)
| ||
$expression | string | the PHP expression |
$n | mixed | the number value |
{return} | boolean | the expression result |
Evaluates a PHP expression with the given number value.
format()
方法
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 |
Formats a message according to the specified number value.