[Translation] renamed Range to Interval

This commit is contained in:
Fabien Potencier 2010-09-28 07:14:43 +02:00
parent 9e50782b9d
commit 4ac65cebcf
3 changed files with 24 additions and 22 deletions

View File

@ -12,13 +12,13 @@ namespace Symfony\Component\Translation;
*/
/**
* Range tests if a given number belongs to a given range.
* Tests if a given number belongs to a given math interval.
*
* A range can represent a finite set of numbers:
* An interval can represent a finite set of numbers:
*
* {1,2,3,4}
*
* A range can represent numbers between two numbers:
* An interval can represent numbers between two numbers:
*
* [1, +Inf]
* ]-1,2[
@ -27,22 +27,24 @@ namespace Symfony\Component\Translation;
* 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.potencier@symfony-project.com>
*/
class Range
class Interval
{
/**
* Tests if the given number is in the range.
* Tests if the given number is in the math interval.
*
* @param integer $number A number
* @param string $range A range of numbers
* @param integer $number A number
* @param string $interval An interval
*/
static public function test($number, $range)
static public function test($number, $interval)
{
$range = trim($range);
$interval = trim($interval);
if (!preg_match('/^'.self::getRangeRegexp().'$/x', $range, $matches)) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid range expression.', $range));
if (!preg_match('/^'.self::getIntervalRegexp().'$/x', $interval, $matches)) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval));
}
if ($matches[1]) {
@ -66,11 +68,11 @@ class Range
}
/**
* Returns a Regexp that matches valid ranges.
* Returns a Regexp that matches valid intervals.
*
* @return string A Regexp (without the delimiters)
*/
static public function getRangeRegexp()
static public function getIntervalRegexp()
{
return <<<EOF
({\s*

View File

@ -26,8 +26,8 @@ class MessageSelector
foreach ($parts as $part) {
$part = trim($part);
if (preg_match('/^(?<range>'.Range::getRangeRegexp().')\s+(?<message>.+?)$/x', $part, $matches)) {
$explicitRules[$matches['range']] = $matches['message'];
if (preg_match('/^(?<interval>'.Interval::getIntervalRegexp().')\s+(?<message>.+?)$/x', $part, $matches)) {
$explicitRules[$matches['interval']] = $matches['message'];
} elseif (preg_match('/^\w+\: +(.+)$/', $part, $matches)) {
$standardRules[] = $matches[1];
} else {
@ -36,8 +36,8 @@ class MessageSelector
}
// try to match an explicit rule, then fallback to the standard ones
foreach ($explicitRules as $range => $m) {
if (Range::test($number, $range)) {
foreach ($explicitRules as $interval => $m) {
if (Interval::test($number, $interval)) {
return $m;
}
}

View File

@ -11,16 +11,16 @@
namespace Symfony\Tests\Component\Translation;
use Symfony\Component\Translation\Range;
use Symfony\Component\Translation\Interval;
class RangeTest extends \PHPUnit_Framework_TestCase
class IntervalTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getTests
*/
public function testTest($expected, $number, $range)
public function testTest($expected, $number, $interval)
{
$this->assertEquals($expected, Range::test($number, $range));
$this->assertEquals($expected, Interval::test($number, $interval));
}
/**
@ -28,7 +28,7 @@ class RangeTest extends \PHPUnit_Framework_TestCase
*/
public function testTestException()
{
Range::test(1, 'foobar');
Interval::test(1, 'foobar');
}
public function getTests()