vendor/symfony/src/Symfony/Component/Translation/Interval.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Translation;
       
    13 
       
    14 /**
       
    15  * Tests if a given number belongs to a given math interval.
       
    16  *
       
    17  * An interval can represent a finite set of numbers:
       
    18  *
       
    19  *  {1,2,3,4}
       
    20  *
       
    21  * An interval can represent numbers between two numbers:
       
    22  *
       
    23  *  [1, +Inf]
       
    24  *  ]-1,2[
       
    25  *
       
    26  * The left delimiter can be [ (inclusive) or ] (exclusive).
       
    27  * The right delimiter can be [ (exclusive) or ] (inclusive).
       
    28  * Beside numbers, you can use -Inf and +Inf for the infinite.
       
    29  *
       
    30  * @see http://en.wikipedia.org/wiki/Interval_%28mathematics%29#The_ISO_notation
       
    31  *
       
    32  * @author Fabien Potencier <fabien@symfony.com>
       
    33  */
       
    34 class Interval
       
    35 {
       
    36     /**
       
    37      * Tests if the given number is in the math interval.
       
    38      *
       
    39      * @param integer $number   A number
       
    40      * @param string  $interval An interval
       
    41      */
       
    42     static public function test($number, $interval)
       
    43     {
       
    44         $interval = trim($interval);
       
    45 
       
    46         if (!preg_match('/^'.self::getIntervalRegexp().'$/x', $interval, $matches)) {
       
    47             throw new \InvalidArgumentException(sprintf('"%s" is not a valid interval.', $interval));
       
    48         }
       
    49 
       
    50         if ($matches[1]) {
       
    51             foreach (explode(',', $matches[2]) as $n) {
       
    52                 if ($number == $n) {
       
    53                     return true;
       
    54                 }
       
    55             }
       
    56         } else {
       
    57             $leftNumber = self::convertNumber($matches['left']);
       
    58             $rightNumber = self::convertNumber($matches['right']);
       
    59 
       
    60             return
       
    61                 ('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber)
       
    62                 && (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber)
       
    63             ;
       
    64         }
       
    65 
       
    66         return false;
       
    67     }
       
    68 
       
    69     /**
       
    70      * Returns a Regexp that matches valid intervals.
       
    71      *
       
    72      * @return string A Regexp (without the delimiters)
       
    73      */
       
    74     static public function getIntervalRegexp()
       
    75     {
       
    76         return <<<EOF
       
    77         ({\s*
       
    78             (\-?\d+[\s*,\s*\-?\d+]*)
       
    79         \s*})
       
    80 
       
    81             |
       
    82 
       
    83         (?<left_delimiter>[\[\]])
       
    84             \s*
       
    85             (?<left>-Inf|\-?\d+)
       
    86             \s*,\s*
       
    87             (?<right>\+?Inf|\-?\d+)
       
    88             \s*
       
    89         (?<right_delimiter>[\[\]])
       
    90 EOF;
       
    91     }
       
    92 
       
    93     static private function convertNumber($number)
       
    94     {
       
    95         if ('-Inf' === $number) {
       
    96             return log(0);
       
    97         } elseif ('+Inf' === $number || 'Inf' === $number) {
       
    98             return -log(0);
       
    99         }
       
   100 
       
   101         return (int) $number;
       
   102     }
       
   103 }