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