Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Translation] renamed Range to Interval
  • Loading branch information
fabpot committed Sep 28, 2010
1 parent 9e50782 commit 4ac65ce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
Expand Up @@ -12,13 +12,13 @@
*/

/**
* 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[
Expand All @@ -27,22 +27,24 @@
* 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]) {
Expand All @@ -66,11 +68,11 @@ static public function test($number, $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*
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Translation/MessageSelector.php
Expand Up @@ -26,8 +26,8 @@ public function choose($message, $number, $locale)
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 {
Expand All @@ -36,8 +36,8 @@ public function choose($message, $number, $locale)
}

// 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;
}
}
Expand Down
Expand Up @@ -11,24 +11,24 @@

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));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testTestException()
{
Range::test(1, 'foobar');
Interval::test(1, 'foobar');
}

public function getTests()
Expand Down

0 comments on commit 4ac65ce

Please sign in to comment.