Development

/branches/1.4/lib/i18n/sfChoiceFormat.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/i18n/sfChoiceFormat.class.php

Revision 33251, 5.7 kB (checked in by fabien, 2 years ago)

[1.4] fixed sfChoiceFormat when a string to translate contains a valid range (closes #9973)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /**
4  * sfChoiceFormat class file.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the BSD License.
8  *
9  * Copyright(c) 2004 by Qiang Xue. All rights reserved.
10  *
11  * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
12  * The latest version of PRADO can be obtained from:
13  * {@link http://prado.sourceforge.net/}
14  *
15  * @author     Wei Zhuo <weizhuo[at]gmail[dot]com>
16  * @version    $Id$
17  * @package    symfony
18  * @subpackage i18n
19  */
20
21
22 /**
23  * sfChoiceFormat class.
24  *
25  * sfChoiceFormat converts between ranges of numeric values and string
26  * names for those ranges.
27  *
28  * A sfChoiceFormat splits the real number line -Inf to +Inf into two or
29  * more contiguous ranges. Each range is mapped to a string.
30  * sfChoiceFormat is generally used in a MessageFormat for displaying
31  * grammatically correct plurals such as "There are 2 files."
32  *
33  * <code>
34  *  $string = '[0] are no files |[1] is one file |(1,Inf] are {number} files';
35  * 
36  *  $formatter = new sfMessageFormat(...); //init for a source
37  *  $translated = $formatter->format($string);
38  *
39  *  $choice = new sfChoiceFormat();
40  *  echo $choice->format($translated, 0); //shows "are no files"
41  * </code>
42  *
43  * The message/string choices are separated by the pipe "|" followed
44  * by a set notation of the form
45  *  # <t>[1,2]</t> -- accepts values between 1 and 2, inclusive.
46  *  # <t>(1,2)</t> -- accepts values between 1 and 2, excluding 1 and 2.
47  *  # <t>{1,2,3,4}</t> -- only values defined in the set are accepted.
48  *  # <t>[-Inf,0)</t> -- accepts value greater or equal to negative infinity
49  *                       and strictly less than 0
50  * Any non-empty combinations of the delimiters of square and round brackets
51  * are acceptable.
52  *
53  * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
54  * @version v1.0, last update on Fri Dec 24 20:46:16 EST 2004
55  * @package    symfony
56  * @subpackage i18n
57  */
58 class sfChoiceFormat
59 {
60   /**
61    * The pattern to validate a set notation
62    */
63   protected $validate = '/[\(\[\{]|[-Inf\d:\s]+|,|[\+Inf\d\s:\?\-=!><%\|&\(\)]+|[\)\]\}]/ms';
64
65   /**
66    * The pattern to parse the formatting string.
67    */
68   protected $parse = '/(?:^\s*|\s*\|)([\(\[\{]([-Inf\d:\s]+,?[\+Inf\d\s:\?\-=!><%\|&\(\)]*)+[\)\]\}])\s*/';
69
70   /**
71    * The value for positive infinity.
72    */
73   protected $inf;
74
75   /**
76    * Constructor.
77    */
78   public function __construct()
79   {
80     $this->inf = -log(0);
81   }
82
83   /**
84    * Determines if the given number belongs to a given set
85    *
86    * @param  float  $number the number to test.
87    * @param  string $set    the set, in set notation.
88    * @return boolean true if number is in the set, false otherwise.
89    */
90   public function isValid($number, $set)
91   {
92     $n = preg_match_all($this->validate, $set, $matches, PREG_SET_ORDER);
93
94     if ($n < 3)
95     {
96       throw new sfException(sprintf('Invalid set "%s".', $set));
97     }
98
99     if (preg_match('/\{\s*n:([^\}]+)\}/', $set, $def))
100     {
101       return $this->isValidSetNotation($number, $def[1]);
102     }
103
104     $leftBracket = $matches[0][0];
105     $rightBracket = $matches[$n - 1][0];
106
107     $i = 0;
108     $elements = array();
109
110     foreach ($matches as $match)
111     {
112       $string = $match[0];
113       if ($i != 0 && $i != $n - 1 && $string !== ',')
114       {
115         if ($string == '-Inf')
116         {
117           $elements[] = -1 * $this->inf;
118         }
119         else if ($string == '+Inf' || $string == 'Inf')
120         {
121           $elements[] = $this->inf;
122         }
123         else
124         {
125           $elements[] = floatval($string);
126         }
127       }
128       $i++;
129     }
130     $total = count($elements);
131     $number = floatval($number);
132
133     if ($leftBracket == '{' && $rightBracket == '}')
134     {
135       return in_array($number, $elements);
136     }
137
138     $left = false;
139     if ($leftBracket == '[')
140     {
141       $left = $number >= $elements[0];
142     }
143     else if ($leftBracket == '(')
144     {
145       $left = $number > $elements[0];
146     }
147
148     $right = false;
149     if ($rightBracket == ']')
150     {
151       $right = $number <= $elements[$total - 1];
152     }
153     else if ($rightBracket == ')')
154     {
155       $right = $number < $elements[$total - 1];
156     }
157
158     if ($left && $right)
159     {
160       return true;
161     }
162
163     return false;
164   }
165
166   protected function isValidSetNotation($number, $set)
167   {
168     $str = '$result = '.str_replace('n', '$number', $set).';';
169     try
170     {
171       eval($str);
172       return $result;
173     }
174     catch (Exception $e)
175     {
176       return false;
177     }
178   }
179
180   /**
181    * Parses a choice string and get a list of sets and a list of strings corresponding to the sets.
182    *
183    * @param  string $string the string containing the choices
184    * @return array array($sets, $strings)
185    */
186   public function parse($string)
187   {
188     $n = preg_match_all($this->parse, $string, $matches, PREG_OFFSET_CAPTURE);
189     $sets = array();
190     foreach ($matches[1] as $match)
191     {
192       $sets[] = $match[0];
193     }
194
195     $offset = $matches[0];
196     $strings = array();
197     for ($i = 0; $i < $n; $i++)
198     {
199       $len = strlen($offset[$i][0]);
200       $begin = $i == 0 ? $len : $offset[$i][1] + $len;
201       $end = $i == $n - 1 ? strlen($string) : $offset[$i + 1][1];
202       $strings[] = substr($string, $begin, $end - $begin);
203     }
204
205     return array($sets, $strings);
206   }
207
208   /**
209    * For the choice string, and a number, find and return the string that satisfied the set within the choices.
210    *
211    * @param  string $string   the choices string.
212    * @param  float  $number   the number to test.
213    * @return string the choosen string.
214    */
215   public function format($string, $number)
216   {
217     list($sets, $strings) = $this->parse($string);
218     $total = count($sets);
219     for ($i = 0; $i < $total; $i++)
220     {
221       if ($this->isValid($number, $sets[$i]))
222       {
223         return $strings[$i];
224       }
225     }
226
227     return false;
228   }
229 }
230
Note: See TracBrowser for help on using the browser.