You are here

public static function Interval::test in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/translation/Interval.php \Symfony\Component\Translation\Interval::test()

Tests if the given number is in the math interval.

Parameters

int $number A number:

string $interval An interval:

Return value

bool

Throws

\InvalidArgumentException

3 calls to Interval::test()
IntervalTest::testTest in vendor/symfony/translation/Tests/IntervalTest.php
@dataProvider getTests
IntervalTest::testTestException in vendor/symfony/translation/Tests/IntervalTest.php
@expectedException \InvalidArgumentException
MessageSelector::choose in vendor/symfony/translation/MessageSelector.php
Given a message with different plural translations separated by a pipe (|), this method returns the correct portion of the message based on the given number, locale and the pluralization rules in the message itself.

File

vendor/symfony/translation/Interval.php, line 46

Class

Interval
Tests if a given number belongs to a given math interval.

Namespace

Symfony\Component\Translation

Code

public static function test($number, $interval) {
  $interval = trim($interval);
  if (!preg_match('/^' . self::getIntervalRegexp() . '$/x', $interval, $matches)) {
    throw new \InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval));
  }
  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']);
    return ('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber) && (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber);
  }
  return false;
}