composer package updates
[openemr.git] / vendor / symfony / translation / Interval.php
blob9e2cae648c6dde54e9cb7a987a71e87d34629e36
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation;
14 use Symfony\Component\Translation\Exception\InvalidArgumentException;
16 /**
17 * Tests if a given number belongs to a given math interval.
19 * An interval can represent a finite set of numbers:
21 * {1,2,3,4}
23 * An interval can represent numbers between two numbers:
25 * [1, +Inf]
26 * ]-1,2[
28 * The left delimiter can be [ (inclusive) or ] (exclusive).
29 * The right delimiter can be [ (exclusive) or ] (inclusive).
30 * Beside numbers, you can use -Inf and +Inf for the infinite.
32 * @author Fabien Potencier <fabien@symfony.com>
34 * @see http://en.wikipedia.org/wiki/Interval_%28mathematics%29#The_ISO_notation
36 class Interval
38 /**
39 * Tests if the given number is in the math interval.
41 * @param int $number A number
42 * @param string $interval An interval
44 * @return bool
46 * @throws InvalidArgumentException
48 public static function test($number, $interval)
50 $interval = trim($interval);
52 if (!preg_match('/^'.self::getIntervalRegexp().'$/x', $interval, $matches)) {
53 throw new InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval));
56 if ($matches[1]) {
57 foreach (explode(',', $matches[2]) as $n) {
58 if ($number == $n) {
59 return true;
62 } else {
63 $leftNumber = self::convertNumber($matches['left']);
64 $rightNumber = self::convertNumber($matches['right']);
66 return
67 ('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber)
68 && (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber)
72 return false;
75 /**
76 * Returns a Regexp that matches valid intervals.
78 * @return string A Regexp (without the delimiters)
80 public static function getIntervalRegexp()
82 return <<<EOF
83 ({\s*
84 (\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
85 \s*})
89 (?P<left_delimiter>[\[\]])
90 \s*
91 (?P<left>-Inf|\-?\d+(\.\d+)?)
92 \s*,\s*
93 (?P<right>\+?Inf|\-?\d+(\.\d+)?)
94 \s*
95 (?P<right_delimiter>[\[\]])
96 EOF;
99 private static function convertNumber($number)
101 if ('-Inf' === $number) {
102 return log(0);
103 } elseif ('+Inf' === $number || 'Inf' === $number) {
104 return -log(0);
107 return (float) $number;