Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

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