CChoiceFormat.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * YiiBase class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CChoiceFormat is a helper that chooses an appropriate message based on the specified number value.
  12. * The candidate messages are given as a string in the following format:
  13. * <pre>
  14. * 'expr1#message1|expr2#message2|expr3#message3'
  15. * </pre>
  16. * where each expression should be a valid PHP expression with <code>'n'</code> as the only variable.
  17. * For example, <code>'n==1'</code> and <code>'n%10==2 && n>10'</code> are both valid expressions.
  18. * The variable <code>'n'</code> will take the given number value, and if an expression evaluates true,
  19. * the corresponding message will be returned.
  20. *
  21. * For example, given the candidate messages <code>'n==1#one|n==2#two|n>2#others'</code> and
  22. * the number value 2, the resulting message will be <code>'two'</code>.
  23. *
  24. * For expressions like <code>'n==1'</code>, we can also use a shortcut <code>'1'</code>. So the above example
  25. * candidate messages can be simplified as <code>'1#one|2#two|n>2#others'</code>.
  26. *
  27. * In case the given number doesn't select any message, the last candidate message
  28. * will be returned.
  29. *
  30. * The PHP expressions will be evaluated using {@link evaluate}.
  31. *
  32. * @author Qiang Xue <qiang.xue@gmail.com>
  33. * @package system.i18n
  34. */
  35. class CChoiceFormat
  36. {
  37. /**
  38. * Formats a message according to the specified number value.
  39. * @param string $messages the candidate messages in the format of 'expr1#message1|expr2#message2|expr3#message3'.
  40. * See {@link CChoiceFormat} for more details.
  41. * @param mixed $number the number value
  42. * @return string the selected message
  43. */
  44. public static function format($messages, $number)
  45. {
  46. $n=preg_match_all('/\s*([^#]*)\s*#([^\|]*)\|/',$messages.'|',$matches);
  47. if($n===0)
  48. return $messages;
  49. for($i=0;$i<$n;++$i)
  50. {
  51. $expression=$matches[1][$i];
  52. $message=$matches[2][$i];
  53. if($expression===(string)(int)$expression)
  54. {
  55. if($expression==$number)
  56. return $message;
  57. }
  58. elseif(self::evaluate(str_replace('n','$n',$expression),$number))
  59. return $message;
  60. }
  61. return $message; // return the last choice
  62. }
  63. /**
  64. * Evaluates a PHP expression with the given number value.
  65. * @param string $expression the PHP expression
  66. * @param mixed $n the number value
  67. * @return boolean the expression result
  68. */
  69. protected static function evaluate($expression,$n)
  70. {
  71. return @eval("return $expression;");
  72. }
  73. }