Semver.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * This file is part of composer/semver.
  4. *
  5. * (c) Composer <https://github.com/composer>
  6. *
  7. * For the full copyright and license information, please view
  8. * the LICENSE file that was distributed with this source code.
  9. */
  10. namespace Composer\Semver;
  11. use Composer\Semver\Constraint\Constraint;
  12. class Semver
  13. {
  14. const SORT_ASC = 1;
  15. const SORT_DESC = -1;
  16. /** @var VersionParser */
  17. private static $versionParser;
  18. /**
  19. * Determine if given version satisfies given constraints.
  20. *
  21. * @param string $version
  22. * @param string $constraints
  23. *
  24. * @return bool
  25. */
  26. public static function satisfies($version, $constraints)
  27. {
  28. if (null === self::$versionParser) {
  29. self::$versionParser = new VersionParser();
  30. }
  31. $versionParser = self::$versionParser;
  32. $provider = new Constraint('==', $versionParser->normalize($version));
  33. $parsedConstraints = $versionParser->parseConstraints($constraints);
  34. return $parsedConstraints->matches($provider);
  35. }
  36. /**
  37. * Return all versions that satisfy given constraints.
  38. *
  39. * @param string[] $versions
  40. * @param string $constraints
  41. *
  42. * @return string[]
  43. */
  44. public static function satisfiedBy(array $versions, $constraints)
  45. {
  46. $versions = array_filter($versions, function ($version) use ($constraints) {
  47. return Semver::satisfies($version, $constraints);
  48. });
  49. return array_values($versions);
  50. }
  51. /**
  52. * Sort given array of versions.
  53. *
  54. * @param string[] $versions
  55. *
  56. * @return string[]
  57. */
  58. public static function sort(array $versions)
  59. {
  60. return self::usort($versions, self::SORT_ASC);
  61. }
  62. /**
  63. * Sort given array of versions in reverse.
  64. *
  65. * @param string[] $versions
  66. *
  67. * @return string[]
  68. */
  69. public static function rsort(array $versions)
  70. {
  71. return self::usort($versions, self::SORT_DESC);
  72. }
  73. /**
  74. * @param string[] $versions
  75. * @param int $direction
  76. *
  77. * @return string[]
  78. */
  79. private static function usort(array $versions, $direction)
  80. {
  81. if (null === self::$versionParser) {
  82. self::$versionParser = new VersionParser();
  83. }
  84. $versionParser = self::$versionParser;
  85. $normalized = array();
  86. // Normalize outside of usort() scope for minor performance increase.
  87. // Creates an array of arrays: [[normalized, key], ...]
  88. foreach ($versions as $key => $version) {
  89. $normalizedVersion = $versionParser->normalize($version);
  90. $normalizedVersion = $versionParser->normalizeDefaultBranch($normalizedVersion);
  91. $normalized[] = array($normalizedVersion, $key);
  92. }
  93. usort($normalized, function (array $left, array $right) use ($direction) {
  94. if ($left[0] === $right[0]) {
  95. return 0;
  96. }
  97. if (Comparator::lessThan($left[0], $right[0])) {
  98. return -$direction;
  99. }
  100. return $direction;
  101. });
  102. // Recreate input array, using the original indexes which are now in sorted order.
  103. $sorted = array();
  104. foreach ($normalized as $item) {
  105. $sorted[] = $versions[$item[1]];
  106. }
  107. return $sorted;
  108. }
  109. }