Development

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

You must first sign up to be able to contribute.

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

Revision 6806, 5.3 kB (checked in by fabien, 7 years ago)

fixed phpdoc package/subpackage entries

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
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]+|,|[\+Inf\d]+|[\)\]\}]/ms';
64
65   /**
66    * The pattern to parse the formatting string.
67    */
68   protected $parse = '/\s?\|?([\(\[\{]([-Inf\d]+,?[\+Inf\d]*)+[\)\]\}])\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 the number to test.
87    * @param  string 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       $error = 'Invalid set "%s"';
97       $error = sprintf($error, $set);
98       throw new sfException($error);
99     }
100
101     $leftBracket = $matches[0][0];
102     $rightBracket = $matches[$n - 1][0];
103
104     $i = 0;
105     $elements = array();
106
107     foreach ($matches as $match)
108     {
109       $string = $match[0];
110       if ($i != 0 && $i != $n - 1 && $string !== ',')
111       {
112         if ($string == '-Inf')
113         {
114           $elements[] = -1 * $this->inf;
115         }
116         else if ($string == '+Inf' || $string == 'Inf')
117         {
118           $elements[] = $this->inf;
119         }
120         else
121         {
122           $elements[] = floatval($string);
123         }
124       }
125       $i++;
126     }
127     $total = count($elements);
128     $number = floatval($number);
129
130     if ($leftBracket == '{' && $rightBracket == '}')
131     {
132       return in_array($number, $elements);
133     }
134
135     $left = false;
136     if ($leftBracket == '[')
137     {
138       $left = $number >= $elements[0];
139     }
140     else if ($leftBracket == '(')
141     {
142       $left = $number > $elements[0];
143     }
144
145     $right = false;
146     if ($rightBracket==']')
147     {
148       $right = $number <= $elements[$total - 1];
149     }
150     else if ($rightBracket == ')')
151     {
152       $right = $number < $elements[$total - 1];
153     }
154
155     if ($left && $right)
156     {
157       return true;
158     }
159
160     return false;
161   }
162
163   /**
164    * Parses a choice string and get a list of sets and a list of strings corresponding to the sets.
165    *
166    * @param  string the string containing the choices
167    * @return array array($sets, $strings)
168    */
169   public function parse($string)
170   {
171     $n = preg_match_all($this->parse, $string, $matches, PREG_OFFSET_CAPTURE);
172     $sets = array();
173     foreach ($matches[1] as $match)
174     {
175       $sets[] = $match[0];
176     }
177
178     $offset = $matches[0];
179     $strings = array();
180     for ($i = 0; $i < $n; $i++)
181     {
182       $len = strlen($offset[$i][0]);
183       $begin = $i == 0 ? $len : $offset[$i][1] + $len;
184       $end = $i == $n - 1 ? strlen($string) : $offset[$i + 1][1];
185       $strings[] = substr($string, $begin, $end - $begin);
186     }
187
188     return array($sets, $strings);
189   }
190
191   /**
192    * For the choice string, and a number, find and return the string that satisfied the set within the choices.
193    *
194    * @param  string the choices string.
195    * @param  float the number to test.
196    * @return string the choosen string.
197    */
198   public function format($string, $number)
199   {
200     list($sets, $strings) = $this->parse($string);
201     $total = count($sets);
202     for ($i = 0; $i < $total; $i++)
203     {
204       if ($this->isValid($number, $sets[$i]))
205       {
206         return $strings[$i];
207       }
208     }
209
210     return false;
211   }
212 }
213
Note: See TracBrowser for help on using the browser.