| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* ChoiceFormat 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 $Revision: 1.1 $ $Date: 2005/01/11 07:19:39 $
|
|
|
17 |
* @package System.I18N.core
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* ChoiceFormat class.
|
|
|
23 |
*
|
|
|
24 |
* ChoiceFormat converts between ranges of numeric values and string
|
|
|
25 |
* names for those ranges.
|
|
|
26 |
*
|
|
|
27 |
* A ChoiceFormat splits the real number line -Inf to +Inf into two or
|
|
|
28 |
* more contiguous ranges. Each range is mapped to a string.
|
|
|
29 |
* ChoiceFormat is generally used in a MessageFormat for displaying
|
|
|
30 |
* grammatically correct plurals such as "There are 2 files."
|
|
|
31 |
*
|
|
|
32 |
* <code>
|
|
|
33 |
* $string = '[0] are no files |[1] is one file |(1,Inf] are {number} files';
|
|
|
34 |
*
|
|
|
35 |
* $formatter = new MessageFormat(...); //init for a source
|
|
|
36 |
* $translated = $formatter->format($string);
|
|
|
37 |
*
|
|
|
38 |
* $choice = new ChoiceFormat();
|
|
|
39 |
* echo $choice->format($translated, 0); //shows "are no files"
|
|
|
40 |
* </code>
|
|
|
41 |
*
|
|
|
42 |
* The message/string choices are separated by the pipe "|" followed
|
|
|
43 |
* by a set notation of the form
|
|
|
44 |
* # <tt>[1,2]</tt> -- accepts values between 1 and 2, inclusive.
|
|
|
45 |
* # <tt>(1,2)</tt> -- accepts values between 1 and 2, excluding 1 and 2.
|
|
|
46 |
* # <tt>{1,2,3,4}</tt> -- only values defined in the set are accepted.
|
|
|
47 |
* # <tt>[-Inf,0)</tt> -- accepts value greater or equal to negative infinity
|
|
|
48 |
* and strictly less than 0
|
|
|
49 |
* Any non-empty combinations of the delimiters of square and round brackets
|
|
|
50 |
* are acceptable.
|
|
|
51 |
*
|
|
|
52 |
* Since version 3.1.2 the following set notation is also possible.
|
|
|
53 |
*
|
|
|
54 |
* # <tt>{n: n % 10 > 1 && n % 10 < 5}</tt> -- matches numbers like 2, 3, 4, 22, 23, 24
|
|
|
55 |
*
|
|
|
56 |
* Where set is defined by the expression after <tt>n:</tt>. In particular, the expression
|
|
|
57 |
* accepts the following mathematical/logical operators to form a set of logical conditions
|
|
|
58 |
* on the value given by <tt>n</tt>:
|
|
|
59 |
* # <tt><</tt> -- less than.
|
|
|
60 |
* # <tt><=</tt> -- less than equals.
|
|
|
61 |
* # <tt>></tt> -- greater than.
|
|
|
62 |
* # <tt>>=</tt> -- greater than equals.
|
|
|
63 |
* # <tt>==</tt> -- of equal value.
|
|
|
64 |
* # <tt>%</tt> -- modulo, e.g., 1 % 10 equals 1, 11 % 10 equals 1.
|
|
|
65 |
* # <tt>-</tt> -- minus, negative.
|
|
|
66 |
* # <tt>+</tt> -- addition.
|
|
|
67 |
* # <tt>&</tt> -- conditional AND.
|
|
|
68 |
* # <tt>&&</tt> -- condition AND with short circuit.
|
|
|
69 |
* # <tt>|</tt> -- conditional OR.
|
|
|
70 |
* # <tt>||</tt> -- conditional OR with short circuit.
|
|
|
71 |
* # <tt>!</tt> -- negation.
|
|
|
72 |
*
|
|
|
73 |
* Additional round brackets can also be used to perform grouping.
|
|
|
74 |
*
|
|
|
75 |
* @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
76 |
* @version v1.0, last update on Fri Dec 24 20:46:16 EST 2004
|
|
|
77 |
* @package System.I18N.core
|
|
|
78 |
*/
|
|
|
79 |
class ChoiceFormat
|
|
|
80 |
{
|
|
|
81 |
/**
|
|
|
82 |
* The pattern to validate a set notation
|
|
|
83 |
* @var string
|
|
|
84 |
*/
|
|
|
85 |
protected $validate = '/[\(\[\{]|[-Inf\d:\s]+|,|[\+Inf\d\s:\?\-=!><%\|&\(\)]+|[\)\]\}]/ms';
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* The pattern to parse the formatting string.
|
|
|
89 |
* @var string
|
|
|
90 |
*/
|
|
|
91 |
protected $parse = '/\s*\|?([\(\[\{]([-Inf\d:\s]+,?[\+Inf\d\s:\?\-=!><%\|&\(\)]*)+[\)\]\}])\s*/';
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* The value for positive infinity.
|
|
|
95 |
* @var float
|
|
|
96 |
*/
|
|
|
97 |
protected $inf;
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Constructor.
|
|
|
102 |
*/
|
|
|
103 |
function __construct()
|
|
|
104 |
{
|
|
|
105 |
$this->inf = -log(0);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Determine if the given number belongs to a given set
|
|
|
111 |
* @param float the number to test.
|
|
|
112 |
* @param string the set, in set notation.
|
|
|
113 |
* @return boolean true if number is in the set, false otherwise.
|
|
|
114 |
*/
|
|
|
115 |
function isValid($number, $set)
|
|
|
116 |
{
|
|
|
117 |
$n = preg_match_all($this->validate,$set,$matches,PREG_SET_ORDER);
|
|
|
118 |
|
|
|
119 |
if($n < 3) throw new Exception("Invalid set \"{$set}\"");
|
|
|
120 |
|
|
|
121 |
if(preg_match('/\{\s*n:([^\}]+)\}/', $set, $def))
|
|
|
122 |
{
|
|
|
123 |
return $this->isValidSetNotation($number, $def[1]);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
$leftBracket = $matches[0][0];
|
|
|
127 |
$rightBracket = $matches[$n-1][0];
|
|
|
128 |
|
|
|
129 |
$i = 0;
|
|
|
130 |
$elements = array();
|
|
|
131 |
foreach($matches as $match)
|
|
|
132 |
{
|
|
|
133 |
$string = $match[0];
|
|
|
134 |
if($i != 0 && $i != $n-1 && $string !== ',')
|
|
|
135 |
{
|
|
|
136 |
if($string == '-Inf')
|
|
|
137 |
$elements[] = -1*$this->inf;
|
|
|
138 |
else if ($string == '+Inf' || $string == 'Inf')
|
|
|
139 |
$elements[] = $this->inf;
|
|
|
140 |
else
|
|
|
141 |
$elements[] = floatval($string);
|
|
|
142 |
}
|
|
|
143 |
$i++;
|
|
|
144 |
}
|
|
|
145 |
$total = count($elements);
|
|
|
146 |
$number = floatval($number);
|
|
|
147 |
|
|
|
148 |
if($leftBracket == '{' && $rightBracket == '}')
|
|
|
149 |
return in_array($number, $elements);
|
|
|
150 |
|
|
|
151 |
$left = false;
|
|
|
152 |
if($leftBracket == '[')
|
|
|
153 |
$left = $number >= $elements[0];
|
|
|
154 |
else if ($leftBracket == '(')
|
|
|
155 |
$left = $number > $elements[0];
|
|
|
156 |
|
|
|
157 |
$right = false;
|
|
|
158 |
if($rightBracket==']')
|
|
|
159 |
$right = $number <= $elements[$total-1];
|
|
|
160 |
else if($rightBracket == ')')
|
|
|
161 |
$right = $number < $elements[$total-1];
|
|
|
162 |
|
|
|
163 |
if($left && $right) return true;
|
|
|
164 |
|
|
|
165 |
return false;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
protected function isValidSetNotation($number, $set)
|
|
|
169 |
{
|
|
|
170 |
$str = '$result = '.str_replace('n', '$number', $set).';';
|
|
|
171 |
try
|
|
|
172 |
{
|
|
|
173 |
eval($str);
|
|
|
174 |
return $result;
|
|
|
175 |
}
|
|
|
176 |
catch(Exception $e)
|
|
|
177 |
{
|
|
|
178 |
return false;
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Parse a choice string and get a list of sets and a list of strings
|
|
|
184 |
* corresponding to the sets.
|
|
|
185 |
* @param string the string containing the choices
|
|
|
186 |
* @return array array($sets, $strings)
|
|
|
187 |
*/
|
|
|
188 |
function parse($string)
|
|
|
189 |
{
|
|
|
190 |
$n = preg_match_all($this->parse,$string,$matches, PREG_OFFSET_CAPTURE);
|
|
|
191 |
$sets = array();
|
|
|
192 |
foreach($matches[1] as $match)
|
|
|
193 |
$sets[] = $match[0];
|
|
|
194 |
$offset = $matches[0];
|
|
|
195 |
$strings = array();
|
|
|
196 |
for($i = 0; $i < $n; $i++)
|
|
|
197 |
{
|
|
|
198 |
$len = strlen($offset[$i][0]);
|
|
|
199 |
$begin = $i == 0? $len : $offset[$i][1] + $len;
|
|
|
200 |
$end = $i == $n-1 ? strlen($string) : $offset[$i+1][1];
|
|
|
201 |
$strings[] = substr($string, $begin, $end - $begin);
|
|
|
202 |
}
|
|
|
203 |
return array($sets, $strings);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* For the choice string, and a number, find and return the
|
|
|
208 |
* string that satisfied the set within the choices.
|
|
|
209 |
* @param string the choices string.
|
|
|
210 |
* @param float the number to test.
|
|
|
211 |
* @return string the choosen string.
|
|
|
212 |
*/
|
|
|
213 |
public function format($string, $number)
|
|
|
214 |
{
|
|
|
215 |
list($sets, $strings) = $this->parse($string);
|
|
|
216 |
$total = count($sets);
|
|
|
217 |
for($i = 0; $i < $total; $i++)
|
|
|
218 |
{
|
|
|
219 |
if($this->isValid($number, $sets[$i]))
|
|
|
220 |
return $strings[$i];
|
|
|
221 |
}
|
|
|
222 |
return false;
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
?>
|