Semver.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 array $versions
  40. * @param string $constraints
  41. *
  42. * @return array
  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 array $versions
  55. *
  56. * @return array
  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 array $versions
  66. *
  67. * @return array
  68. */
  69. public static function rsort(array $versions)
  70. {
  71. return self::usort($versions, self::SORT_DESC);
  72. }
  73. /**
  74. * @param array $versions
  75. * @param int $direction
  76. *
  77. * @return array
  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. $normalized[] = array($versionParser->normalize($version), $key);
  90. }
  91. usort($normalized, function (array $left, array $right) use ($direction) {
  92. if ($left[0] === $right[0]) {
  93. return 0;
  94. }
  95. if (Comparator::lessThan($left[0], $right[0])) {
  96. return -$direction;
  97. }
  98. return $direction;
  99. });
  100. // Recreate input array, using the original indexes which are now in sorted order.
  101. $sorted = array();
  102. foreach ($normalized as $item) {
  103. $sorted[] = $versions[$item[1]];
  104. }
  105. return $sorted;
  106. }
  107. }