IcuVersion.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Intl\Util;
  11. /**
  12. * Facilitates the comparison of ICU version strings.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class IcuVersion
  17. {
  18. /**
  19. * Compares two ICU versions with an operator.
  20. *
  21. * This method is identical to {@link version_compare()}, except that you
  22. * can pass the number of regarded version components in the last argument
  23. * $precision.
  24. *
  25. * Also, a single digit release version and a single digit major version
  26. * are contracted to a two digit release version. If no major version
  27. * is given, it is substituted by zero.
  28. *
  29. * Examples:
  30. *
  31. * IcuVersion::compare('1.2.3', '1.2.4', '==')
  32. * // => false
  33. *
  34. * IcuVersion::compare('1.2.3', '1.2.4', '==', 2)
  35. * // => true
  36. *
  37. * IcuVersion::compare('1.2.3', '12.3', '==')
  38. * // => true
  39. *
  40. * IcuVersion::compare('1', '10', '==')
  41. * // => true
  42. *
  43. * @param string $version1 A version string
  44. * @param string $version2 A version string to compare
  45. * @param string $operator The comparison operator
  46. * @param int|null $precision The number of components to compare. Pass
  47. * NULL to compare the versions unchanged.
  48. *
  49. * @return bool Whether the comparison succeeded
  50. *
  51. * @see normalize()
  52. */
  53. public static function compare($version1, $version2, $operator, $precision = null)
  54. {
  55. $version1 = self::normalize($version1, $precision);
  56. $version2 = self::normalize($version2, $precision);
  57. return version_compare($version1, $version2, $operator);
  58. }
  59. /**
  60. * Normalizes a version string to the number of components given in the
  61. * parameter $precision.
  62. *
  63. * A single digit release version and a single digit major version are
  64. * contracted to a two digit release version. If no major version is given,
  65. * it is substituted by zero.
  66. *
  67. * Examples:
  68. *
  69. * IcuVersion::normalize('1.2.3.4');
  70. * // => '12.3.4'
  71. *
  72. * IcuVersion::normalize('1.2.3.4', 1);
  73. * // => '12'
  74. *
  75. * IcuVersion::normalize('1.2.3.4', 2);
  76. * // => '12.3'
  77. *
  78. * @param string $version An ICU version string
  79. * @param int|null $precision The number of components to include. Pass
  80. * NULL to return the version unchanged.
  81. *
  82. * @return string|null The normalized ICU version or NULL if it couldn't be
  83. * normalized.
  84. */
  85. public static function normalize($version, $precision)
  86. {
  87. $version = preg_replace('/^(\d)\.(\d)/', '$1$2', $version);
  88. if (1 === strlen($version)) {
  89. $version .= '0';
  90. }
  91. return Version::normalize($version, $precision);
  92. }
  93. /**
  94. * Must not be instantiated.
  95. */
  96. private function __construct()
  97. {
  98. }
  99. }