| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 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*\|?([\(\[\{]([-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 |
|
|---|