VersionConstraintParser.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of PharIo\Version.
  4. *
  5. * (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
  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 PharIo\Version;
  11. class VersionConstraintParser {
  12. /**
  13. * @param string $value
  14. *
  15. * @return VersionConstraint
  16. *
  17. * @throws UnsupportedVersionConstraintException
  18. */
  19. public function parse($value) {
  20. if (strpos($value, '||') !== false) {
  21. return $this->handleOrGroup($value);
  22. }
  23. if (!preg_match('/^[\^~\*]?[\d.\*]+$/', $value)) {
  24. throw new UnsupportedVersionConstraintException(
  25. sprintf('Version constraint %s is not supported.', $value)
  26. );
  27. }
  28. switch ($value[0]) {
  29. case '~':
  30. return $this->handleTildeOperator($value);
  31. case '^':
  32. return $this->handleCaretOperator($value);
  33. }
  34. $version = new VersionConstraintValue($value);
  35. if ($version->getMajor()->isAny()) {
  36. return new AnyVersionConstraint();
  37. }
  38. if ($version->getMinor()->isAny()) {
  39. return new SpecificMajorVersionConstraint(
  40. $value,
  41. $version->getMajor()->getValue()
  42. );
  43. }
  44. if ($version->getPatch()->isAny()) {
  45. return new SpecificMajorAndMinorVersionConstraint(
  46. $value,
  47. $version->getMajor()->getValue(),
  48. $version->getMinor()->getValue()
  49. );
  50. }
  51. return new ExactVersionConstraint($value);
  52. }
  53. /**
  54. * @param $value
  55. *
  56. * @return OrVersionConstraintGroup
  57. */
  58. private function handleOrGroup($value) {
  59. $constraints = [];
  60. foreach (explode('||', $value) as $groupSegment) {
  61. $constraints[] = $this->parse(trim($groupSegment));
  62. }
  63. return new OrVersionConstraintGroup($value, $constraints);
  64. }
  65. /**
  66. * @param string $value
  67. *
  68. * @return AndVersionConstraintGroup
  69. */
  70. private function handleTildeOperator($value) {
  71. $version = new Version(substr($value, 1));
  72. $constraints = [
  73. new GreaterThanOrEqualToVersionConstraint($value, $version)
  74. ];
  75. if ($version->getPatch()->isAny()) {
  76. $constraints[] = new SpecificMajorVersionConstraint(
  77. $value,
  78. $version->getMajor()->getValue()
  79. );
  80. } else {
  81. $constraints[] = new SpecificMajorAndMinorVersionConstraint(
  82. $value,
  83. $version->getMajor()->getValue(),
  84. $version->getMinor()->getValue()
  85. );
  86. }
  87. return new AndVersionConstraintGroup($value, $constraints);
  88. }
  89. /**
  90. * @param string $value
  91. *
  92. * @return AndVersionConstraintGroup
  93. */
  94. private function handleCaretOperator($value) {
  95. $version = new Version(substr($value, 1));
  96. return new AndVersionConstraintGroup(
  97. $value,
  98. [
  99. new GreaterThanOrEqualToVersionConstraint($value, $version),
  100. new SpecificMajorVersionConstraint($value, $version->getMajor()->getValue())
  101. ]
  102. );
  103. }
  104. }