[ Index ] |
|
Code source de PRADO 3.0.6 |
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 * # <t>[1,2]</t> -- accepts values between 1 and 2, inclusive. 45 * # <t>(1,2)</t> -- accepts values between 1 and 2, excluding 1 and 2. 46 * # <t>{1,2,3,4}</t> -- only values defined in the set are accepted. 47 * # <t>[-Inf,0)</t> -- 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 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com> 53 * @version v1.0, last update on Fri Dec 24 20:46:16 EST 2004 54 * @package System.I18N.core 55 */ 56 class ChoiceFormat 57 { 58 /** 59 * The pattern to validate a set notation 60 * @var string 61 */ 62 protected $validate = '/[\(\[\{]|[-Inf\d]+|,|[\+Inf\d]+|[\)\]\}]/ms'; 63 64 /** 65 * The pattern to parse the formatting string. 66 * @var string 67 */ 68 protected $parse = '/\s?\|?([\(\[\{]([-Inf\d]+,?[\+Inf\d]*)+[\)\]\}])\s?/'; 69 70 /** 71 * The value for positive infinity. 72 * @var float 73 */ 74 protected $inf; 75 76 77 /** 78 * Constructor. 79 */ 80 function __construct() 81 { 82 $this->inf = -log(0); 83 } 84 85 86 /** 87 * Determine if the given number belongs to a given set 88 * @param float the number to test. 89 * @param string the set, in set notation. 90 * @return boolean true if number is in the set, false otherwise. 91 */ 92 function isValid($number, $set) 93 { 94 $n = preg_match_all($this->validate,$set,$matches,PREG_SET_ORDER); 95 96 if($n < 3) throw new Exception("Invalid set \"{$set}\""); 97 98 $leftBracket = $matches[0][0]; 99 $rightBracket = $matches[$n-1][0]; 100 101 $i = 0; 102 $elements = array(); 103 104 foreach($matches as $match) 105 { 106 $string = $match[0]; 107 if($i != 0 && $i != $n-1 && $string !== ',') 108 { 109 if($string == '-Inf') 110 $elements[] = -1*$this->inf; 111 else if ($string == '+Inf' || $string == 'Inf') 112 $elements[] = $this->inf; 113 else 114 $elements[] = floatval($string); 115 } 116 $i++; 117 } 118 $total = count($elements); 119 $number = floatval($number); 120 121 if($leftBracket == '{' && $rightBracket == '}') 122 return in_array($number, $elements); 123 124 $left = false; 125 if($leftBracket == '[') 126 $left = $number >= $elements[0]; 127 else if ($leftBracket == '(') 128 $left = $number > $elements[0]; 129 130 $right = false; 131 if($rightBracket==']') 132 $right = $number <= $elements[$total-1]; 133 else if($rightBracket == ')') 134 $right = $number < $elements[$total-1]; 135 136 if($left && $right) return true; 137 138 return false; 139 } 140 141 142 /** 143 * Parse a choice string and get a list of sets and a list of strings 144 * corresponding to the sets. 145 * @param string the string containing the choices 146 * @return array array($sets, $strings) 147 */ 148 function parse($string) 149 { 150 $n = preg_match_all($this->parse,$string,$matches, PREG_OFFSET_CAPTURE); 151 $sets = array(); 152 foreach($matches[1] as $match) 153 $sets[] = $match[0]; 154 $offset = $matches[0]; 155 $strings = array(); 156 for($i = 0; $i < $n; $i++) 157 { 158 $len = strlen($offset[$i][0]); 159 $begin = $i == 0? $len : $offset[$i][1] + $len; 160 $end = $i == $n-1 ? strlen($string) : $offset[$i+1][1]; 161 $strings[] = substr($string, $begin, $end - $begin); 162 } 163 return array($sets, $strings); 164 } 165 166 /** 167 * For the choice string, and a number, find and return the 168 * string that satisfied the set within the choices. 169 * @param string the choices string. 170 * @param float the number to test. 171 * @return string the choosen string. 172 */ 173 public function format($string, $number) 174 { 175 list($sets, $strings) = $this->parse($string); 176 $total = count($sets); 177 for($i = 0; $i < $total; $i++) 178 { 179 if($this->isValid($number, $sets[$i])) 180 return $strings[$i]; 181 } 182 return false; 183 } 184 } 185 186 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |