9e2cae648c6dde54e9cb7a987a71e87d34629e36
[yaffs-website] / vendor / symfony / translation / Interval.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Translation;
13
14 use Symfony\Component\Translation\Exception\InvalidArgumentException;
15
16 /**
17  * Tests if a given number belongs to a given math interval.
18  *
19  * An interval can represent a finite set of numbers:
20  *
21  *  {1,2,3,4}
22  *
23  * An interval can represent numbers between two numbers:
24  *
25  *  [1, +Inf]
26  *  ]-1,2[
27  *
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.
31  *
32  * @author Fabien Potencier <fabien@symfony.com>
33  *
34  * @see    http://en.wikipedia.org/wiki/Interval_%28mathematics%29#The_ISO_notation
35  */
36 class Interval
37 {
38     /**
39      * Tests if the given number is in the math interval.
40      *
41      * @param int    $number   A number
42      * @param string $interval An interval
43      *
44      * @return bool
45      *
46      * @throws InvalidArgumentException
47      */
48     public static function test($number, $interval)
49     {
50         $interval = trim($interval);
51
52         if (!preg_match('/^'.self::getIntervalRegexp().'$/x', $interval, $matches)) {
53             throw new InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval));
54         }
55
56         if ($matches[1]) {
57             foreach (explode(',', $matches[2]) as $n) {
58                 if ($number == $n) {
59                     return true;
60                 }
61             }
62         } else {
63             $leftNumber = self::convertNumber($matches['left']);
64             $rightNumber = self::convertNumber($matches['right']);
65
66             return
67                 ('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber)
68                 && (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber)
69             ;
70         }
71
72         return false;
73     }
74
75     /**
76      * Returns a Regexp that matches valid intervals.
77      *
78      * @return string A Regexp (without the delimiters)
79      */
80     public static function getIntervalRegexp()
81     {
82         return <<<EOF
83         ({\s*
84             (\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
85         \s*})
86
87             |
88
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;
97     }
98
99     private static function convertNumber($number)
100     {
101         if ('-Inf' === $number) {
102             return log(0);
103         } elseif ('+Inf' === $number || 'Inf' === $number) {
104             return -log(0);
105         }
106
107         return (float) $number;
108     }
109 }