123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace Symfony\Component\Translation;
- class Interval
- {
-
- public static function test($number, $interval)
- {
- $interval = trim($interval);
- if (!preg_match('/^'.self::getIntervalRegexp().'$/x', $interval, $matches)) {
- throw new \InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval));
- }
- if ($matches[1]) {
- foreach (explode(',', $matches[2]) as $n) {
- if ($number == $n) {
- return true;
- }
- }
- } else {
- $leftNumber = self::convertNumber($matches['left']);
- $rightNumber = self::convertNumber($matches['right']);
- return
- ('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber)
- && (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber)
- ;
- }
- return false;
- }
-
- public static function getIntervalRegexp()
- {
- return <<<EOF
- ({\s*
- (\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
- \s*})
- |
- (?P<left_delimiter>[\[\]])
- \s*
- (?P<left>-Inf|\-?\d+(\.\d+)?)
- \s*,\s*
- (?P<right>\+?Inf|\-?\d+(\.\d+)?)
- \s*
- (?P<right_delimiter>[\[\]])
- EOF;
- }
- private static function convertNumber($number)
- {
- if ('-Inf' === $number) {
- return log(0);
- } elseif ('+Inf' === $number || 'Inf' === $number) {
- return -log(0);
- }
- return (float) $number;
- }
- }
|