Interval.php 3.0 KB

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