[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} * {1,2,3,4}
* *
* A range can represent numbers between two numbers: * An interval can represent numbers between two numbers:
* *
* [1, +Inf] * [1, +Inf]
* ]-1,2[ * ]-1,2[
@ -27,22 +27,24 @@ namespace Symfony\Component\Translation;
* The right delimiter can be [ (exclusive) or ] (inclusive). * The right delimiter can be [ (exclusive) or ] (inclusive).
* Beside numbers, you can use -Inf and +Inf for the infinite. * 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> * @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 integer $number A number
* @param string $range A range of numbers * @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)) { if (!preg_match('/^'.self::getIntervalRegexp().'$/x', $interval, $matches)) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid range expression.', $range)); throw new \InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval));
} }
if ($matches[1]) { 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) * @return string A Regexp (without the delimiters)
*/ */
static public function getRangeRegexp() static public function getIntervalRegexp()
{ {
return <<<EOF return <<<EOF
({\s* ({\s*

View File

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

View File

@ -11,16 +11,16 @@
namespace Symfony\Tests\Component\Translation; 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 * @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() public function testTestException()
{ {
Range::test(1, 'foobar'); Interval::test(1, 'foobar');
} }
public function getTests() public function getTests()