2010-09-27 08:45:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Translation;
|
|
|
|
|
2010-09-27 08:45:29 +01:00
|
|
|
/**
|
2010-09-28 06:14:43 +01:00
|
|
|
* Tests if a given number belongs to a given math interval.
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
2010-09-28 06:14:43 +01:00
|
|
|
* An interval can represent a finite set of numbers:
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
|
|
|
* {1,2,3,4}
|
|
|
|
*
|
2010-09-28 06:14:43 +01:00
|
|
|
* An interval can represent numbers between two numbers:
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
|
|
|
* [1, +Inf]
|
|
|
|
* ]-1,2[
|
|
|
|
*
|
|
|
|
* The left delimiter can be [ (inclusive) or ] (exclusive).
|
|
|
|
* The right delimiter can be [ (exclusive) or ] (inclusive).
|
|
|
|
* Beside numbers, you can use -Inf and +Inf for the infinite.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
|
|
|
* @see http://en.wikipedia.org/wiki/Interval_%28mathematics%29#The_ISO_notation
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
2010-09-28 06:14:43 +01:00
|
|
|
class Interval
|
2010-09-27 08:45:29 +01:00
|
|
|
{
|
|
|
|
/**
|
2010-09-28 06:14:43 +01:00
|
|
|
* Tests if the given number is in the math interval.
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @param int $number A number
|
|
|
|
* @param string $interval An interval
|
2012-12-16 12:02:54 +00:00
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool
|
2012-12-16 12:02:54 +00:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
2012-07-09 13:50:58 +01:00
|
|
|
public static function test($number, $interval)
|
2010-09-27 08:45:29 +01:00
|
|
|
{
|
2010-09-28 06:14:43 +01:00
|
|
|
$interval = trim($interval);
|
2010-09-27 08:45:29 +01:00
|
|
|
|
2010-09-28 06:14:43 +01:00
|
|
|
if (!preg_match('/^'.self::getIntervalRegexp().'$/x', $interval, $matches)) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval));
|
2010-09-27 08:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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']);
|
|
|
|
|
2011-04-15 20:12:02 +01:00
|
|
|
return
|
2010-09-27 08:45:29 +01:00
|
|
|
('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber)
|
2010-10-31 21:33:08 +00:00
|
|
|
&& (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber)
|
2010-09-27 08:45:29 +01:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-09-28 06:14:43 +01:00
|
|
|
* Returns a Regexp that matches valid intervals.
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
|
|
|
* @return string A Regexp (without the delimiters)
|
|
|
|
*/
|
2012-07-09 13:50:58 +01:00
|
|
|
public static function getIntervalRegexp()
|
2010-09-27 08:45:29 +01:00
|
|
|
{
|
|
|
|
return <<<EOF
|
|
|
|
({\s*
|
2012-11-01 22:19:52 +00:00
|
|
|
(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
|
2010-09-27 08:45:29 +01:00
|
|
|
\s*})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-28 21:05:29 +01:00
|
|
|
(?P<left_delimiter>[\[\]])
|
2010-09-27 08:45:29 +01:00
|
|
|
\s*
|
2012-11-01 22:19:52 +00:00
|
|
|
(?P<left>-Inf|\-?\d+(\.\d+)?)
|
2010-09-27 08:45:29 +01:00
|
|
|
\s*,\s*
|
2012-11-01 22:19:52 +00:00
|
|
|
(?P<right>\+?Inf|\-?\d+(\.\d+)?)
|
2010-09-27 08:45:29 +01:00
|
|
|
\s*
|
2011-08-28 21:05:29 +01:00
|
|
|
(?P<right_delimiter>[\[\]])
|
2010-09-27 08:45:29 +01:00
|
|
|
EOF;
|
|
|
|
}
|
|
|
|
|
2012-07-09 13:50:58 +01:00
|
|
|
private static function convertNumber($number)
|
2010-09-27 08:45:29 +01:00
|
|
|
{
|
|
|
|
if ('-Inf' === $number) {
|
|
|
|
return log(0);
|
|
|
|
} elseif ('+Inf' === $number || 'Inf' === $number) {
|
|
|
|
return -log(0);
|
|
|
|
}
|
2011-02-27 17:29:48 +00:00
|
|
|
|
2012-11-01 22:19:52 +00:00
|
|
|
return (float) $number;
|
2010-09-27 08:45:29 +01:00
|
|
|
}
|
|
|
|
}
|