SymfonyComponentTranslationInterval

Tests if a given number belongs to a given math interval.

Defined (1)

The class is defined in the following location(s).

/vendor/symfony/translation/Interval.php  
  1. class Interval 
  2. /** 
  3. * Tests if the given number is in the math interval. 
  4. * @param int $number A number 
  5. * @param string $interval An interval 
  6. * @return bool 
  7. * @throws \InvalidArgumentException 
  8. */ 
  9. public static function test($number, $interval) 
  10. $interval = trim($interval); 
  11.  
  12. if (!preg_match('/^'.self::getIntervalRegexp().'$/x', $interval, $matches)) { 
  13. throw new \InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval)); 
  14.  
  15. if ($matches[1]) { 
  16. foreach (explode(', ', $matches[2]) as $n) { 
  17. if ($number == $n) { 
  18. return true; 
  19. } else { 
  20. $leftNumber = self::convertNumber($matches['left']); 
  21. $rightNumber = self::convertNumber($matches['right']); 
  22.  
  23. return 
  24. ('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber) 
  25. && (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber) 
  26.  
  27. return false; 
  28.  
  29. /** 
  30. * Returns a Regexp that matches valid intervals. 
  31. * @return string A Regexp (without the delimiters) 
  32. */ 
  33. public static function getIntervalRegexp() 
  34. return <<<EOF 
  35. ({\s* 
  36. (\-?\d+(\.\d+)?[\s*, \s*\-?\d+(\.\d+)?]*) 
  37. \s*}) 
  38.  
  39.  
  40. (?P<left_delimiter>[\[\]]) 
  41. \s* 
  42. (?P<left>-Inf|\-?\d+(\.\d+)?) 
  43. \s*, \s* 
  44. (?P<right>\+?Inf|\-?\d+(\.\d+)?) 
  45. \s* 
  46. (?P<right_delimiter>[\[\]]) 
  47. EOF; 
  48.  
  49. private static function convertNumber($number) 
  50. if ('-Inf' === $number) { 
  51. return log(0); 
  52. } elseif ('+Inf' === $number || 'Inf' === $number) { 
  53. return -log(0); 
  54.  
  55. return (float) $number;