IcuVersion.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 int|null $precision The number of components to compare. Pass
  44. * NULL to compare the versions unchanged.
  45. *
  46. * @return bool Whether the comparison succeeded
  47. *
  48. * @see normalize()
  49. */
  50. public static function compare(string $version1, string $version2, string $operator, ?int $precision = null)
  51. {
  52. $version1 = self::normalize($version1, $precision);
  53. $version2 = self::normalize($version2, $precision);
  54. return version_compare($version1, $version2, $operator);
  55. }
  56. /**
  57. * Normalizes a version string to the number of components given in the
  58. * parameter $precision.
  59. *
  60. * A single digit release version and a single digit major version are
  61. * contracted to a two digit release version. If no major version is given,
  62. * it is substituted by zero.
  63. *
  64. * Examples:
  65. *
  66. * IcuVersion::normalize('1.2.3.4');
  67. * // => '12.3.4'
  68. *
  69. * IcuVersion::normalize('1.2.3.4', 1);
  70. * // => '12'
  71. *
  72. * IcuVersion::normalize('1.2.3.4', 2);
  73. * // => '12.3'
  74. *
  75. * @param int|null $precision The number of components to include. Pass
  76. * NULL to return the version unchanged.
  77. *
  78. * @return string|null the normalized ICU version or NULL if it couldn't be
  79. * normalized
  80. */
  81. public static function normalize(string $version, ?int $precision)
  82. {
  83. $version = preg_replace('/^(\d)\.(\d)/', '$1$2', $version);
  84. if (1 === \strlen($version)) {
  85. $version .= '0';
  86. }
  87. return Version::normalize($version, $precision);
  88. }
  89. /**
  90. * Must not be instantiated.
  91. */
  92. private function __construct()
  93. {
  94. }
  95. }