This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Translation/Interval.php

104 lines
2.6 KiB
PHP
Raw Normal View History

2010-09-27 08:45:29 +01:00
<?php
/*
* This file is part of the Symfony package.
2010-09-27 08:45:29 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-09-27 08:45:29 +01: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
*/
namespace Symfony\Component\Translation;
2010-09-27 08:45:29 +01:00
/**
* Tests if a given number belongs to a given math interval.
2010-09-27 08:45:29 +01:00
*
* An interval can represent a finite set of numbers:
2010-09-27 08:45:29 +01:00
*
* {1,2,3,4}
*
* 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.
*
* @see http://en.wikipedia.org/wiki/Interval_%28mathematics%29#The_ISO_notation
*
* @author Fabien Potencier <fabien@symfony.com>
2010-09-27 08:45:29 +01:00
*/
class Interval
2010-09-27 08:45:29 +01:00
{
/**
* Tests if the given number is in the math interval.
2010-09-27 08:45:29 +01:00
*
* @param integer $number A number
* @param string $interval An interval
2010-09-27 08:45:29 +01:00
*/
static public function test($number, $interval)
2010-09-27 08:45:29 +01:00
{
$interval = trim($interval);
2010-09-27 08:45:29 +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;
}
/**
* Returns a Regexp that matches valid intervals.
2010-09-27 08:45:29 +01:00
*
* @return string A Regexp (without the delimiters)
*/
static public function getIntervalRegexp()
2010-09-27 08:45:29 +01:00
{
return <<<EOF
({\s*
(\-?\d+[\s*,\s*\-?\d+]*)
\s*})
|
(?P<left_delimiter>[\[\]])
2010-09-27 08:45:29 +01:00
\s*
(?P<left>-Inf|\-?\d+)
2010-09-27 08:45:29 +01:00
\s*,\s*
(?P<right>\+?Inf|\-?\d+)
2010-09-27 08:45:29 +01:00
\s*
(?P<right_delimiter>[\[\]])
2010-09-27 08:45:29 +01:00
EOF;
}
static private 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
return (int) $number;
2010-09-27 08:45:29 +01:00
}
}